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 = ' '
@@ -17,9 +17,9 @@ const labelID = generateRandomID('text-label-')
17
17
18
18
let element = {}
19
19
let mdcTextField = {}
20
+ let width = ' '
20
21
21
22
$: mdcTextField .value = value
22
- $: width = ` ${ element .offsetWidth } px`
23
23
$: hasReachedMaxLength = maxlength && value .length >= maxlength
24
24
$: error = hasReachedMaxLength
25
25
$: showCounter = maxlength && value .length / maxlength > 0.85
@@ -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
@@ -69,6 +64,8 @@ const focus = (node) => autofocus && node.focus()
69
64
type =" text"
70
65
class =" mdc-text-field__input"
71
66
aria-labelledby ={labelID }
67
+ aria-controls =" {labelID }-helper-id"
68
+ aria-describedby =" {labelID }-helper-id"
72
69
bind:value
73
70
use:focus
74
71
on:blur
@@ -98,15 +95,18 @@ const focus = (node) => autofocus && node.focus()
98
95
<span class =" mdc-notched-outline__trailing" />
99
96
</span >
100
97
</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 >
105
107
{#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 }
110
110
</div >
111
111
{/if }
112
112
</div >
0 commit comments