UNPKG

7.51 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 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 { __assign, __extends } from "tslib";
24import { MDCFoundation } from '@material/base/foundation';
25import { cssClasses, strings } from './constants';
26var MDCSelectHelperTextFoundation = /** @class */ (function (_super) {
27 __extends(MDCSelectHelperTextFoundation, _super);
28 function MDCSelectHelperTextFoundation(adapter) {
29 return _super.call(this, __assign(__assign({}, MDCSelectHelperTextFoundation.defaultAdapter), adapter)) || this;
30 }
31 Object.defineProperty(MDCSelectHelperTextFoundation, "cssClasses", {
32 get: function () {
33 return cssClasses;
34 },
35 enumerable: false,
36 configurable: true
37 });
38 Object.defineProperty(MDCSelectHelperTextFoundation, "strings", {
39 get: function () {
40 return strings;
41 },
42 enumerable: false,
43 configurable: true
44 });
45 Object.defineProperty(MDCSelectHelperTextFoundation, "defaultAdapter", {
46 /**
47 * See {@link MDCSelectHelperTextAdapter} for typing information on parameters and return types.
48 */
49 get: function () {
50 // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
51 return {
52 addClass: function () { return undefined; },
53 removeClass: function () { return undefined; },
54 hasClass: function () { return false; },
55 setAttr: function () { return undefined; },
56 getAttr: function () { return null; },
57 removeAttr: function () { return undefined; },
58 setContent: function () { return undefined; },
59 };
60 // tslint:enable:object-literal-sort-keys
61 },
62 enumerable: false,
63 configurable: true
64 });
65 /**
66 * @return The ID of the helper text, or null if none is set.
67 */
68 MDCSelectHelperTextFoundation.prototype.getId = function () {
69 return this.adapter.getAttr('id');
70 };
71 /**
72 * @return Whether the helper text is currently visible.
73 */
74 MDCSelectHelperTextFoundation.prototype.isVisible = function () {
75 return this.adapter.getAttr(strings.ARIA_HIDDEN) !== 'true';
76 };
77 /**
78 * Sets the content of the helper text field.
79 */
80 MDCSelectHelperTextFoundation.prototype.setContent = function (content) {
81 this.adapter.setContent(content);
82 };
83 /**
84 * Sets the helper text to act as a validation message.
85 * By default, validation messages are hidden when the select is valid and
86 * visible when the select is invalid.
87 *
88 * @param isValidation True to make the helper text act as an error validation
89 * message.
90 */
91 MDCSelectHelperTextFoundation.prototype.setValidation = function (isValidation) {
92 if (isValidation) {
93 this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
94 }
95 else {
96 this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
97 }
98 };
99 /**
100 * Sets the persistency of the validation helper text.
101 * This keeps the validation message visible even if the select is valid,
102 * though it will be displayed in the normal (grey) color.
103 */
104 MDCSelectHelperTextFoundation.prototype.setValidationMsgPersistent = function (isPersistent) {
105 if (isPersistent) {
106 this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
107 }
108 else {
109 this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
110 }
111 };
112 /**
113 * @return Whether the helper text acts as a validation message.
114 * By default, validation messages are hidden when the select is valid and
115 * visible when the select is invalid.
116 */
117 MDCSelectHelperTextFoundation.prototype.getIsValidation = function () {
118 return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
119 };
120 /**
121 * @return Whether the validation helper text persists even if the input is
122 * valid. If it is, it will be displayed in the normal (grey) color.
123 */
124 MDCSelectHelperTextFoundation.prototype.getIsValidationMsgPersistent = function () {
125 return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
126 };
127 /**
128 * When acting as a validation message, shows/hides the helper text and
129 * triggers alerts as necessary based on the select's validity.
130 */
131 MDCSelectHelperTextFoundation.prototype.setValidity = function (selectIsValid) {
132 var isValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
133 if (!isValidationMsg) {
134 // Non-validating helper-text is always displayed and does not participate
135 // in validation logic.
136 return;
137 }
138 var isPersistentValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
139 // Validating helper text is displayed if select is invalid, unless it is
140 // set as persistent, in which case it always displays.
141 var msgShouldDisplay = !selectIsValid || isPersistentValidationMsg;
142 if (msgShouldDisplay) {
143 this.showToScreenReader();
144 // In addition to displaying, also trigger an alert if the select
145 // has become invalid.
146 if (!selectIsValid) {
147 this.adapter.setAttr(strings.ROLE, 'alert');
148 }
149 else {
150 this.adapter.removeAttr(strings.ROLE);
151 }
152 return;
153 }
154 // Hide everything.
155 this.adapter.removeAttr(strings.ROLE);
156 this.hide();
157 };
158 /**
159 * Makes the helper text visible to screen readers.
160 */
161 MDCSelectHelperTextFoundation.prototype.showToScreenReader = function () {
162 this.adapter.removeAttr(strings.ARIA_HIDDEN);
163 };
164 /**
165 * Hides the help text from screen readers.
166 */
167 MDCSelectHelperTextFoundation.prototype.hide = function () {
168 this.adapter.setAttr(strings.ARIA_HIDDEN, 'true');
169 };
170 return MDCSelectHelperTextFoundation;
171}(MDCFoundation));
172export { MDCSelectHelperTextFoundation };
173// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
174export default MDCSelectHelperTextFoundation;
175//# sourceMappingURL=foundation.js.map
\No newline at end of file