Skip to content

Commit 2b594ae

Browse files
committed
feat(TextField): add required field option
1 parent c85c427 commit 2b594ae

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

components/mdc/TextInput/TextField.svelte

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export let placeholder = ''
1010
export let maxlength = undefined
1111
export let autofocus = false
1212
export let disabled = false
13+
export let required = false
1314
1415
const labelID = generateRandomID('text-label-')
1516
@@ -26,6 +27,15 @@ onMount(() => {
2627
const focus = (node) => autofocus && node.focus()
2728
</script>
2829

30+
<style>
31+
.required {
32+
color: #da1414;
33+
font-size: small;
34+
margin-left: 1rem;
35+
margin-top: 0.2rem;
36+
}
37+
</style>
38+
2939
<label
3040
class="mdc-text-field mdc-text-field--outlined {$$props.class} textfield-radius"
3141
class:mdc-text-field--no-label={!label}
@@ -42,6 +52,7 @@ const focus = (node) => autofocus && node.focus()
4252
on:keydown
4353
on:keypress
4454
on:keyup
55+
{required}
4556
{maxlength}
4657
{disabled}
4758
{placeholder}
@@ -50,9 +61,14 @@ const focus = (node) => autofocus && node.focus()
5061
<span class="mdc-notched-outline__leading" />
5162
{#if label}
5263
<span class="mdc-notched-outline__notch">
53-
<span class="mdc-floating-label" id={labelID}>{label}</span>
64+
<span class="mdc-floating-label" id={labelID}>
65+
{label}
66+
</span>
5467
</span>
5568
{/if}
5669
<span class="mdc-notched-outline__trailing" />
5770
</span>
5871
</label>
72+
{#if required && !value}
73+
<div class="required">*Required</div>
74+
{/if}

stories/TextField.stories.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ let lastKey = ''
2727

2828
<Story name="Default" {args} />
2929

30+
<Story name="Required" args={copyAndModifyArgs(args, { required: true })} />
31+
3032
<Story name="Placeholder" args={copyAndModifyArgs(args, { placeholder: 'Enter text here' })} />
3133

3234
<Story name="Autofocus" args={copyAndModifyArgs(args, { autofocus: true })} />

0 commit comments

Comments
 (0)