@material/textfield
Version:
The Material Components for the web text field component
157 lines • 6.69 kB
JavaScript
/**
* @license
* Copyright 2017 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import { __assign, __extends } from "tslib";
import { MDCFoundation } from '@material/base/foundation';
import { cssClasses, strings } from './constants';
var MDCTextFieldHelperTextFoundation = /** @class */ (function (_super) {
__extends(MDCTextFieldHelperTextFoundation, _super);
function MDCTextFieldHelperTextFoundation(adapter) {
return _super.call(this, __assign(__assign({}, MDCTextFieldHelperTextFoundation.defaultAdapter), adapter)) || this;
}
Object.defineProperty(MDCTextFieldHelperTextFoundation, "cssClasses", {
get: function () {
return cssClasses;
},
enumerable: false,
configurable: true
});
Object.defineProperty(MDCTextFieldHelperTextFoundation, "strings", {
get: function () {
return strings;
},
enumerable: false,
configurable: true
});
Object.defineProperty(MDCTextFieldHelperTextFoundation, "defaultAdapter", {
/**
* See {@link MDCTextFieldHelperTextAdapter} for typing information on parameters and return types.
*/
get: function () {
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
return {
addClass: function () { return undefined; },
removeClass: function () { return undefined; },
hasClass: function () { return false; },
getAttr: function () { return null; },
setAttr: function () { return undefined; },
removeAttr: function () { return undefined; },
setContent: function () { return undefined; },
};
// tslint:enable:object-literal-sort-keys
},
enumerable: false,
configurable: true
});
MDCTextFieldHelperTextFoundation.prototype.getId = function () {
return this.adapter.getAttr('id');
};
MDCTextFieldHelperTextFoundation.prototype.isVisible = function () {
return this.adapter.getAttr(strings.ARIA_HIDDEN) !== 'true';
};
/**
* Sets the content of the helper text field.
*/
MDCTextFieldHelperTextFoundation.prototype.setContent = function (content) {
this.adapter.setContent(content);
};
MDCTextFieldHelperTextFoundation.prototype.isPersistent = function () {
return this.adapter.hasClass(cssClasses.HELPER_TEXT_PERSISTENT);
};
/**
* @param isPersistent Sets the persistency of the helper text.
*/
MDCTextFieldHelperTextFoundation.prototype.setPersistent = function (isPersistent) {
if (isPersistent) {
this.adapter.addClass(cssClasses.HELPER_TEXT_PERSISTENT);
}
else {
this.adapter.removeClass(cssClasses.HELPER_TEXT_PERSISTENT);
}
};
/**
* @return whether the helper text acts as an error validation message.
*/
MDCTextFieldHelperTextFoundation.prototype.isValidation = function () {
return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
};
/**
* @param isValidation True to make the helper text act as an error validation message.
*/
MDCTextFieldHelperTextFoundation.prototype.setValidation = function (isValidation) {
if (isValidation) {
this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
}
else {
this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
}
};
/**
* Makes the helper text visible to the screen reader.
*/
MDCTextFieldHelperTextFoundation.prototype.showToScreenReader = function () {
this.adapter.removeAttr(strings.ARIA_HIDDEN);
};
/**
* Sets the validity of the helper text based on the input validity.
*/
MDCTextFieldHelperTextFoundation.prototype.setValidity = function (inputIsValid) {
var helperTextIsPersistent = this.adapter.hasClass(cssClasses.HELPER_TEXT_PERSISTENT);
var helperTextIsValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
var validationMsgNeedsDisplay = helperTextIsValidationMsg && !inputIsValid;
if (validationMsgNeedsDisplay) {
this.showToScreenReader();
// If role is already alert, refresh it to trigger another announcement
// from screenreader.
if (this.adapter.getAttr(strings.ROLE) === 'alert') {
this.refreshAlertRole();
}
else {
this.adapter.setAttr(strings.ROLE, 'alert');
}
}
else {
this.adapter.removeAttr(strings.ROLE);
}
if (!helperTextIsPersistent && !validationMsgNeedsDisplay) {
this.hide();
}
};
/**
* Hides the help text from screen readers.
*/
MDCTextFieldHelperTextFoundation.prototype.hide = function () {
this.adapter.setAttr(strings.ARIA_HIDDEN, 'true');
};
MDCTextFieldHelperTextFoundation.prototype.refreshAlertRole = function () {
var _this = this;
this.adapter.removeAttr(strings.ROLE);
requestAnimationFrame(function () {
_this.adapter.setAttr(strings.ROLE, 'alert');
});
};
return MDCTextFieldHelperTextFoundation;
}(MDCFoundation));
export { MDCTextFieldHelperTextFoundation };
// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
export default MDCTextFieldHelperTextFoundation;
//# sourceMappingURL=foundation.js.map