1 |
|
2 | title: "Form Fields"
|
3 | layout: detail
|
4 | section: components
|
5 | path: /catalog/input-controls/form-fields/
|
6 | -->
|
7 |
|
8 | # Form Fields
|
9 |
|
10 | MDC Form Field aligns an MDC Web form field (for example, a checkbox) with its label and makes it RTL-aware. It also activates a [ripple](../mdc-ripple) effect upon interacting with the label.
|
11 |
|
12 | ## Installation
|
13 |
|
14 | ```
|
15 | npm install @material/form-field
|
16 | ```
|
17 |
|
18 | ## Demos
|
19 |
|
20 | <ul class="icon-list">
|
21 | <li class="icon-list-item icon-list-item--link">
|
22 | <a href="https://material-components.github.io/material-components-web-catalog/#/component/radio">Demo with radio button</a>
|
23 | </li>
|
24 | </ul>
|
25 |
|
26 | ## Basic Usage
|
27 |
|
28 | ### HTML Structure
|
29 |
|
30 | Use the `mdc-form-field` element to wrap any combination of adjacent _input_ and _label_ elements of MDC Web form controls, such as [MDC Checkbox](../mdc-checkbox) or [MDC Radio](../mdc-radio). Here's an example with MDC Checkbox:
|
31 |
|
32 | ```html
|
33 | <div class="mdc-form-field">
|
34 | <div class="mdc-checkbox">
|
35 | <input type="checkbox" id="my-checkbox" class="mdc-checkbox__native-control"/>
|
36 | <div class="mdc-checkbox__background">
|
37 | ...
|
38 | </div>
|
39 | </div>
|
40 | <label for="my-checkbox">This is my checkbox</label>
|
41 | </div>
|
42 | ```
|
43 |
|
44 | > _NOTE_: MDC Form Field is **not** intended for cases where a label and input are already handled together in a component's styles and logic. For example, [MDC Text Field](../mdc-textfield) already manages a label and input together under its own root element.
|
45 |
|
46 | ### JavaScript Instantiation
|
47 |
|
48 | If you are using MDC Form Field with an MDC Web component that has a [ripple](../mdc-ripple) effect, you can instantiate `MDCFormField` and set its [`input` property](#mdcformfield-properties-and-methods) to activate the ripple effect upon interacting with the label. Here is an example with [MDC Checkbox](../mdc-checkbox):
|
49 |
|
50 | ```js
|
51 | import {MDCFormField} from '@material/form-field';
|
52 | import {MDCCheckbox} from '@material/checkbox';
|
53 |
|
54 | const formField = new MDCFormField(document.querySelector('.mdc-form-field'));
|
55 | const checkbox = new MDCCheckbox(document.querySelector('.mdc-checkbox'));
|
56 | formField.input = checkbox;
|
57 | ```
|
58 |
|
59 | > See [Importing the JS component](../../docs/importing-js.md) for more information on how to import JavaScript.
|
60 |
|
61 | ## Variants
|
62 |
|
63 | ### Label position
|
64 |
|
65 | By default, the input will be positioned before the label. You can position the input after the label by adding the `mdc-form-field--align-end` class:
|
66 |
|
67 | ```html
|
68 | <div class="mdc-form-field mdc-form-field--align-end">
|
69 | <div class="mdc-checkbox">
|
70 | <input type="checkbox" id="my-checkbox" class="mdc-checkbox__native-control"/>
|
71 | <div class="mdc-checkbox__background">
|
72 | ...
|
73 | </div>
|
74 | </div>
|
75 | <label for="my-checkbox">This is my checkbox</label>
|
76 | </div>
|
77 | ```
|
78 |
|
79 | ### Nowrap
|
80 |
|
81 | If the label text is too long for a single line, it will wrap the text by default. You can force the text to stay on a single line and ellipse the overflow text by adding the `mdc-form-field--nowrap` class:
|
82 |
|
83 | ```html
|
84 | <div class="mdc-form-field mdc-form-field--nowrap">
|
85 | <div class="mdc-checkbox">
|
86 | <input type="checkbox" id="my-checkbox" class="mdc-checkbox__native-control"/>
|
87 | <div class="mdc-checkbox__background">
|
88 | ...
|
89 | </div>
|
90 | </div>
|
91 | <label for="my-checkbox">This some really really really long text</label>
|
92 | </div>
|
93 | ```
|
94 |
|
95 | ## `MDCFormField` Properties and Methods
|
96 |
|
97 | Property | Value Type | Description
|
98 | --- | --- | ---
|
99 | `input` | String | Gets and sets the form field input.
|
100 |
|
101 | In order for the label ripple integration to work correctly, the `input` property needs to be set to a valid instance of an MDC Web input element which exposes a `ripple` getter. No action is taken if the `input` property is not set or the input instance doesn't expose a `ripple` getter.
|
102 |
|
103 | ## Usage within Web Frameworks
|
104 |
|
105 | If you are using a JavaScript framework, such as React or Angular, you can create a Form Field for your framework. Depending on your needs, you can use the _Simple Approach: Wrapping MDC Web Vanilla Components_, or the _Advanced Approach: Using Foundations and Adapters_. Please follow the instructions [here](../../docs/integrating-into-frameworks.md).
|
106 |
|
107 | ### `MDCFormFieldAdapter`
|
108 |
|
109 | | Method Signature | Description |
|
110 | | --- | --- |
|
111 | | `registerInteractionHandler(type: string, handler: EventListener) => void` | Adds an event listener `handler` for event type `type` to the label. |
|
112 | | `deregisterInteractionHandler(type: string, handler: EventListener) => void` | Removes an event listener `handler` for event type `type` to the label. |
|
113 | | `activateInputRipple() => void` | Activates the ripple on the input element. Should call `activate` on the input element's `ripple` property. |
|
114 | | `deactivateInputRipple() => void` | Deactivates the ripple on the input element. Should call `deactivate` on the input element's `ripple` property. |
|