component.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license
  3. * Copyright 2016 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. */
  23. import { __assign, __extends } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { applyPassive } from '@material/dom/events';
  26. import { MDCRipple } from '@material/ripple/component';
  27. import { MDCRippleFoundation } from '@material/ripple/foundation';
  28. import { MDCRadioFoundation } from './foundation';
  29. /** MDC Radio */
  30. var MDCRadio = /** @class */ (function (_super) {
  31. __extends(MDCRadio, _super);
  32. function MDCRadio() {
  33. var _this = _super !== null && _super.apply(this, arguments) || this;
  34. _this.rippleSurface = _this.createRipple();
  35. return _this;
  36. }
  37. MDCRadio.attachTo = function (root) {
  38. return new MDCRadio(root);
  39. };
  40. Object.defineProperty(MDCRadio.prototype, "checked", {
  41. get: function () {
  42. return this.nativeControl.checked;
  43. },
  44. set: function (checked) {
  45. this.nativeControl.checked = checked;
  46. },
  47. enumerable: false,
  48. configurable: true
  49. });
  50. Object.defineProperty(MDCRadio.prototype, "disabled", {
  51. get: function () {
  52. return this.nativeControl.disabled;
  53. },
  54. set: function (disabled) {
  55. this.foundation.setDisabled(disabled);
  56. },
  57. enumerable: false,
  58. configurable: true
  59. });
  60. Object.defineProperty(MDCRadio.prototype, "value", {
  61. get: function () {
  62. return this.nativeControl.value;
  63. },
  64. set: function (value) {
  65. this.nativeControl.value = value;
  66. },
  67. enumerable: false,
  68. configurable: true
  69. });
  70. Object.defineProperty(MDCRadio.prototype, "ripple", {
  71. get: function () {
  72. return this.rippleSurface;
  73. },
  74. enumerable: false,
  75. configurable: true
  76. });
  77. MDCRadio.prototype.destroy = function () {
  78. this.rippleSurface.destroy();
  79. _super.prototype.destroy.call(this);
  80. };
  81. MDCRadio.prototype.getDefaultFoundation = function () {
  82. var _this = this;
  83. // DO NOT INLINE this variable. For backward compatibility, foundations take
  84. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  85. // methods, we need a separate, strongly typed adapter variable.
  86. var adapter = {
  87. addClass: function (className) {
  88. _this.root.classList.add(className);
  89. },
  90. removeClass: function (className) {
  91. _this.root.classList.remove(className);
  92. },
  93. setNativeControlDisabled: function (disabled) { return _this.nativeControl.disabled =
  94. disabled; },
  95. };
  96. return new MDCRadioFoundation(adapter);
  97. };
  98. MDCRadio.prototype.createRipple = function () {
  99. var _this = this;
  100. // DO NOT INLINE this variable. For backward compatibility, foundations take
  101. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  102. // methods, we need a separate, strongly typed adapter variable.
  103. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  104. var adapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { registerInteractionHandler: function (evtType, handler) {
  105. _this.nativeControl.addEventListener(evtType, handler, applyPassive());
  106. }, deregisterInteractionHandler: function (evtType, handler) {
  107. _this.nativeControl.removeEventListener(evtType, handler, applyPassive());
  108. },
  109. // Radio buttons technically go "active" whenever there is *any* keyboard
  110. // interaction. This is not the UI we desire.
  111. isSurfaceActive: function () { return false; }, isUnbounded: function () { return true; } });
  112. // tslint:enable:object-literal-sort-keys
  113. return new MDCRipple(this.root, new MDCRippleFoundation(adapter));
  114. };
  115. Object.defineProperty(MDCRadio.prototype, "nativeControl", {
  116. get: function () {
  117. var NATIVE_CONTROL_SELECTOR = MDCRadioFoundation.strings.NATIVE_CONTROL_SELECTOR;
  118. var el = this.root.querySelector(NATIVE_CONTROL_SELECTOR);
  119. if (!el) {
  120. throw new Error("Radio component requires a " + NATIVE_CONTROL_SELECTOR + " element");
  121. }
  122. return el;
  123. },
  124. enumerable: false,
  125. configurable: true
  126. });
  127. return MDCRadio;
  128. }(MDCComponent));
  129. export { MDCRadio };
  130. //# sourceMappingURL=component.js.map