Skip to content

Commit

Permalink
Favour light one-way example in rule docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gilest committed Nov 11, 2023
1 parent 9edf1d6 commit e5a2123
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions docs/rule/no-builtin-form-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,36 @@ This rule **forbids** the following:

## Migration

The migration path typically involves replacing the built-in form component with a native HTML element and binding an event listener to handle user input.
Many forms may be simplified by switching to a light one-way data approach.

For example – vanilla JavaScript has everything we need to handle form data, de-sync it from our source data and collect all user input in a single object.

```js
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class MyComponent extends Component {
@tracked userInput = {};

@action
handleInput(event) {
event.preventDefault();
const formData = new FormData(event.currentTarget);
this.userInput = Object.fromEntries(formData.entries());
}
}
```

```hbs
<form {{on "input" this.handleInput}}>
<label> Name
<input name="name">
</label>
</form>
```

An more granular migration path would be replacing the built-in form component with a native HTML element and binding an event listener to handle user input.

In the following example the initial value of a field is controlled by a local tracked property, which is updated by an event listener.

Expand Down Expand Up @@ -53,19 +82,17 @@ You may consider composing the [set helper](https://github.com/pzuraq/ember-set-
/>
```

Depending on your requirements, consider using form management patterns like the "light" [Form component from ember-primitives](https://ember-primitives.pages.dev/7-forms/1-intro) or the "controlled" [ember-headless-form](https://ember-headless-form.pages.dev/).

## Related Rules

* [no-mut-helper](no-mut-helper.md)

## References

* [Built-in components guides](https://guides.emberjs.com/release/components/built-in-components/)
* [Built-in `Input` component API](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Input?anchor=Input)
* [Built-in `Textarea` component API](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Textarea?anchor=Textarea)
* [Native HTML `input`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input)
* [Native HTML `textarea`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea)
* [Native HTML `FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
* [The `on` modifier](https://guides.emberjs.com/release/components/component-state-and-actions/#toc_html-modifiers-and-actions)
* [Form component – ember-primitives](https://ember-primitives.pages.dev/7-forms/1-intro)
* [ember-headless-form](https://ember-headless-form.pages.dev/)
* [Built-in components guides](https://guides.emberjs.com/release/components/built-in-components/)
* [Built-in `Input` component API](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Input?anchor=Input)
* [Built-in `Textarea` component API](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Textarea?anchor=Textarea)

0 comments on commit e5a2123

Please sign in to comment.