Front end of the Slack clone application.

radio-button.js 6.4KB

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