From 2ef02ea749b304defff7965992194ffe07006c8b Mon Sep 17 00:00:00 2001 From: Ryan Carville Date: Thu, 14 Dec 2023 11:29:46 +0100 Subject: [PATCH 1/2] fix(Textarea): update defaultValue and value props logic, styles and stories --- src/components/Textarea/Textarea.stories.tsx | 17 ++++++++++++++++- src/components/Textarea/Textarea.tsx | 12 ++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/components/Textarea/Textarea.stories.tsx b/src/components/Textarea/Textarea.stories.tsx index dc97f12877..7fe1ffd44a 100644 --- a/src/components/Textarea/Textarea.stories.tsx +++ b/src/components/Textarea/Textarea.stories.tsx @@ -68,6 +68,14 @@ export default { defaultValue: { summary: undefined }, }, }, + value: { + type: 'string', + description: 'Value set by parent', + table: { + type: { summary: 'string | undefined' }, + defaultValue: { summary: undefined }, + }, + }, disabled: { type: 'boolean', table: { @@ -152,6 +160,7 @@ export default { type: 'function', description: 'Callback function to return current value on the `Textarea`', table: { + type: { summary: '((value: string) => void) | undefined' }, defaultValue: { summary: undefined }, }, }, @@ -206,7 +215,13 @@ export default { } as Meta; const TextareaTemplate: StoryFn = (args) => { - const [input, setInput] = useState(undefined); + const { value } = args; + const [input, setInput] = useState(value ?? undefined); + useEffect(() => { + if (value) { + setInput(value); + } + }, [value, setInput]); return ( diff --git a/src/components/Textarea/Textarea.tsx b/src/components/Textarea/Textarea.tsx index 163d3dc5d7..d4f2c4abb3 100644 --- a/src/components/Textarea/Textarea.tsx +++ b/src/components/Textarea/Textarea.tsx @@ -89,6 +89,13 @@ export const Textarea = ({ focusOnMount && textareaRef.current?.focus(); }, [focusOnMount, textareaRef]); + useEffect(() => { + const isValueSet = value !== textareaRef.current?.value; + if (textareaRef.current && value && !isValueSet) { + textareaRef.current.value = value; + } + }, [value, textareaRef]); + const autosizeProps = { minRows, maxRows }; const getPaddingRight = () => { @@ -145,7 +152,8 @@ export const Textarea = ({ readOnly={readOnly} ref={textareaRef} required={required} - value={defaultValue ?? value} + defaultValue={defaultValue} + value={value} placeholder={placeholder} onBlur={onBlur} onChange={handleOnChange} @@ -162,12 +170,12 @@ export const Textarea = ({ aria-label={dataTestId} data-test-id={dataTestId} className={merge([ - InputStyles.base, hugWidth ? '' : InputStyles.width, minRows ? '' : InputStyles.height, InputStyles.disabled, InputStyles.readOnly, InputStyles.element, + InputStyles.base, InputStyles.focus, InputStyles.hover, isFocusVisible && FOCUS_STYLE, From 0d50e1a56e4b3f5b44b8fbea60ae4277b9ac9658 Mon Sep 17 00:00:00 2001 From: Ryan Carville Date: Thu, 14 Dec 2023 15:52:42 +0100 Subject: [PATCH 2/2] fix(Textarea): add mroe tests --- src/components/Textarea/Textarea.cy.tsx | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/components/Textarea/Textarea.cy.tsx b/src/components/Textarea/Textarea.cy.tsx index 98c271f603..0981ccf9cc 100644 --- a/src/components/Textarea/Textarea.cy.tsx +++ b/src/components/Textarea/Textarea.cy.tsx @@ -2,7 +2,8 @@ import { IconClipboard16, IconNook16, IconQuestionMark16 } from '@foundation/Icon'; import { Textarea } from './Textarea'; -import { Validation } from '@utilities/validation'; +import { Validation, validationClassMap } from '@utilities/validation'; +import { ExtraAction } from 'src/types/input'; const TEXTAREA_ID = '[data-test-id=fondue-textarea]'; const TEXTAREA_DECORATOR_ID = '[data-test-id=fondue-textarea-decorator]'; @@ -13,7 +14,7 @@ const DEFAULT_TEXT = 'I am some text text.'; const PLACEHOLDER = 'Enter some text in the textarea'; const INPUT_TEXT = 'I am some input text'; const ROW_HEIGHT = 20; -const EXTRA_ACTIONS = [ +const EXTRA_ACTIONS: ExtraAction[] = [ { icon: , title: 'Save to Clipboard', @@ -48,6 +49,11 @@ describe('Textarea Unit tests', () => { cy.get(TEXTAREA_ID).should('have.value', DEFAULT_TEXT); }); + it('sets and gets the value', () => { + cy.mount(