Skip to content

Commit 5966db5

Browse files
author
losgif
authored
feat: re-render with new autoSize property. (#13)
* feat: re-render with new autoSize property. * test: add test to auto calculate according to autoSize property. * chore: install shallowequal to npm packages * fix: improve comparison logic and test module
1 parent cbad703 commit 5966db5

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@
4545
"@babel/runtime": "^7.10.1",
4646
"classnames": "^2.2.1",
4747
"rc-resize-observer": "^1.0.0",
48-
"rc-util": "^5.7.0"
48+
"rc-util": "^5.7.0",
49+
"shallowequal": "^1.1.0"
4950
},
5051
"devDependencies": {
5152
"@types/classnames": "^2.2.9",
5253
"@types/enzyme": "^3.10.10",
5354
"@types/react": "^16.9.2",
5455
"@types/react-dom": "^16.9.0",
56+
"@types/shallowequal": "^1.1.1",
5557
"@umijs/fabric": "^2.0.8",
5658
"coveralls": "^3.0.6",
5759
"cross-env": "^7.0.2",

src/ResizableTextArea.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import ResizeObserver from 'rc-resize-observer';
33
import omit from 'rc-util/lib/omit';
44
import classNames from 'classnames';
55
import calculateNodeHeight from './calculateNodeHeight';
6-
import { TextAreaProps } from '.';
6+
import type { TextAreaProps } from '.';
7+
import shallowEqual from 'shallowequal';
78

89
// eslint-disable-next-line @typescript-eslint/naming-convention
910
enum RESIZE_STATUS {
@@ -43,8 +44,11 @@ class ResizableTextArea extends React.Component<TextAreaProps, TextAreaState> {
4344
};
4445

4546
componentDidUpdate(prevProps: TextAreaProps) {
46-
// Re-render with the new content then recalculate the height as required.
47-
if (prevProps.value !== this.props.value) {
47+
// Re-render with the new content or new autoSize property then recalculate the height as required.
48+
if (
49+
prevProps.value !== this.props.value ||
50+
!shallowEqual(prevProps.autoSize, this.props.autoSize)
51+
) {
4852
this.resizeTextarea();
4953
}
5054
}

tests/index.spec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('TextArea', () => {
4141
expect(onChange.mock.calls[0][0].target.value).toBe('222');
4242
});
4343

44-
it('should auto calculate height according to content length', async () => {
44+
it('should auto calculate height according to content length and autoSize property', async () => {
4545
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
4646

4747
const wrapper = mount(
@@ -62,6 +62,24 @@ describe('TextArea', () => {
6262
wrapper.setProps({ value: '1111' });
6363
await sleep(0);
6464
expect(mockFunc).toHaveBeenCalledTimes(2);
65+
66+
wrapper.setProps({ autoSize: { minRows: 1, maxRows: 6 } });
67+
await sleep(0);
68+
expect(mockFunc).toHaveBeenCalledTimes(3);
69+
wrapper.setProps({ autoSize: { minRows: 1, maxRows: 6 } });
70+
await sleep(0);
71+
expect(mockFunc).toHaveBeenCalledTimes(3);
72+
wrapper.setProps({ autoSize: { minRows: 1, maxRows: 12 } });
73+
await sleep(0);
74+
expect(mockFunc).toHaveBeenCalledTimes(4);
75+
76+
wrapper.setProps({ autoSize: true });
77+
await sleep(0);
78+
expect(mockFunc).toHaveBeenCalledTimes(5);
79+
wrapper.setProps({ autoSize: false });
80+
await sleep(0);
81+
expect(mockFunc).toHaveBeenCalledTimes(6);
82+
6583
wrapper.update();
6684
expect(wrapper.find('textarea').props().style.overflow).toBeFalsy();
6785

0 commit comments

Comments
 (0)