Skip to content

Commit

Permalink
Fix textarea bug
Browse files Browse the repository at this point in the history
The test changes revealed a bug with textarea. It happens because we
currently always insert trailing comment nodes. We should optimize that
away. However, we also don't really support complex children so we
should toString it anyway which is what partial renderer used to do.
  • Loading branch information
sebmarkbage committed Feb 24, 2022
1 parent f62de1d commit e915eee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('ReactDOMServerIntegrationTextarea', () => {
expect(e.value).toBe('foo');
});

itRenders('a textarea with a value of undefined', async render => {
const e = await render(<textarea value={undefined} />);
expect(e.getAttribute('value')).toBe(null);
expect(e.value).toBe('');
});

itRenders('a textarea with a value and readOnly', async render => {
const e = await render(<textarea value="foo" readOnly={true} />);
// textarea DOM elements don't have a value **attribute**, the text is
Expand Down
12 changes: 11 additions & 1 deletion packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,17 @@ function pushStartTextArea(
target.push(leadingNewline);
}

return value;
// ToString and push directly instead of recurse over children.
// We don't really support complex children in the value anyway.
// This also currently avoids a trailing comment node which breaks textarea.
if (value !== null) {
if (__DEV__) {
checkAttributeStringCoercion(value, 'value');
}
target.push(stringToChunk(encodeHTMLTextNode('' + value)));
}

return null;
}

function pushSelfClosing(
Expand Down

0 comments on commit e915eee

Please sign in to comment.