Skip to content

Commit 9de5b9d

Browse files
werdnanoslengidjin
andauthored
fix: allow CharacterCount value ^ defaultValue (#2397)
Co-authored-by: John Gedeon <[email protected]>
1 parent ef2be8d commit 9de5b9d

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

src/components/forms/CharacterCount/CharacterCount.test.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,33 @@ describe('CharacterCount component', () => {
132132

133133
it('handles own props', () => {
134134
const tRef = React.createRef<HTMLTextAreaElement>()
135-
const { getByRole } = render(
136-
<CharacterCount
137-
id="character-count-id"
138-
name="character-count"
139-
defaultValue="Prefill this value"
140-
isTextArea
141-
rows={5}
142-
maxLength={10}
143-
inputRef={tRef}
144-
/>
135+
const { getByDisplayValue } = render(
136+
<>
137+
<CharacterCount
138+
id="character-count-defaultValue"
139+
name="character-count-defaultValue"
140+
defaultValue="Prefilled defaultValue"
141+
isTextArea
142+
rows={5}
143+
maxLength={10}
144+
inputRef={tRef}
145+
/>
146+
<CharacterCount
147+
id="character-count-value"
148+
name="character-count-value"
149+
value="Prefilled value"
150+
isTextArea
151+
rows={5}
152+
maxLength={10}
153+
/>
154+
</>
145155
)
146-
const textarea = getByRole('textbox')
147-
expect(textarea).toHaveAttribute('name', 'character-count')
148-
expect(textarea).toHaveAttribute('rows', '5')
149-
expect(textarea).toHaveTextContent('Prefill this value')
150-
expect(textarea).toBe(tRef.current)
156+
const textareaDefaultValue = getByDisplayValue('Prefilled defaultValue')
157+
expect(textareaDefaultValue).toHaveAttribute('name', 'character-count-defaultValue')
158+
expect(textareaDefaultValue).toHaveAttribute('rows', '5')
159+
expect(textareaDefaultValue).toBe(tRef.current)
160+
const textareaValue = getByDisplayValue('Prefilled value')
161+
expect(textareaValue).toHaveAttribute('id', 'character-count-value')
151162
})
152163

153164
it('calls own onChange and onBlur functions', () => {

src/components/forms/CharacterCount/CharacterCount.tsx

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type BaseCharacterCountProps = {
3737
id: string
3838
name: string
3939
maxLength: number
40+
value?: string
4041
defaultValue?: string
4142
className?: string
4243
isTextArea?: boolean
@@ -57,6 +58,7 @@ export const CharacterCount = ({
5758
name,
5859
className,
5960
maxLength,
61+
value = '',
6062
defaultValue = '',
6163
isTextArea = false,
6264
getCharacterCount = defaultCharacterCount,
@@ -65,7 +67,7 @@ export const CharacterCount = ({
6567
}:
6668
| TextInputCharacterCountProps
6769
| TextareaCharacterCountProps): React.ReactElement => {
68-
const initialCount = getCharacterCount(defaultValue)
70+
const initialCount = getCharacterCount(value || defaultValue)
6971
const [length, setLength] = useState(initialCount)
7072
const [message, setMessage] = useState(getMessage(initialCount, maxLength))
7173
const [isValid, setIsValid] = useState(initialCount < maxLength)
@@ -114,18 +116,17 @@ export const CharacterCount = ({
114116
const { onBlur, onChange, inputRef, ...textAreaProps } =
115117
remainingProps as Partial<TextareaCharacterCountProps>
116118

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} />)
129130
} else {
130131
const {
131132
onBlur,
@@ -134,20 +135,18 @@ export const CharacterCount = ({
134135
type = 'text',
135136
...inputProps
136137
} = 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} />)
151150
}
152151

153152
return (

0 commit comments

Comments
 (0)