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

Make <TextArea growVertically={true}/> size appropriately to the initial contents #3647

Merged
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
19 changes: 17 additions & 2 deletions packages/core/src/components/forms/textArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ export interface ITextAreaState {
/* istanbul ignore next */
export class TextArea extends React.PureComponent<ITextAreaProps, ITextAreaState> {
public static displayName = `${DISPLAYNAME_PREFIX}.TextArea`;

public state: ITextAreaState = {};
private internalTextAreaRef: HTMLTextAreaElement;

public componentDidMount() {
if (this.props.growVertically) {
this.setState({
height: this.internalTextAreaRef.scrollHeight,
});
}
}
public render() {
const { className, fill, inputRef, intent, large, small, growVertically, ...htmlProps } = this.props;

Expand Down Expand Up @@ -87,7 +94,7 @@ export class TextArea extends React.PureComponent<ITextAreaProps, ITextAreaState
{...htmlProps}
className={rootClasses}
onChange={this.handleChange}
ref={inputRef}
ref={this.handleInternalRef}
style={style}
/>
);
Expand All @@ -104,4 +111,12 @@ export class TextArea extends React.PureComponent<ITextAreaProps, ITextAreaState
this.props.onChange(e);
}
};

// hold an internal ref for growVertically
private handleInternalRef = (ref: HTMLTextAreaElement | null) => {
this.internalTextAreaRef = ref;
if (this.props.inputRef != null) {
this.props.inputRef(ref);
}
};
}
14 changes: 14 additions & 0 deletions packages/core/test/forms/textAreaTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,18 @@ describe("<TextArea>", () => {

assert.equal((textarea.getDOMNode() as HTMLElement).style.marginTop, "10px");
});
it("can fit large initial content", () => {
const initialValue = `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean finibus eget enim non accumsan.
Nunc lobortis luctus magna eleifend consectetur.
Suspendisse ut semper sem, quis efficitur felis.
Praesent suscipit nunc non semper tempor.
Sed eros sapien, semper sed imperdiet sed,
dictum eget purus. Donec porta accumsan pretium.
Fusce at felis mattis, tincidunt erat non, varius erat.`;
const wrapper = mount(<TextArea growVertically={true} value={initialValue} style={{ marginTop: 10 }} />);
const textarea = wrapper.find("textarea");
const scrollHeightInPixels = `${(textarea.getDOMNode() as HTMLElement).scrollHeight}px`;
assert.equal((textarea.getDOMNode() as HTMLElement).style.height, scrollHeightInPixels);
});
});