@material/select
Version:
The Material Components web select (text field drop-down) component
175 lines • 7.51 kB
JavaScript
/**
* @license
* Copyright 2018 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 MDCSelectHelperTextFoundation = /** @class */ (function (_super) {
__extends(MDCSelectHelperTextFoundation, _super);
function MDCSelectHelperTextFoundation(adapter) {
return _super.call(this, __assign(__assign({}, MDCSelectHelperTextFoundation.defaultAdapter), adapter)) || this;
}
Object.defineProperty(MDCSelectHelperTextFoundation, "cssClasses", {
get: function () {
return cssClasses;
},
enumerable: false,
configurable: true
});
Object.defineProperty(MDCSelectHelperTextFoundation, "strings", {
get: function () {
return strings;
},
enumerable: false,
configurable: true
});
Object.defineProperty(MDCSelectHelperTextFoundation, "defaultAdapter", {
/**
* See {@link MDCSelectHelperTextAdapter} 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; },
setAttr: function () { return undefined; },
getAttr: function () { return null; },
removeAttr: function () { return undefined; },
setContent: function () { return undefined; },
};
// tslint:enable:object-literal-sort-keys
},
enumerable: false,
configurable: true
});
/**
* @return The ID of the helper text, or null if none is set.
*/
MDCSelectHelperTextFoundation.prototype.getId = function () {
return this.adapter.getAttr('id');
};
/**
* @return Whether the helper text is currently visible.
*/
MDCSelectHelperTextFoundation.prototype.isVisible = function () {
return this.adapter.getAttr(strings.ARIA_HIDDEN) !== 'true';
};
/**
* Sets the content of the helper text field.
*/
MDCSelectHelperTextFoundation.prototype.setContent = function (content) {
this.adapter.setContent(content);
};
/**
* Sets the helper text to act as a validation message.
* By default, validation messages are hidden when the select is valid and
* visible when the select is invalid.
*
* @param isValidation True to make the helper text act as an error validation
* message.
*/
MDCSelectHelperTextFoundation.prototype.setValidation = function (isValidation) {
if (isValidation) {
this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
}
else {
this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
}
};
/**
* Sets the persistency of the validation helper text.
* This keeps the validation message visible even if the select is valid,
* though it will be displayed in the normal (grey) color.
*/
MDCSelectHelperTextFoundation.prototype.setValidationMsgPersistent = function (isPersistent) {
if (isPersistent) {
this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
}
else {
this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
}
};
/**
* @return Whether the helper text acts as a validation message.
* By default, validation messages are hidden when the select is valid and
* visible when the select is invalid.
*/
MDCSelectHelperTextFoundation.prototype.getIsValidation = function () {
return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
};
/**
* @return Whether the validation helper text persists even if the input is
* valid. If it is, it will be displayed in the normal (grey) color.
*/
MDCSelectHelperTextFoundation.prototype.getIsValidationMsgPersistent = function () {
return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
};
/**
* When acting as a validation message, shows/hides the helper text and
* triggers alerts as necessary based on the select's validity.
*/
MDCSelectHelperTextFoundation.prototype.setValidity = function (selectIsValid) {
var isValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
if (!isValidationMsg) {
// Non-validating helper-text is always displayed and does not participate
// in validation logic.
return;
}
var isPersistentValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
// Validating helper text is displayed if select is invalid, unless it is
// set as persistent, in which case it always displays.
var msgShouldDisplay = !selectIsValid || isPersistentValidationMsg;
if (msgShouldDisplay) {
this.showToScreenReader();
// In addition to displaying, also trigger an alert if the select
// has become invalid.
if (!selectIsValid) {
this.adapter.setAttr(strings.ROLE, 'alert');
}
else {
this.adapter.removeAttr(strings.ROLE);
}
return;
}
// Hide everything.
this.adapter.removeAttr(strings.ROLE);
this.hide();
};
/**
* Makes the helper text visible to screen readers.
*/
MDCSelectHelperTextFoundation.prototype.showToScreenReader = function () {
this.adapter.removeAttr(strings.ARIA_HIDDEN);
};
/**
* Hides the help text from screen readers.
*/
MDCSelectHelperTextFoundation.prototype.hide = function () {
this.adapter.setAttr(strings.ARIA_HIDDEN, 'true');
};
return MDCSelectHelperTextFoundation;
}(MDCFoundation));
export { MDCSelectHelperTextFoundation };
// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
export default MDCSelectHelperTextFoundation;
//# sourceMappingURL=foundation.js.map