From 823b465f32549c27702141db70a0136582bb3c31 Mon Sep 17 00:00:00 2001 From: Tanner Summers Date: Fri, 31 Mar 2023 22:31:27 -0500 Subject: [PATCH 01/11] fix(react): updated textarea counter value changes on re-render --- .../components/TextArea/TextArea.stories.js | 5 + .../src/components/TextArea/TextArea.tsx | 9 +- .../TextArea/__tests__/TextArea-test.js | 304 +++++++++++++++++- 3 files changed, 300 insertions(+), 18 deletions(-) diff --git a/packages/react/src/components/TextArea/TextArea.stories.js b/packages/react/src/components/TextArea/TextArea.stories.js index 6a6ab4ab27c2..2d5d57aa4ec4 100644 --- a/packages/react/src/components/TextArea/TextArea.stories.js +++ b/packages/react/src/components/TextArea/TextArea.stories.js @@ -153,6 +153,11 @@ Playground.argTypes = { }, defaultValue: 'This is a warning message.', }, + value: { + control: { + type: 'text', + }, + }, }; Playground.args = { diff --git a/packages/react/src/components/TextArea/TextArea.tsx b/packages/react/src/components/TextArea/TextArea.tsx index 7bc31b021144..9615f39b937b 100644 --- a/packages/react/src/components/TextArea/TextArea.tsx +++ b/packages/react/src/components/TextArea/TextArea.tsx @@ -6,7 +6,7 @@ */ import PropTypes, { ReactNodeLike } from 'prop-types'; -import React, { useState, useContext, useRef } from 'react'; +import React, { useState, useContext, useRef, useEffect } from 'react'; import classNames from 'classnames'; import deprecate from '../../prop-types/deprecate'; import { WarningFilled, WarningAltFilled } from '@carbon/icons-react'; @@ -155,9 +155,13 @@ const TextArea = React.forwardRef((props: TextAreaProps, forwardRef) => { const enabled = useFeatureFlag('enable-v11-release'); const { defaultValue, value, disabled } = other; const [textCount, setTextCount] = useState( - defaultValue?.toString().length || value?.toString().length || 0 + defaultValue?.toString()?.length || value?.toString()?.length || 0 ); + useEffect(() => { + setTextCount(defaultValue?.toString()?.length || value?.toString()?.length || 0); + }, [value, defaultValue]); + const textareaProps: { id: TextAreaProps['id']; onChange: TextAreaProps['onChange']; @@ -261,6 +265,7 @@ const TextArea = React.forwardRef((props: TextAreaProps, forwardRef) => {