radio.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import config from '../common/config';
  8. import { SuperComponent, wxComponent } from '../common/src/index';
  9. import Props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-radio`;
  12. const iconDefault = {
  13. 'fill-circle': ['check-circle-filled', 'circle'],
  14. 'stroke-line': ['check', ''],
  15. };
  16. let Radio = class Radio extends SuperComponent {
  17. constructor() {
  18. super(...arguments);
  19. this.externalClasses = [
  20. `${prefix}-class`,
  21. `${prefix}-class-label`,
  22. `${prefix}-class-icon`,
  23. `${prefix}-class-content`,
  24. `${prefix}-class-border`,
  25. ];
  26. this.behaviors = ['wx://form-field'];
  27. this.relations = {
  28. '../radio-group/radio-group': {
  29. type: 'ancestor',
  30. },
  31. };
  32. this.options = {
  33. multipleSlots: true,
  34. };
  35. this.lifetimes = {
  36. attached() {
  37. this.initStatus();
  38. },
  39. };
  40. this.properties = Object.assign(Object.assign({}, Props), { borderless: {
  41. type: Boolean,
  42. value: false,
  43. } });
  44. this.controlledProps = [
  45. {
  46. key: 'checked',
  47. event: 'change',
  48. },
  49. ];
  50. this.observers = {
  51. checked(isChecked) {
  52. this.setData({
  53. active: isChecked,
  54. });
  55. },
  56. };
  57. this.data = {
  58. prefix,
  59. active: false,
  60. classPrefix: name,
  61. customIcon: false,
  62. optionLinked: false,
  63. iconVal: [],
  64. };
  65. this.methods = {
  66. handleTap(e) {
  67. if (this.data.disabled)
  68. return;
  69. const { target } = e.currentTarget.dataset;
  70. if (target === 'text' && this.data.contentDisabled)
  71. return;
  72. this.doChange();
  73. },
  74. doChange() {
  75. var _a;
  76. const { value, active } = this.data;
  77. const [parent] = (_a = this.getRelationNodes('../radio-group/radio-group')) !== null && _a !== void 0 ? _a : [null];
  78. if (parent) {
  79. parent.updateValue(value);
  80. }
  81. else {
  82. this._trigger('change', { checked: !active });
  83. }
  84. },
  85. initStatus() {
  86. const { icon } = this.data;
  87. const isIdArr = Array.isArray(icon);
  88. this.setData({
  89. customIcon: isIdArr,
  90. iconVal: !isIdArr ? iconDefault[icon] : this.data.icon,
  91. });
  92. },
  93. setDisabled(disabled) {
  94. this.setData({
  95. disabled: this.data.disabled || disabled,
  96. });
  97. },
  98. };
  99. }
  100. };
  101. Radio = __decorate([
  102. wxComponent()
  103. ], Radio);
  104. export default Radio;