radio-group.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = ['class', `${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 } = this.data;
  26. target.setData({
  27. checked: value === target.data.value,
  28. });
  29. target.setDisabled(disabled);
  30. },
  31. },
  32. };
  33. this.properties = props;
  34. this.controlledProps = [
  35. {
  36. key: 'value',
  37. event: 'change',
  38. },
  39. ];
  40. this.observers = {
  41. value(v) {
  42. this.getChilds().forEach((item) => {
  43. item.setData({
  44. checked: v === item.data.value,
  45. });
  46. });
  47. },
  48. options() {
  49. this.initWithOptions();
  50. },
  51. };
  52. this.methods = {
  53. getChilds() {
  54. let items = this.$children;
  55. if (!(items === null || items === void 0 ? void 0 : items.length)) {
  56. items = this.selectAllComponents(`.${prefix}-radio-option`);
  57. }
  58. return items;
  59. },
  60. updateValue(value) {
  61. this._trigger('change', { value });
  62. },
  63. handleRadioChange(e) {
  64. const { value, index } = e.target.dataset;
  65. this._trigger('change', { value, index });
  66. },
  67. initWithOptions() {
  68. const { options, value, keys } = this.data;
  69. if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options)) {
  70. this.setData({
  71. radioOptions: [],
  72. });
  73. return;
  74. }
  75. const optionsValue = [];
  76. try {
  77. options.forEach((element) => {
  78. var _a, _b, _c;
  79. const typeName = typeof element;
  80. if (typeName === 'number' || typeName === 'string') {
  81. optionsValue.push({
  82. label: `${element}`,
  83. value: element,
  84. checked: value === element,
  85. });
  86. }
  87. else if (typeName === 'object') {
  88. 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'] }));
  89. }
  90. });
  91. this.setData({
  92. radioOptions: optionsValue,
  93. });
  94. }
  95. catch (error) {
  96. console.error('error', error);
  97. }
  98. },
  99. };
  100. }
  101. };
  102. RadioGroup = __decorate([
  103. wxComponent()
  104. ], RadioGroup);
  105. export default RadioGroup;