diff --git a/.changeset/giant-maps-enjoy.md b/.changeset/giant-maps-enjoy.md new file mode 100644 index 00000000000..3f051d02564 --- /dev/null +++ b/.changeset/giant-maps-enjoy.md @@ -0,0 +1,5 @@ +--- +'@kaizen/components': patch +--- + +TextArea: no longer forced into controlled mode under the hood, allowing value manipulation via JS if needed diff --git a/packages/components/src/TextArea/TextArea.tsx b/packages/components/src/TextArea/TextArea.tsx index ba07c0c2e1b..16206d73fbf 100644 --- a/packages/components/src/TextArea/TextArea.tsx +++ b/packages/components/src/TextArea/TextArea.tsx @@ -33,17 +33,18 @@ export const TextArea = ({ }: TextAreaProps): JSX.Element => { const [internalValue, setInternalValue] = useState< string | number | readonly string[] | undefined - >(autogrow && !value ? defaultValue : undefined) - // ^ holds an internal state of the value so that autogrow can still work with uncontrolled textareas - // essentially forces the textarea into an (interally) controlled mode if autogrow is true and mode is uncontrolled + >(defaultValue) + // ^holds an internal state of the value, used for the autogrow feature if uncontrolled (no `value` provided) - const controlledValue = value ?? internalValue const fallbackRef = useRef(null) const textAreaRef = propsTextAreaRef ?? fallbackRef const onChange = (event: React.ChangeEvent): void => { propsOnChange?.(event) - setInternalValue(event.target.value) + + if (!value && autogrow) { + setInternalValue(event.target.value) + } } return ( @@ -51,7 +52,7 @@ export const TextArea = ({ className={classnames(styles.wrapper, { [styles.wrapperAutogrow]: autogrow, })} - data-value={autogrow ? controlledValue : undefined} + data-value={autogrow ? (value ?? internalValue) : undefined} >