UNPKG

5.62 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23import { EventType, SpecificEventListener } from '@material/base/types';
24import { MDCTextFieldNativeInputElement } from './types';
25/**
26 * Defines the shape of the adapter expected by the foundation.
27 * Implement this adapter for your framework of choice to delegate updates to
28 * the component in your framework of choice. See architecture documentation
29 * for more details.
30 * https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
31 */
32export interface MDCTextFieldAdapter extends MDCTextFieldRootAdapter, MDCTextFieldInputAdapter, MDCTextFieldLabelAdapter, MDCTextFieldLineRippleAdapter, MDCTextFieldOutlineAdapter {
33}
34export interface MDCTextFieldRootAdapter {
35 /**
36 * Adds a class to the root Element.
37 */
38 addClass(className: string): void;
39 /**
40 * Removes a class from the root Element.
41 */
42 removeClass(className: string): void;
43 /**
44 * @return true if the root element contains the given class name.
45 */
46 hasClass(className: string): boolean;
47 /**
48 * Registers an event handler on the root element for a given event.
49 */
50 registerTextFieldInteractionHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
51 /**
52 * Deregisters an event handler on the root element for a given event.
53 */
54 deregisterTextFieldInteractionHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
55 /**
56 * Registers a validation attribute change listener on the input element.
57 * Handler accepts list of attribute names.
58 */
59 registerValidationAttributeChangeHandler(handler: (attributeNames: string[]) => void): MutationObserver;
60 /**
61 * Disconnects a validation attribute observer on the input element.
62 */
63 deregisterValidationAttributeChangeHandler(observer: MutationObserver): void;
64}
65export interface MDCTextFieldInputAdapter {
66 /**
67 * @return The native `<input>` element, or an object with the same shape.
68 * Note that this method can return null, which the foundation will handle gracefully.
69 */
70 getNativeInput(): MDCTextFieldNativeInputElement | null;
71 /**
72 * Sets the specified attribute to the specified value on the input element.
73 */
74 setInputAttr(attr: string, value: string): void;
75 /**
76 * Removes the specified attribute from the input element.
77 */
78 removeInputAttr(attr: string): void;
79 /**
80 * @return true if the textfield is focused. We achieve this via `document.activeElement === this.root`.
81 */
82 isFocused(): boolean;
83 /**
84 * Registers an event listener on the native input element for a given event.
85 */
86 registerInputInteractionHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
87 /**
88 * Deregisters an event listener on the native input element for a given event.
89 */
90 deregisterInputInteractionHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
91}
92export interface MDCTextFieldLabelAdapter {
93 /**
94 * Only implement if label exists.
95 * Shakes label if shouldShake is true.
96 */
97 shakeLabel(shouldShake: boolean): void;
98 /**
99 * Only implement if label exists.
100 * Floats the label above the input element if shouldFloat is true.
101 */
102 floatLabel(shouldFloat: boolean): void;
103 /**
104 * @return true if label element exists, false if it doesn't.
105 */
106 hasLabel(): boolean;
107 /**
108 * Only implement if label exists.
109 * @return width of label in pixels.
110 */
111 getLabelWidth(): number;
112 /**
113 * Only implement if label exists.
114 * Styles the label as required.
115 */
116 setLabelRequired(isRequired: boolean): void;
117}
118export interface MDCTextFieldLineRippleAdapter {
119 /**
120 * Activates the line ripple.
121 */
122 activateLineRipple(): void;
123 /**
124 * Deactivates the line ripple.
125 */
126 deactivateLineRipple(): void;
127 /**
128 * Sets the transform origin of the line ripple.
129 */
130 setLineRippleTransformOrigin(normalizedX: number): void;
131}
132export interface MDCTextFieldOutlineAdapter {
133 /**
134 * @return true if outline element exists, false if it doesn't.
135 */
136 hasOutline(): boolean;
137 /**
138 * Only implement if outline element exists.
139 */
140 notchOutline(labelWidth: number): void;
141 /**
142 * Only implement if outline element exists.
143 * Closes notch in outline element.
144 */
145 closeOutline(): void;
146}