@@ -37,6 +37,7 @@ type BaseCharacterCountProps = {
37
37
id : string
38
38
name : string
39
39
maxLength : number
40
+ value ?: string
40
41
defaultValue ?: string
41
42
className ?: string
42
43
isTextArea ?: boolean
@@ -57,6 +58,7 @@ export const CharacterCount = ({
57
58
name,
58
59
className,
59
60
maxLength,
61
+ value = '' ,
60
62
defaultValue = '' ,
61
63
isTextArea = false ,
62
64
getCharacterCount = defaultCharacterCount ,
@@ -65,7 +67,7 @@ export const CharacterCount = ({
65
67
} :
66
68
| TextInputCharacterCountProps
67
69
| TextareaCharacterCountProps ) : React . ReactElement => {
68
- const initialCount = getCharacterCount ( defaultValue )
70
+ const initialCount = getCharacterCount ( value || defaultValue )
69
71
const [ length , setLength ] = useState ( initialCount )
70
72
const [ message , setMessage ] = useState ( getMessage ( initialCount , maxLength ) )
71
73
const [ isValid , setIsValid ] = useState ( initialCount < maxLength )
@@ -114,18 +116,17 @@ export const CharacterCount = ({
114
116
const { onBlur, onChange, inputRef, ...textAreaProps } =
115
117
remainingProps as Partial < TextareaCharacterCountProps >
116
118
117
- InputComponent = (
118
- < Textarea
119
- id = { id }
120
- name = { name }
121
- className = { classes }
122
- defaultValue = { defaultValue }
123
- onBlur = { ( e ) : void => handleBlur ( e , onBlur ) }
124
- onChange = { ( e ) : void => handleChange ( e , onChange ) }
125
- inputRef = { inputRef }
126
- { ...textAreaProps }
127
- />
128
- )
119
+ const attributes = {
120
+ id : id ,
121
+ name : name ,
122
+ className : classes ,
123
+ ...( value ? { value : value } : { defaultValue : defaultValue } ) ,
124
+ onBlur : ( e : React . FocusEvent < HTMLTextAreaElement , Element > ) : void => handleBlur ( e , onBlur ) ,
125
+ onChange : ( e : React . ChangeEvent < HTMLTextAreaElement > ) : void => handleChange ( e , onChange ) ,
126
+ inputRef : inputRef ,
127
+ ...textAreaProps ,
128
+ }
129
+ InputComponent = ( < Textarea { ...attributes } /> )
129
130
} else {
130
131
const {
131
132
onBlur,
@@ -134,20 +135,18 @@ export const CharacterCount = ({
134
135
type = 'text' ,
135
136
...inputProps
136
137
} = remainingProps as Partial < TextInputCharacterCountProps >
137
-
138
- InputComponent = (
139
- < TextInput
140
- id = { id }
141
- type = { type }
142
- name = { name }
143
- className = { classes }
144
- defaultValue = { defaultValue }
145
- onBlur = { ( e ) : void => handleBlur ( e , onBlur ) }
146
- onChange = { ( e ) : void => handleChange ( e , onChange ) }
147
- inputRef = { inputRef }
148
- { ...inputProps }
149
- />
150
- )
138
+ const attributes = {
139
+ id : id ,
140
+ type : type ,
141
+ name : name ,
142
+ className : classes ,
143
+ ...( value ? { value : value } : { defaultValue : defaultValue } ) ,
144
+ onBlur : ( e : React . FocusEvent < HTMLInputElement , Element > ) : void => handleBlur ( e , onBlur ) ,
145
+ onChange : ( e : React . ChangeEvent < HTMLInputElement > ) : void => handleChange ( e , onChange ) ,
146
+ inputRef : inputRef ,
147
+ ...inputProps ,
148
+ }
149
+ InputComponent = ( < TextInput { ...attributes } /> )
151
150
}
152
151
153
152
return (
0 commit comments