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

How can I, if possible, manually triggering resize for reactive loading #354

Open
ggJSC opened this issue Feb 2, 2018 · 9 comments
Open

Comments

@ggJSC
Copy link

ggJSC commented Feb 2, 2018

I'm using React and loading information into the text area. Is there a way I can force it to resize because when my page loads info for the first time, the text area is too small.

@jackmoore
Copy link
Owner

jackmoore commented Feb 2, 2018

Not sure how you have things setup, but you'll want to exclude the textarea element from reactive updates (have shouldComponentUpdate return false) since React will replace the inline styles on the textarea element that Autosize is adding.

Here's an example component of how I think it would work:

export default class extends React.Component {
  componentDidMount() {
    autosize(this._el);
  }
  shouldComponentUpdate(nextProps, nextState) {
    return false;
  }
  render() {
    return <textarea ref={el => this._el = el} defaultValue={this.props.children || ''}/>
  }
}

@ggJSC
Copy link
Author

ggJSC commented Feb 2, 2018

My bad, I was using React as an example I wasn't sure if you would know how to do it using VueJS which is what I'm using for my project. Is there any method I can call to trigger resize? I tried using jQuerys trigger with all the event listeners you attach but nothing happened.

@jackmoore
Copy link
Owner

Call autosize.update(x), where x is a textarea element, or an array or array-like object (such as a NodeList or jQuery collection). So if you are using jQuery you can call autosize.update($('your-selector')).

@jackmoore
Copy link
Owner

I believe you can't use jQuery to trigger a native custom event (it has it's own internal events system that, and you can use trigger to fire a native event or a jQuery custom event).

@ggJSC
Copy link
Author

ggJSC commented Feb 2, 2018

Beautiful. Thank you so much.

@ChrisRobston
Copy link

I have the same issue with Vue, but can't fix it. #364

@ggJSC how did you fix it?

I tried

watch: {
    propValue: function () {
      this.textfieldValue = this.propValue
      autosize.update(this.$refs.textarea)
    }
  }

But this have 0 effect (with this function my data is setting to textarea, so I am trying to update textarea same time)

@jackmoore
Copy link
Owner

I don't have experience with Vue, but my guess would be that you need to wait for the changed value to not just change, but for the change to be reflected in the DOM. I would try using the update lifecycle method instead of watch.

@ChrisRobston
Copy link

@jackmoore thanks for tip, I remembered nextTick lol

watch: {
    propValue: function () {
      this.textfieldValue = this.propValue
      this.$nextTick(function () {
        autosize.update(this.$refs.textarea)
      })
    }
  }

@WoodrowShigeru
Copy link

WoodrowShigeru commented Jan 2, 2021

If the update() method turns out to be useless, consider reinitializing the element. jQuery example:

var $textarea = $('textarea.js-autosize');

// autosize.update($textarea);
autosize( $textarea.removeAttr('style') );

EDIT: As an addendum I'd like to add that I believe subsequent update() calls fail to work if the element or a parent has been display:none hidden during initialization.

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

No branches or pull requests

4 participants