UNPKG

6.69 kBJavaScriptView 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 { __assign, __extends } from "tslib";
24import { MDCFoundation } from '@material/base/foundation';
25import { cssClasses, strings } from './constants';
26var MDCTextFieldHelperTextFoundation = /** @class */ (function (_super) {
27 __extends(MDCTextFieldHelperTextFoundation, _super);
28 function MDCTextFieldHelperTextFoundation(adapter) {
29 return _super.call(this, __assign(__assign({}, MDCTextFieldHelperTextFoundation.defaultAdapter), adapter)) || this;
30 }
31 Object.defineProperty(MDCTextFieldHelperTextFoundation, "cssClasses", {
32 get: function () {
33 return cssClasses;
34 },
35 enumerable: false,
36 configurable: true
37 });
38 Object.defineProperty(MDCTextFieldHelperTextFoundation, "strings", {
39 get: function () {
40 return strings;
41 },
42 enumerable: false,
43 configurable: true
44 });
45 Object.defineProperty(MDCTextFieldHelperTextFoundation, "defaultAdapter", {
46 /**
47 * See {@link MDCTextFieldHelperTextAdapter} 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 getAttr: function () { return null; },
56 setAttr: function () { return undefined; },
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 MDCTextFieldHelperTextFoundation.prototype.getId = function () {
66 return this.adapter.getAttr('id');
67 };
68 MDCTextFieldHelperTextFoundation.prototype.isVisible = function () {
69 return this.adapter.getAttr(strings.ARIA_HIDDEN) !== 'true';
70 };
71 /**
72 * Sets the content of the helper text field.
73 */
74 MDCTextFieldHelperTextFoundation.prototype.setContent = function (content) {
75 this.adapter.setContent(content);
76 };
77 MDCTextFieldHelperTextFoundation.prototype.isPersistent = function () {
78 return this.adapter.hasClass(cssClasses.HELPER_TEXT_PERSISTENT);
79 };
80 /**
81 * @param isPersistent Sets the persistency of the helper text.
82 */
83 MDCTextFieldHelperTextFoundation.prototype.setPersistent = function (isPersistent) {
84 if (isPersistent) {
85 this.adapter.addClass(cssClasses.HELPER_TEXT_PERSISTENT);
86 }
87 else {
88 this.adapter.removeClass(cssClasses.HELPER_TEXT_PERSISTENT);
89 }
90 };
91 /**
92 * @return whether the helper text acts as an error validation message.
93 */
94 MDCTextFieldHelperTextFoundation.prototype.isValidation = function () {
95 return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
96 };
97 /**
98 * @param isValidation True to make the helper text act as an error validation message.
99 */
100 MDCTextFieldHelperTextFoundation.prototype.setValidation = function (isValidation) {
101 if (isValidation) {
102 this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
103 }
104 else {
105 this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
106 }
107 };
108 /**
109 * Makes the helper text visible to the screen reader.
110 */
111 MDCTextFieldHelperTextFoundation.prototype.showToScreenReader = function () {
112 this.adapter.removeAttr(strings.ARIA_HIDDEN);
113 };
114 /**
115 * Sets the validity of the helper text based on the input validity.
116 */
117 MDCTextFieldHelperTextFoundation.prototype.setValidity = function (inputIsValid) {
118 var helperTextIsPersistent = this.adapter.hasClass(cssClasses.HELPER_TEXT_PERSISTENT);
119 var helperTextIsValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
120 var validationMsgNeedsDisplay = helperTextIsValidationMsg && !inputIsValid;
121 if (validationMsgNeedsDisplay) {
122 this.showToScreenReader();
123 // If role is already alert, refresh it to trigger another announcement
124 // from screenreader.
125 if (this.adapter.getAttr(strings.ROLE) === 'alert') {
126 this.refreshAlertRole();
127 }
128 else {
129 this.adapter.setAttr(strings.ROLE, 'alert');
130 }
131 }
132 else {
133 this.adapter.removeAttr(strings.ROLE);
134 }
135 if (!helperTextIsPersistent && !validationMsgNeedsDisplay) {
136 this.hide();
137 }
138 };
139 /**
140 * Hides the help text from screen readers.
141 */
142 MDCTextFieldHelperTextFoundation.prototype.hide = function () {
143 this.adapter.setAttr(strings.ARIA_HIDDEN, 'true');
144 };
145 MDCTextFieldHelperTextFoundation.prototype.refreshAlertRole = function () {
146 var _this = this;
147 this.adapter.removeAttr(strings.ROLE);
148 requestAnimationFrame(function () {
149 _this.adapter.setAttr(strings.ROLE, 'alert');
150 });
151 };
152 return MDCTextFieldHelperTextFoundation;
153}(MDCFoundation));
154export { MDCTextFieldHelperTextFoundation };
155// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
156export default MDCTextFieldHelperTextFoundation;
157//# sourceMappingURL=foundation.js.map
\No newline at end of file