radio-button.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = Object.setPrototypeOf ||
  3. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  5. return function (d, b) {
  6. extendStatics(d, b);
  7. function __() { this.constructor = d; }
  8. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  9. };
  10. })();
  11. (function (factory) {
  12. if (typeof module === "object" && typeof module.exports === "object") {
  13. var v = factory(require, exports);
  14. if (v !== undefined) module.exports = v;
  15. }
  16. else if (typeof define === "function" && define.amd) {
  17. define(["require", "exports", "@angular/core", "../../config/config", "../../util/form", "../ion", "../../util/util", "../item/item", "./radio-group"], factory);
  18. }
  19. })(function (require, exports) {
  20. "use strict";
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. var core_1 = require("@angular/core");
  23. var config_1 = require("../../config/config");
  24. var form_1 = require("../../util/form");
  25. var ion_1 = require("../ion");
  26. var util_1 = require("../../util/util");
  27. var item_1 = require("../item/item");
  28. var radio_group_1 = require("./radio-group");
  29. /**
  30. * @description
  31. * A radio button is a button that can be either checked or unchecked. A user can tap
  32. * the button to check or uncheck it. It can also be checked from the template using
  33. * the `checked` property.
  34. *
  35. * Use an element with a `radio-group` attribute to group a set of radio buttons. When
  36. * radio buttons are inside a [radio group](../RadioGroup), exactly one radio button
  37. * in the group can be checked at any time. If a radio button is not placed in a group,
  38. * they will all have the ability to be checked at the same time.
  39. *
  40. * See the [Angular Forms Docs](https://angular.io/docs/ts/latest/guide/forms.html) for
  41. * more information on forms and input.
  42. *
  43. * @usage
  44. * ```html
  45. * <ion-list radio-group [(ngModel)]="relationship">
  46. * <ion-item>
  47. * <ion-label>Friends</ion-label>
  48. * <ion-radio value="friends" checked></ion-radio>
  49. * </ion-item>
  50. * <ion-item>
  51. * <ion-label>Family</ion-label>
  52. * <ion-radio value="family"></ion-radio>
  53. * </ion-item>
  54. * <ion-item>
  55. * <ion-label>Enemies</ion-label>
  56. * <ion-radio value="enemies" [disabled]="isDisabled"></ion-radio>
  57. * </ion-item>
  58. * </ion-list>
  59. * ```
  60. * @demo /docs/demos/src/radio/
  61. * @see {@link /docs/components#radio Radio Component Docs}
  62. * @see {@link ../RadioGroup RadioGroup API Docs}
  63. */
  64. var RadioButton = (function (_super) {
  65. __extends(RadioButton, _super);
  66. function RadioButton(_form, config, elementRef, renderer, _item, _group) {
  67. var _this = _super.call(this, config, elementRef, renderer, 'radio') || this;
  68. _this._form = _form;
  69. _this._item = _item;
  70. _this._group = _group;
  71. /**
  72. * @internal
  73. */
  74. _this._checked = false;
  75. /**
  76. * @internal
  77. */
  78. _this._disabled = false;
  79. /**
  80. * @internal
  81. */
  82. _this._value = null;
  83. /**
  84. * @output {any} Emitted when the radio button is selected.
  85. */
  86. _this.ionSelect = new core_1.EventEmitter();
  87. _form.register(_this);
  88. if (_group) {
  89. // register with the radiogroup
  90. _this.id = 'rb-' + _group.add(_this);
  91. }
  92. if (_item) {
  93. // register the input inside of the item
  94. // reset to the item's id instead of the radiogroup id
  95. _this.id = 'rb-' + _item.registerInput('radio');
  96. _this._labelId = 'lbl-' + _item.id;
  97. _this._item.setElementClass('item-radio', true);
  98. }
  99. return _this;
  100. }
  101. Object.defineProperty(RadioButton.prototype, "color", {
  102. /**
  103. * @input {string} The color to use from your Sass `$colors` map.
  104. * Default options are: `"primary"`, `"secondary"`, `"danger"`, `"light"`, and `"dark"`.
  105. * For more information, see [Theming your App](/docs/theming/theming-your-app).
  106. */
  107. set: function (val) {
  108. this._setColor(val);
  109. if (this._item) {
  110. this._item._updateColor(val, 'item-radio');
  111. }
  112. },
  113. enumerable: true,
  114. configurable: true
  115. });
  116. Object.defineProperty(RadioButton.prototype, "value", {
  117. /**
  118. * @input {any} The value of the radio button. Defaults to the generated id.
  119. */
  120. get: function () {
  121. // if the value is not defined then use it's unique id
  122. return util_1.isBlank(this._value) ? this.id : this._value;
  123. },
  124. set: function (val) {
  125. this._value = val;
  126. },
  127. enumerable: true,
  128. configurable: true
  129. });
  130. Object.defineProperty(RadioButton.prototype, "checked", {
  131. /**
  132. * @input {boolean} If true, the element is selected, and other buttons in the group are unselected.
  133. */
  134. get: function () {
  135. return this._checked;
  136. },
  137. set: function (val) {
  138. this._checked = util_1.isTrueProperty(val);
  139. if (this._item) {
  140. this._item.setElementClass('item-radio-checked', this._checked);
  141. }
  142. },
  143. enumerable: true,
  144. configurable: true
  145. });
  146. Object.defineProperty(RadioButton.prototype, "disabled", {
  147. /**
  148. * @input {boolean} If true, the user cannot interact with this element.
  149. */
  150. get: function () {
  151. return this._disabled || (this._group != null && this._group.disabled);
  152. },
  153. set: function (val) {
  154. this._disabled = util_1.isTrueProperty(val);
  155. this._item && this._item.setElementClass('item-radio-disabled', this._disabled);
  156. },
  157. enumerable: true,
  158. configurable: true
  159. });
  160. /**
  161. * @hidden
  162. */
  163. RadioButton.prototype.initFocus = function () {
  164. this._elementRef.nativeElement.querySelector('button').focus();
  165. };
  166. /**
  167. * @internal
  168. */
  169. RadioButton.prototype._click = function (ev) {
  170. (void 0) /* console.debug */;
  171. ev.preventDefault();
  172. ev.stopPropagation();
  173. this.checked = true;
  174. this.ionSelect.emit(this.value);
  175. };
  176. /**
  177. * @internal
  178. */
  179. RadioButton.prototype.ngOnInit = function () {
  180. if (this._group && util_1.isPresent(this._group.value)) {
  181. this.checked = util_1.isCheckedProperty(this._group.value, this.value);
  182. }
  183. if (this._group && this._group.disabled) {
  184. this.disabled = this._group.disabled;
  185. }
  186. };
  187. /**
  188. * @internal
  189. */
  190. RadioButton.prototype.ngOnDestroy = function () {
  191. this._form.deregister(this);
  192. this._group && this._group.remove(this);
  193. };
  194. RadioButton.decorators = [
  195. { type: core_1.Component, args: [{
  196. selector: 'ion-radio',
  197. template: '<div class="radio-icon" [class.radio-checked]="_checked"> ' +
  198. '<div class="radio-inner"></div> ' +
  199. '</div> ' +
  200. '<button role="radio" ' +
  201. 'type="button" ' +
  202. 'ion-button="item-cover" ' +
  203. '[id]="id" ' +
  204. '[attr.aria-checked]="_checked" ' +
  205. '[attr.aria-labelledby]="_labelId" ' +
  206. '[attr.aria-disabled]="_disabled" ' +
  207. 'class="item-cover"> ' +
  208. '</button>',
  209. host: {
  210. '[class.radio-disabled]': '_disabled'
  211. },
  212. encapsulation: core_1.ViewEncapsulation.None,
  213. },] },
  214. ];
  215. /** @nocollapse */
  216. RadioButton.ctorParameters = function () { return [
  217. { type: form_1.Form, },
  218. { type: config_1.Config, },
  219. { type: core_1.ElementRef, },
  220. { type: core_1.Renderer, },
  221. { type: item_1.Item, decorators: [{ type: core_1.Optional },] },
  222. { type: radio_group_1.RadioGroup, decorators: [{ type: core_1.Optional },] },
  223. ]; };
  224. RadioButton.propDecorators = {
  225. 'color': [{ type: core_1.Input },],
  226. 'ionSelect': [{ type: core_1.Output },],
  227. 'value': [{ type: core_1.Input },],
  228. 'checked': [{ type: core_1.Input },],
  229. 'disabled': [{ type: core_1.Input },],
  230. '_click': [{ type: core_1.HostListener, args: ['click', ['$event'],] },],
  231. };
  232. return RadioButton;
  233. }(ion_1.Ion));
  234. exports.RadioButton = RadioButton;
  235. });
  236. //# sourceMappingURL=radio-button.js.map