radio-group.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-group`;
  12. let RadioGroup = class RadioGroup extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [`${prefix}-class`];
  16. this.data = {
  17. prefix,
  18. classPrefix: name,
  19. radioOptions: [],
  20. };
  21. this.relations = {
  22. '../radio/radio': {
  23. type: 'descendant',
  24. linked(target) {
  25. const { value, disabled, readonly } = this.data;
  26. target.setData({
  27. checked: value === target.data.value,
  28. });
  29. target.setDisabled(disabled);
  30. target.setReadonly(readonly);
  31. },
  32. },
  33. };
  34. this.properties = props;
  35. this.controlledProps = [
  36. {
  37. key: 'value',
  38. event: 'change',
  39. },
  40. ];
  41. this.observers = {
  42. value(v) {
  43. this.getChildren().forEach((item) => {
  44. item.setData({
  45. checked: v === item.data.value,
  46. });
  47. });
  48. },
  49. options() {
  50. this.initWithOptions();
  51. },
  52. disabled(v) {
  53. var _a;
  54. if ((_a = this.data.options) === null || _a === void 0 ? void 0 : _a.length) {
  55. this.initWithOptions();
  56. return;
  57. }
  58. this.getChildren().forEach((item) => {
  59. item.setDisabled(v);
  60. });
  61. },
  62. };
  63. this.methods = {
  64. getChildren() {
  65. let items = this.$children;
  66. if (!(items === null || items === void 0 ? void 0 : items.length)) {
  67. items = this.selectAllComponents(`.${prefix}-radio-option`);
  68. }
  69. return items;
  70. },
  71. updateValue(value) {
  72. this._trigger('change', { value });
  73. },
  74. handleRadioChange(e) {
  75. const { checked } = e.detail;
  76. const { value, index, allowUncheck } = e.target.dataset;
  77. this._trigger('change', checked === false && allowUncheck ? { value: null, index } : { value, index });
  78. },
  79. initWithOptions() {
  80. const { options, value, keys, disabled, readonly } = this.data;
  81. if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options)) {
  82. this.setData({
  83. radioOptions: [],
  84. });
  85. return;
  86. }
  87. const optionsValue = [];
  88. try {
  89. options.forEach((element) => {
  90. var _a, _b, _c;
  91. const typeName = typeof element;
  92. if (typeName === 'number' || typeName === 'string') {
  93. optionsValue.push({
  94. label: `${element}`,
  95. value: element,
  96. checked: value === element,
  97. disabled,
  98. readonly,
  99. });
  100. }
  101. else if (typeName === 'object') {
  102. optionsValue.push(Object.assign(Object.assign({}, element), { label: element[(_a = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _a !== void 0 ? _a : 'label'], value: element[(_b = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _b !== void 0 ? _b : 'value'], checked: value === element[(_c = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _c !== void 0 ? _c : 'value'], disabled: element.disabled || disabled, readonly: element.readonly || readonly }));
  103. }
  104. });
  105. this.setData({
  106. radioOptions: optionsValue,
  107. });
  108. }
  109. catch (error) {
  110. console.error('error', error);
  111. }
  112. },
  113. };
  114. }
  115. };
  116. RadioGroup = __decorate([
  117. wxComponent()
  118. ], RadioGroup);
  119. export default RadioGroup;