Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] TextArea: fix style object regression #3440

Merged
merged 2 commits into from
Mar 22, 2019
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
28 changes: 18 additions & 10 deletions packages/core/src/components/forms/textArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,29 @@ export class TextArea extends React.PureComponent<ITextAreaProps, ITextAreaState
className,
);

const styleProps =
this.props.growVertically && this.state.height != null
? {
style: {
height: `${this.state.height}px`,
},
}
: {};
// add explicit height style while preserving user-supplied styles if they exist
let { style = {} } = htmlProps;
if (growVertically && this.state.height != null) {
// this style object becomes non-extensible when mounted (at least in the enzyme renderer),
// so we make a new one to add a property
style = {
...style,
height: `${this.state.height}px`,
};
}

return (
<textarea {...htmlProps} {...styleProps} className={rootClasses} ref={inputRef} onChange={this.onChange} />
<textarea
{...htmlProps}
className={rootClasses}
onChange={this.handleChange}
ref={inputRef}
style={style}
/>
);
}

private onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
private handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
if (this.props.growVertically) {
this.setState({
height: e.target.scrollHeight,
Expand Down
15 changes: 10 additions & 5 deletions packages/core/test/forms/textAreaTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ describe("<TextArea>", () => {
const wrapper = mount(<TextArea growVertically={true} />);
const textarea = wrapper.find("textarea");

textarea.simulate("change", {
target: {
scrollHeight: 500,
},
});
textarea.simulate("change", { target: { scrollHeight: 500 } });

assert.equal((textarea.getDOMNode() as HTMLElement).style.height, "500px");
});
Expand All @@ -36,4 +32,13 @@ describe("<TextArea>", () => {

assert.equal((textarea.getDOMNode() as HTMLElement).style.height, "");
});

it("doesn't clobber user-supplied styles", () => {
const wrapper = mount(<TextArea growVertically={true} style={{ marginTop: 10 }} />);
const textarea = wrapper.find("textarea");

textarea.simulate("change", { target: { scrollHeight: 500 } });

assert.equal((textarea.getDOMNode() as HTMLElement).style.marginTop, "10px");
});
});