radio.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. let Radio = class Radio extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [
  16. `${prefix}-class`,
  17. `${prefix}-class-label`,
  18. `${prefix}-class-icon`,
  19. `${prefix}-class-content`,
  20. `${prefix}-class-border`,
  21. ];
  22. this.behaviors = ['wx://form-field'];
  23. this.relations = {
  24. '../radio-group/radio-group': {
  25. type: 'ancestor',
  26. linked(parent) {
  27. if (parent.data.borderless) {
  28. this.setData({ borderless: true });
  29. }
  30. },
  31. },
  32. };
  33. this.options = {
  34. multipleSlots: true,
  35. };
  36. this.lifetimes = {
  37. attached() {
  38. this.init();
  39. },
  40. };
  41. this.properties = Object.assign(Object.assign({}, Props), { borderless: {
  42. type: Boolean,
  43. value: false,
  44. }, tId: {
  45. type: String,
  46. } });
  47. this.controlledProps = [
  48. {
  49. key: 'checked',
  50. event: 'change',
  51. },
  52. ];
  53. this.data = {
  54. prefix,
  55. classPrefix: name,
  56. customIcon: false,
  57. slotIcon: false,
  58. optionLinked: false,
  59. iconVal: [],
  60. _placement: '',
  61. _disabled: false,
  62. _readonly: false,
  63. };
  64. this.observers = {
  65. disabled(v) {
  66. this.setData({ _disabled: v });
  67. },
  68. readonly(v) {
  69. this.setData({ _readonly: v });
  70. },
  71. };
  72. this.methods = {
  73. handleTap(e) {
  74. const { _disabled, _readonly, contentDisabled } = this.data;
  75. const { target } = e.currentTarget.dataset;
  76. if (_disabled || _readonly || (target === 'text' && contentDisabled))
  77. return;
  78. this.doChange();
  79. },
  80. doChange() {
  81. var _a;
  82. const { value, checked, allowUncheck } = this.data;
  83. const isAllowUncheck = Boolean(allowUncheck || ((_a = this.$parent) === null || _a === void 0 ? void 0 : _a.data.allowUncheck));
  84. if (this.$parent) {
  85. this.$parent.updateValue(checked && isAllowUncheck ? null : value);
  86. }
  87. else {
  88. this._trigger('change', { checked: isAllowUncheck ? !checked : true });
  89. }
  90. },
  91. init() {
  92. var _a, _b, _c, _d, _e, _f;
  93. const { icon } = this.data;
  94. const isIdArr = Array.isArray(((_a = this.$parent) === null || _a === void 0 ? void 0 : _a.icon) || icon);
  95. this.setData({
  96. customIcon: isIdArr,
  97. slotIcon: icon === 'slot',
  98. iconVal: isIdArr ? ((_b = this.$parent) === null || _b === void 0 ? void 0 : _b.icon) || icon : [],
  99. _placement: (_f = (_c = this.data.placement) !== null && _c !== void 0 ? _c : (_e = (_d = this.$parent) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.placement) !== null && _f !== void 0 ? _f : 'left',
  100. });
  101. },
  102. setDisabled(disabled) {
  103. this.setData({
  104. _disabled: this.data.disabled || disabled,
  105. });
  106. },
  107. setReadonly(readonly) {
  108. this.setData({
  109. _readonly: this.data.readonly || readonly,
  110. });
  111. },
  112. };
  113. }
  114. };
  115. Radio = __decorate([
  116. wxComponent()
  117. ], Radio);
  118. export default Radio;