|
1 | 1 | # MDC Form Field |
2 | 2 |
|
3 | 3 | MDC Form Field provides an `mdc-form-field` helper class for easily making theme-aware, RTL-aware |
4 | | -form field + label combos. |
| 4 | +form field + label combos. It also provides an `MDCFormField` class for easily making input ripples |
| 5 | +respond to label events. |
5 | 6 |
|
6 | 7 | ## Installation |
7 | 8 |
|
8 | 9 | ``` |
9 | 10 | npm install --save @material/form-field |
10 | 11 | ``` |
11 | 12 |
|
12 | | -## Usage |
| 13 | +## CSS Usage |
13 | 14 |
|
14 | 15 | The `mdc-form-field` class can be used as a wrapper element with any `input` + `label` combo: |
15 | 16 |
|
@@ -71,3 +72,81 @@ checkbox: |
71 | 72 |
|
72 | 73 | `mdc-form-field` is dark theme aware, and will change the text color to the "primary on dark" text |
73 | 74 | color when used within a dark theme. |
| 75 | + |
| 76 | + |
| 77 | +## JS Usage |
| 78 | + |
| 79 | +### Including in code |
| 80 | + |
| 81 | +#### ES2015 |
| 82 | + |
| 83 | +```javascript |
| 84 | +import {MDCFormField, MDCFormFieldFoundation} from 'mdc-form-field'; |
| 85 | +``` |
| 86 | + |
| 87 | +#### CommonJS |
| 88 | + |
| 89 | +```javascript |
| 90 | +const mdcFormField = require('mdc-form-field'); |
| 91 | +const MDCFormField = mdcFormField.MDCFormField; |
| 92 | +const MDCFormFieldFoundation = mdcFormField.MDCFormFieldFoundation; |
| 93 | +``` |
| 94 | + |
| 95 | +#### AMD |
| 96 | + |
| 97 | +```javascript |
| 98 | +require(['path/to/mdc-form-field'], mdcFormField => { |
| 99 | + const MDCFormField = mdcFormField.MDCFormField; |
| 100 | + const MDCFormFieldFoundation = mdcFormField.MDCFormFieldFoundation; |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +#### Global |
| 105 | + |
| 106 | +```javascript |
| 107 | +const MDCFormField = mdc.radio.MDCFormField; |
| 108 | +const MDCFormFieldFoundation = mdc.radio.MDCFormFieldFoundation; |
| 109 | +``` |
| 110 | + |
| 111 | +### Instantiation |
| 112 | + |
| 113 | +```javascript |
| 114 | +import {MDCFormField} from 'mdc-form-field'; |
| 115 | + |
| 116 | +const formField = new MDCFormField(document.querySelector('.mdc-form-field')); |
| 117 | +``` |
| 118 | + |
| 119 | +### MDCFormField API |
| 120 | + |
| 121 | +The `MDCFormField` functionality is exposed through a single accessor method. |
| 122 | + |
| 123 | +#### MDCFormField.input |
| 124 | + |
| 125 | +Read-write property that works with an instance of an MDC-Web input element. |
| 126 | + |
| 127 | +In order for the label ripple integration to work correctly, this property needs to be set to a |
| 128 | +valid instance of an MDC-Web input element which exposes a `ripple` getter. |
| 129 | + |
| 130 | +```javascript |
| 131 | +const formField = new MDCFormField(document.querySelector('.mdc-form-field')); |
| 132 | +const radio = new MDCRadio(document.querySelector('.mdc-radio')); |
| 133 | + |
| 134 | +formField.input = radio; |
| 135 | +``` |
| 136 | + |
| 137 | +No action is taken if the `input` property is not set or the input instance doesn't expose a |
| 138 | +`ripple` getter. |
| 139 | + |
| 140 | + |
| 141 | +### Adapter |
| 142 | + |
| 143 | +The adapter for `MDCFormField` is extremely simple, providing only methods for adding and |
| 144 | +removing event listeners from the label, as well as methods for activating and deactivating |
| 145 | +the input ripple. |
| 146 | + |
| 147 | +| Method Signature | Description | |
| 148 | +| --- | --- | |
| 149 | +| `registerInteractionHandler(type: string, handler: EventListener) => void` | Adds an event listener `handler` for event type `type` to the label. | |
| 150 | +| `deregisterInteractionHandler(type: string, handler: EventListener) => void` | Removes an event listener `handler` for event type `type` to the label. | |
| 151 | +| `activateInputRipple() => void` | Activates the ripple on the input element. Should call `activate` on the input element's `ripple` property. | |
| 152 | +| `deactivateInputRipple() => void` | Deactivates the ripple on the input element. Should call `deactivate` on the input element's `ripple` property. | |
0 commit comments