2
2
<script >
3
3
import { MDCTextField } from ' @material/textfield'
4
4
import { generateRandomID } from ' ../../../random'
5
- import { onMount } from ' svelte'
5
+ import { afterUpdate , onMount } from ' svelte'
6
6
7
7
export let label = ' '
8
8
export let value = ' '
@@ -16,9 +16,9 @@ const labelID = generateRandomID('text-label-')
16
16
17
17
let element = {}
18
18
let mdcTextField = {}
19
+ let width = ' '
19
20
20
21
$: mdcTextField .value = value
21
- $: width = ` ${ element .offsetWidth } px`
22
22
$: valueLength = value .toString ().length
23
23
$: hasReachedMaxLength = maxlength && valueLength >= maxlength
24
24
$: error = hasReachedMaxLength
@@ -29,6 +29,8 @@ onMount(() => {
29
29
return () => mdcTextField .destroy ()
30
30
})
31
31
32
+ afterUpdate (() => (width = ` ${ element .offsetWidth } px` ))
33
+
32
34
const focus = (node ) => autofocus && node .focus ()
33
35
</script >
34
36
@@ -41,20 +43,13 @@ const focus = (node) => autofocus && node.focus()
41
43
}
42
44
.required {
43
45
color : var (--mdc-required-input , var (--mdc-theme-status-error ));
44
- font-size : small ;
45
- margin-left : 1rem ;
46
- margin-top : 0.2rem ;
47
46
}
48
47
.label-margin {
49
48
margin-left : 1.1rem ;
50
49
}
51
50
.mdc-text-field--label-floating .mdc-floating-label {
52
51
margin-left : 0 ;
53
52
}
54
- .mdc-text-field-helper-line {
55
- float : right ;
56
- padding-right : 1rem ;
57
- }
58
53
</style >
59
54
60
55
<label
@@ -71,6 +66,8 @@ const focus = (node) => autofocus && node.focus()
71
66
min =" 0"
72
67
class =" mdc-text-field__input"
73
68
aria-labelledby ={labelID }
69
+ aria-controls =" {labelID }-helper-id"
70
+ aria-describedby =" {labelID }-helper-id"
74
71
bind:value
75
72
use:focus
76
73
on:blur
@@ -99,15 +96,18 @@ const focus = (node) => autofocus && node.focus()
99
96
<span class =" mdc-notched-outline__trailing" />
100
97
</span >
101
98
</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 >
106
108
{#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 }
111
111
</div >
112
112
{/if }
113
113
</div >
0 commit comments