|
1 | 1 | # Field
|
2 | 2 |
|
3 |
| -Field is a component to aid in creating form components. It provides a label, hint sections for things like help text, a control block, and an error section for rendering errors. It allows you to provide your own custom controls with a consistent form-element shell. |
| 3 | +Field is a component to aid in creating form components. It provides a label, hint sections for things like help text, a control block, and an error section for rendering errors. It allows users to provide custom controls with a consistent form-element shell and is not opinionated on what underlying control element is used. |
4 | 4 |
|
5 |
| -## Yielded Components |
| 5 | +## Yielded Items |
6 | 6 |
|
| 7 | +- `id`: A unique ID to provide to the control element `id` attribute and the Label component `for` attribute. |
| 8 | +- `hintId`: A unique ID to provide to the control element `aria-describedby` attribute and the Hint component `id` attribute. |
| 9 | +- `errorId`: A unique ID to provide to the control element `aria-describedby` or `aria-errormessage` attribute and the Error component `id` attribute. |
7 | 10 | - `Label`: Renders a `<label>` element. Form element label text is normally rendered here.
|
8 | 11 | - `Hint`: Renders a `<div>` element. Help text or supplemental information is normally rendered here.
|
9 |
| -- `Control`: Renders a control of your choosing, for example, you can provide `<input>`, `<textarea>`, etc. |
| 12 | +- `Control`: A control block where a form element is rendered, for example, an `<input>`, `<textarea>`, etc. |
10 | 13 | - `Error`: Renders a `<div>` element. Error information is normally rendered here.
|
11 | 14 |
|
| 15 | +## Accessibility |
| 16 | + |
| 17 | +The Field component does not handle accessibility automatically for the label, hint, and error sections of the component; however, it does provide identifiers to assist here. Each code example on this page also shows how to take advantage of these IDs. |
| 18 | + |
| 19 | +- [for](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/for) |
| 20 | +- [aria-describedby](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) |
| 21 | +- [aria-errormessage](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-errormessage) |
| 22 | +- [aria-invalid](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-invalid) |
| 23 | + |
12 | 24 | ## Optionally Rendering Components
|
13 | 25 |
|
14 |
| -The yielded components from Field can be optionally rendered by using the `{{if}}` helper. For example, control when helper text or errors may be displayed. |
| 26 | +The yielded components from Field can be optionally rendered by using the `{{#if}}` helper. The below example optionally renders the Hint and Error based on component arguments. |
15 | 27 |
|
16 | 28 | ```hbs
|
17 | 29 | <Form::Field as |field|>
|
18 |
| - <field.Label>{{@label}}</field.Label> |
| 30 | + <field.Label for={{field.id}}>{{@label}}</field.Label> |
19 | 31 |
|
20 | 32 | {{#if @helperText}}
|
21 |
| - <field.Hint>{{@helperText}}</field.Hint> |
| 33 | + <field.Hint id={{field.hintId}}>{{@helperText}}</field.Hint> |
22 | 34 | {{/if}}
|
23 | 35 |
|
24 | 36 | <field.Control>
|
25 |
| - <input class='border-critical bg-blue' ...attributes /> |
| 37 | + <input id={{field.id}} aria-describedby={{field.hintId}} aria-invalid={{if @error "true"}} aria-errormessage={{if @error field.errorId}}class='border-critical bg-blue' ...attributes /> |
26 | 38 | </field.Control>
|
27 | 39 |
|
28 | 40 | {{#if @error}}
|
29 |
| - <field.Error>{{@error}}</field.Error> |
| 41 | + <field.Error id={{field.errorId}}>{{@error}}</field.Error> |
30 | 42 | {{/if}}
|
31 | 43 | </Form::Field>
|
32 | 44 | ```
|
33 | 45 |
|
34 | 46 | ### Attributes and Modifiers
|
35 | 47 |
|
36 |
| -We have `...attributes` added to each sub component of the Field, so HTML attributes and Ember modifiers can be added. |
| 48 | +Attributes are spread to each sub component of the Field via `...attributes`, so HTML attributes and Ember modifiers can be added. |
37 | 49 |
|
38 | 50 | ```hbs
|
39 | 51 | <Form::Field as |field|>
|
40 |
| - <field.Label class='mt-3' {{on 'hover' this.onLabelHover}}>First name</field.Label> |
| 52 | + <field.Label |
| 53 | + for={{field.id}} |
| 54 | + class='mt-3' |
| 55 | + data-test-label |
| 56 | + {{on 'hover' this.onLabelHover}} |
| 57 | + >First name</field.Label> |
41 | 58 | {{! other components below ...}}
|
42 | 59 | </Form::Field>
|
43 | 60 | ```
|
44 | 61 |
|
45 | 62 | ### Ordering of Components
|
46 | 63 |
|
47 |
| -Ordering of the elements can be changed by adjusting the children order. For example, if you'd prefer putting help text below the control: |
| 64 | +Ordering of the elements can be changed by adjusting the order of the children. For example, if it is preferred to put the hint block underneath the control. |
48 | 65 |
|
49 | 66 | ```hbs
|
50 | 67 | <Form::Field as |field|>
|
51 |
| - <field.Label>First name</field.Label> |
| 68 | + <field.Label for={{field.id}}>First name</field.Label> |
52 | 69 | <field.Control>
|
53 |
| - <input ...attributes /> |
| 70 | + <input id={{field.id}} aria-describedby={{field.hintId}} ...attributes /> |
54 | 71 | </field.Control>
|
55 |
| - <field.Hint>Hint text below the input</field.Hint> |
| 72 | + <field.Hint id={{field.hintId}}>Hint text below the input</field.Hint> |
56 | 73 | </Form::Field>
|
57 | 74 | ```
|
0 commit comments