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

Textarea value gets decoupled from what's displayed when updating @value after typing in textarea #446

Open
kmillns opened this issue Nov 25, 2024 · 0 comments

Comments

@kmillns
Copy link
Contributor

kmillns commented Nov 25, 2024

When using Form::Controls::Textarea and you type in the textarea the value and defaultValue of the underlying textarea get out of sync with each other, so further updates to @value update the defaultValue but the value isn't updated and the textarea doesn't visually update.

This appears to be because the @value set up like <textarea>{{@value}}</textarea> which is technically setting the defaultValue rather than setting the value property like <textarea value={{@value}}>

Relevant snippet from MDN

The <textarea> element specifies its content differently in HTML and JavaScript contexts:

  • In HTML, the initial content of a <textarea> is specified between its opening and closing tags, not as a value attribute.
  • In JavaScript, <textarea> elements have a value property that can be used to get or set the current content, and defaultValue to get and set its initial value (equivalent to accessing the HTML element's text content).

Example of the problem

export default class ExampleComponent extends Component {
  @tracked fooBar = 'foobar';

  @action
  updateFooBar(value, e) {
    console.log({ e, value });
    this.fooBar = value;
  }

  @action
  repeatFooBar() {
    console.log('repeat');
    this.fooBar = this.fooBar + 'fooBar';
  }
}
  <span>{{this.fooBar}}</span>
  <Form::Controls::Textarea
    @name={{this.fooBar}}
    class="font-monospace type-md"
    @label="foobar"
    @value={{this.fooBar}}
    @onChange={{this.updateFooBar}}
  />
  <button type="button" {{on "click" this.repeatFooBar}}>Repeat</button>

Steps:

  1. Click "Repeat" - this correctly appends to the textarea contents
  2. Type in the textarea - this correctly updates
  3. Click "Repeat" again - note that it no longer updates the textarea contents
  4. Inspect the textarea and check .value and .defaultValue

Screenshot 2024-11-20 at 5 11 07 PM
Screenshot 2024-11-20 at 5 11 15 PM
Screenshot 2024-11-20 at 5 11 23 PM
Screenshot 2024-11-20 at 5 11 29 PM
Screenshot 2024-11-22 at 1 49 34 PM

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

1 participant