Skip to content

Commit

Permalink
Reset textarea to empty after defaultValue is removed or null
Browse files Browse the repository at this point in the history
This doesn't have an attribute to be removed so we just reset it to empty string.
  • Loading branch information
sebmarkbage committed Apr 9, 2023
1 parent f9c8bfc commit 5e01aee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/react-dom-bindings/src/client/ReactDOMTextarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export function updateTextarea(element: Element, props: Object) {
const node: HTMLTextAreaElement = (element: any);
const value = getToStringValue(props.value);
const defaultValue = getToStringValue(props.defaultValue);
if (defaultValue != null) {
node.defaultValue = toString(defaultValue);
} else {
node.defaultValue = '';
}
if (value != null) {
// Cast `value` to a string to ensure the value is set correctly. While
// browsers typically do this as necessary, jsdom doesn't.
Expand All @@ -74,9 +79,6 @@ export function updateTextarea(element: Element, props: Object) {
node.defaultValue = newValue;
}
}
if (defaultValue != null) {
node.defaultValue = toString(defaultValue);
}
}

export function postInitTextarea(element: Element, props: Object) {
Expand Down
28 changes: 28 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,32 @@ describe('ReactDOMTextarea', () => {
expect(node.value).toBe('foo');
});
});

it('should remove previous `defaultValue`', () => {
const container = document.createElement('div');
const node = ReactDOM.render(
<textarea defaultValue="0" />,
container,
);

expect(node.value).toBe('0');
expect(node.defaultValue).toBe('0');

ReactDOM.render(<textarea />, container);
expect(node.defaultValue).toBe('');
});

it('should treat `defaultValue={null}` as missing', () => {
const container = document.createElement('div');
const node = ReactDOM.render(
<textarea defaultValue="0" />,
container,
);

expect(node.value).toBe('0');
expect(node.defaultValue).toBe('0');

ReactDOM.render(<textarea defaultValue={null} />, container);
expect(node.defaultValue).toBe('');
});
});

0 comments on commit 5e01aee

Please sign in to comment.