Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/@react-spectrum/textfield/src/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import {chain} from '@react-aria/utils';
import React, {RefObject, useRef} from 'react';
import React, {RefObject, useCallback, useEffect, useRef} from 'react';
import {SpectrumTextFieldProps, TextFieldRef} from '@react-types/textfield';
import {TextFieldBase} from './TextFieldBase';
import {useProviderProps} from '@react-spectrum/provider';
Expand All @@ -30,13 +30,25 @@ function TextArea(props: SpectrumTextFieldProps, ref: RefObject<TextFieldRef>) {

let inputRef = useRef<HTMLInputElement & HTMLTextAreaElement>();

let onHeightChange = () => {
let onHeightChange = useCallback(() => {
if (isQuiet) {
let input = inputRef.current;
input.style.height = 'auto';
input.style.height = `${input.scrollHeight}px`;
}
};
}, [isQuiet, inputRef]);

useEffect(() => {
if (inputRef.current) {
// if no value or defaultValue is passed
// no need to call onHeightChange
if (!inputRef.current.value) {
return;
}
onHeightChange();
}
}, [onHeightChange]);


let {labelProps, inputProps} = useTextField({
...props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ storiesOf('TextArea', module)
'isQuiet: true',
() => render({isQuiet: true})
)
.add(
'isQuiet, defaultValue',
() => render({isQuiet: true, defaultValue: 'foo '.repeat(10)})
)
.add(
'isQuiet, value',
() => render({isQuiet: true, value: 'foo '.repeat(10)})
)
.add(
'isDisabled: true',
() => render({isDisabled: true})
Expand Down
11 changes: 11 additions & 0 deletions packages/@react-spectrum/textfield/test/TextArea.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ describe('TextArea', () => {
fireEvent.change(input, {target: {value: '15', style: {}}});
expect(input.style.height).toBe(`${mockScrollHeight}px`);
});

it.each`
Name | Component | props
${'v3 TextArea (controlled)'} | ${TextArea} | ${{isQuiet: true, onChange, value: 'foo'}}
${'v3 TextArea (uncontrolled)'} | ${TextArea} | ${{isQuiet: true, onChange, defaultValue: 'foo'}}
`('$Name quiet variant automatically adjusts its vertical height on mount', ({Component, props}) => {
let tree = renderComponent(Component, props);
let input = tree.getByTestId(testId);

expect(input.style.height).toBe(`${mockScrollHeight}px`);
});
});