Skip to content

Commit 67b145a

Browse files
committedDec 16, 2021
feat(TextField): add maxlength warning, fix width
1 parent 68bce67 commit 67b145a

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed
 

‎components/mdc/TextInput/TextField.svelte

+17-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script>
33
import { MDCTextField } from '@material/textfield'
44
import { generateRandomID } from '../../../random'
5-
import { onMount } from 'svelte'
5+
import { afterUpdate, onMount } from 'svelte'
66
77
export let label = ''
88
export let value = ''
@@ -17,9 +17,9 @@ const labelID = generateRandomID('text-label-')
1717
1818
let element = {}
1919
let mdcTextField = {}
20+
let width = ''
2021
2122
$: mdcTextField.value = value
22-
$: width = `${element.offsetWidth}px`
2323
$: hasReachedMaxLength = maxlength && value.length >= maxlength
2424
$: error = hasReachedMaxLength
2525
$: showCounter = maxlength && value.length / maxlength > 0.85
@@ -29,6 +29,8 @@ onMount(() => {
2929
return () => mdcTextField.destroy()
3030
})
3131
32+
afterUpdate(() => (width = `${element.offsetWidth}px`))
33+
3234
const focus = (node) => autofocus && node.focus()
3335
</script>
3436

@@ -41,20 +43,13 @@ const focus = (node) => autofocus && node.focus()
4143
}
4244
.required {
4345
color: var(--mdc-required-input, var(--mdc-theme-status-error));
44-
font-size: small;
45-
margin-left: 1rem;
46-
margin-top: 0.2rem;
4746
}
4847
.label-margin {
4948
margin-left: 1.1rem;
5049
}
5150
.mdc-text-field--label-floating .mdc-floating-label {
5251
margin-left: 0;
5352
}
54-
.mdc-text-field-helper-line {
55-
float: right;
56-
padding-right: 1rem;
57-
}
5853
</style>
5954

6055
<label
@@ -69,6 +64,8 @@ const focus = (node) => autofocus && node.focus()
6964
type="text"
7065
class="mdc-text-field__input"
7166
aria-labelledby={labelID}
67+
aria-controls="{labelID}-helper-id"
68+
aria-describedby="{labelID}-helper-id"
7269
bind:value
7370
use:focus
7471
on:blur
@@ -98,15 +95,18 @@ const focus = (node) => autofocus && node.focus()
9895
<span class="mdc-notched-outline__trailing" />
9996
</span>
10097
</label>
101-
<div style="width: {width};">
102-
{#if required && !value}
103-
<div class="required d-inline">*Required</div>
104-
{/if}
98+
<div class="mdc-text-field-helper-line" style="width: {width};">
99+
<div class="mdc-text-field-helper-text" id="{labelID}-helper-id" aria-hidden="true">
100+
{#if required && !value}
101+
<span class="required">*Required</span>
102+
{/if}
103+
{#if showCounter}
104+
<span class="error">Maximum {maxlength} characters</span>
105+
{/if}
106+
</div>
105107
{#if showCounter}
106-
<div class="mdc-text-field-helper-line">
107-
<div class="mdc-text-field-character-counter" class:error>
108-
{value.length} / {maxlength}
109-
</div>
108+
<div class="mdc-text-field-character-counter" class:error>
109+
{value.length} / {maxlength}
110110
</div>
111111
{/if}
112112
</div>

0 commit comments

Comments
 (0)
Please sign in to comment.