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] fix(TextArea): resize vertically in controlled mode #5975

Conversation

AaronJRubin
Copy link
Contributor

@AaronJRubin AaronJRubin commented Feb 27, 2023

Fixes #5828

Checklist

  • Includes tests
  • Update documentation

Changes proposed in this pull request:

Resize text area when its value changes due to a new string being passed in through props

Reviewers should focus on:

How we can test this - at first, I tried adding a test like the below:

    it("resizes when props change if growVertically is true", () => {
        const initialText = "A";
        const longText = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
        const wrapper = mount(<TextArea growVertically={true} value={initialText} style={{ marginTop: 10 }} />);
        const textarea = wrapper.find("textarea");
        const initialHeight = (textarea.getDOMNode() as HTMLElement).style.height;
        wrapper.setProps({ value: longText });
        const newHeight = (textarea.getDOMNode() as HTMLElement).style.height;
        assert.notEqual(newHeight, initialHeight);
    });

But both newHeight and initialHeight are always 0, so this test fails. The existing test for the resize behavior (can fit large initial content) appears to be spurious; the comparison at https://github.com/palantir/blueprint/blob/develop/packages/core/test/forms/textAreaTests.tsx#L67 is also just comparing the strings 0px and 0px.

I manually tested this by building another app against the locally-compiled blueprint-core package and observing that the buggy behavior was fixed, but I'm not sure if that meets our standards for testing.

@adidahiya
Copy link
Contributor

@AaronJRubin I think the tests are not working because we're not mounting into an attachTo container element. I'll poke at the test suite in this PR

@adidahiya
Copy link
Contributor

@AaronJRubin I wasn't able to get the tests working, but I've added a little example to the docs which demonstrates controlled usage w/ growVertically. it seems to work with your change. I went ahead and implemented some code style fixes / improvements on top of your commit.

2023-02-27 11 46 08

Note that it does not shrink vertically automatically, but this matches the existing behavior with the uncontrolled API.

@adidahiya adidahiya changed the title Resize text area when value passed via props changes [core] fix(TextArea): resize vertically in controlled mode Feb 27, 2023
Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self-review

packages/core/src/components/forms/textArea.tsx Outdated Show resolved Hide resolved
@@ -75,22 +79,29 @@ export class TextArea extends AbstractPureComponent2<TextAreaProps, ITextAreaSta
this.props.inputRef,
);

public componentDidMount() {
if (this.props.growVertically && this.textareaElement !== null) {
// HACKHACK: this should probably be done in getSnapshotBeforeUpdate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't really help us. removed this comment

public componentDidMount() {
if (this.props.growVertically && this.textareaElement !== null) {
// HACKHACK: this should probably be done in getSnapshotBeforeUpdate
/* eslint-disable-next-line react/no-did-mount-set-state */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a significantly better way to do this, and I think the potential for layout thrashing is minimal. removed this comment.

packages/core/src/components/forms/textArea.tsx Outdated Show resolved Hide resolved
if (prevProps.inputRef !== this.props.inputRef) {
setRef(prevProps.inputRef, null);
this.handleRef = refHandler(this, "textareaElement", this.props.inputRef);
setRef(this.props.inputRef, this.textareaElement);
}

if (prevProps.value !== this.props.value || prevProps.style !== this.props.style) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that props.style changes can cause layout changes which affect scrollHeight

@adidahiya adidahiya merged commit 3c58026 into palantir:develop Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TextArea Does Not Resize Vertically When Props Change After Initial Mount
2 participants