UNPKG

@material/switch

Version:
100 lines 4.32 kB
/** * @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 MDCSwitchFoundation = /** @class */ (function (_super) { __extends(MDCSwitchFoundation, _super); function MDCSwitchFoundation(adapter) { return _super.call(this, __assign(__assign({}, MDCSwitchFoundation.defaultAdapter), adapter)) || this; } Object.defineProperty(MDCSwitchFoundation, "strings", { /** The string constants used by the switch. */ get: function () { return strings; }, enumerable: false, configurable: true }); Object.defineProperty(MDCSwitchFoundation, "cssClasses", { /** The CSS classes used by the switch. */ get: function () { return cssClasses; }, enumerable: false, configurable: true }); Object.defineProperty(MDCSwitchFoundation, "defaultAdapter", { /** The default Adapter for the switch. */ get: function () { return { addClass: function () { return undefined; }, removeClass: function () { return undefined; }, setNativeControlChecked: function () { return undefined; }, setNativeControlDisabled: function () { return undefined; }, setNativeControlAttr: function () { return undefined; }, }; }, enumerable: false, configurable: true }); /** Sets the checked state of the switch. */ MDCSwitchFoundation.prototype.setChecked = function (checked) { this.adapter.setNativeControlChecked(checked); this.updateAriaChecked(checked); this.updateCheckedStyling(checked); }; /** Sets the disabled state of the switch. */ MDCSwitchFoundation.prototype.setDisabled = function (disabled) { this.adapter.setNativeControlDisabled(disabled); if (disabled) { this.adapter.addClass(cssClasses.DISABLED); } else { this.adapter.removeClass(cssClasses.DISABLED); } }; /** Handles the change event for the switch native control. */ MDCSwitchFoundation.prototype.handleChange = function (evt) { var nativeControl = evt.target; this.updateAriaChecked(nativeControl.checked); this.updateCheckedStyling(nativeControl.checked); }; /** Updates the styling of the switch based on its checked state. */ MDCSwitchFoundation.prototype.updateCheckedStyling = function (checked) { if (checked) { this.adapter.addClass(cssClasses.CHECKED); } else { this.adapter.removeClass(cssClasses.CHECKED); } }; MDCSwitchFoundation.prototype.updateAriaChecked = function (checked) { this.adapter.setNativeControlAttr(strings.ARIA_CHECKED_ATTR, "" + !!checked); }; return MDCSwitchFoundation; }(MDCFoundation)); export { MDCSwitchFoundation }; // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier. export default MDCSwitchFoundation; //# sourceMappingURL=foundation.js.map