Skip to content

Commit 6ce6458

Browse files
committedDec 16, 2021
feat(MoneyInput): add maxlength warning, fix width
1 parent db59648 commit 6ce6458

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed
 

‎components/mdc/TextInput/MoneyInput.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 = ''
@@ -16,9 +16,9 @@ const labelID = generateRandomID('text-label-')
1616
1717
let element = {}
1818
let mdcTextField = {}
19+
let width = ''
1920
2021
$: mdcTextField.value = value
21-
$: width = `${element.offsetWidth}px`
2222
$: valueLength = value.toString().length
2323
$: hasReachedMaxLength = maxlength && valueLength >= maxlength
2424
$: error = hasReachedMaxLength
@@ -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
@@ -71,6 +66,8 @@ const focus = (node) => autofocus && node.focus()
7166
min="0"
7267
class="mdc-text-field__input"
7368
aria-labelledby={labelID}
69+
aria-controls="{labelID}-helper-id"
70+
aria-describedby="{labelID}-helper-id"
7471
bind:value
7572
use:focus
7673
on:blur
@@ -99,15 +96,18 @@ const focus = (node) => autofocus && node.focus()
9996
<span class="mdc-notched-outline__trailing" />
10097
</span>
10198
</label>
102-
<div style="width: {width};">
103-
{#if required && !value}
104-
<div class="required">*Required</div>
105-
{/if}
99+
<div class="mdc-text-field-helper-line" style="width: {width};">
100+
<div class="mdc-text-field-helper-text" id="{labelID}-helper-id" aria-hidden="true">
101+
{#if required && !value}
102+
<span class="required">*Required</span>
103+
{/if}
104+
{#if showCounter}
105+
<span class="error">Maximum {maxlength} characters</span>
106+
{/if}
107+
</div>
106108
{#if showCounter}
107-
<div class="mdc-text-field-helper-line">
108-
<div class="mdc-text-field-character-counter" class:error>
109-
{valueLength} / {maxlength}
110-
</div>
109+
<div class="mdc-text-field-character-counter" class:error>
110+
{valueLength} / {maxlength}
111111
</div>
112112
{/if}
113113
</div>

0 commit comments

Comments
 (0)
Please sign in to comment.