Skip to content

Commit c5a0eee

Browse files
authored
fix: Prevent a controlled TextArea from expanding when autoExpand is false (#947)
1 parent 8c73bd4 commit c5a0eee

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/text-area/text-area.stories.mdx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ export function preventDefault(event) {
2222
event.preventDefault()
2323
}
2424

25-
export function InteractivePropsStory({ label, auxiliaryLabel, ...props }) {
25+
export function InteractivePropsStory({
26+
label,
27+
auxiliaryLabel,
28+
value,
29+
controlled: _controlled,
30+
...props
31+
}) {
2632
return (
2733
<TextArea
2834
{...props}
35+
value={value}
2936
label={label}
3037
auxiliaryLabel={
3138
auxiliaryLabel ? (
@@ -49,7 +56,15 @@ export function InteractivePropsStory({ label, auxiliaryLabel, ...props }) {
4956
control: { type: 'text' },
5057
defaultValue: 'User bio',
5158
},
52-
value: { table: { disable: true } },
59+
controlled: {
60+
control: { type: 'boolean' },
61+
defaultValue: false,
62+
},
63+
value: {
64+
if: { arg: 'controlled', truthy: true },
65+
control: { type: 'text' },
66+
defaultValue: '',
67+
},
5368
tone: {
5469
options: ['neutral', 'success', 'error', 'loading'],
5570
control: { type: 'inline-radio' },

src/text-area/text-area.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,27 @@ describe('TextArea', () => {
248248
expect(screen.getByTestId('value')).toHaveTextContent('I love to travel around the world')
249249
})
250250

251+
it('sets a data-replicated-value attribute on a controlled field when autoExpand is true', () => {
252+
render(
253+
<TextArea
254+
value="I am a Frontend Developer"
255+
label="Tell us a bit about you"
256+
autoExpand
257+
/>,
258+
)
259+
const textareaElement = screen.getByRole('textbox', { name: 'Tell us a bit about you' })
260+
expect(textareaElement.parentElement).toHaveAttribute(
261+
'data-replicated-value',
262+
'I am a Frontend Developer',
263+
)
264+
})
265+
266+
it('does NOT set a data-replicated-value attribute on an uncontrolled field when autoExpand is false', () => {
267+
render(<TextArea value="I am a Frontend Developer" label="Tell us a bit about you" />)
268+
const textareaElement = screen.getByRole('textbox', { name: 'Tell us a bit about you' })
269+
expect(textareaElement.parentElement).not.toHaveAttribute('data-replicated-value')
270+
})
271+
251272
describe('a11y', () => {
252273
it('renders with no a11y violations', async () => {
253274
const { container } = render(

src/text-area/text-area.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function useAutoExpand({
166166

167167
React.useEffect(
168168
function setupAutoExpandWhenControlled() {
169-
if (!isControlled) {
169+
if (!isControlled || !autoExpand) {
170170
return
171171
}
172172

@@ -175,7 +175,7 @@ function useAutoExpand({
175175
containerElement.dataset.replicatedValue = value
176176
}
177177
},
178-
[value, containerRef, isControlled],
178+
[value, containerRef, isControlled, autoExpand],
179179
)
180180
}
181181

0 commit comments

Comments
 (0)