water_xcx/miniprogram_npm/tdesign-miniprogram/radio-group/radio-group.js

104 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2025-04-15 15:44:51 +08:00
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import config from '../common/config';
import { SuperComponent, wxComponent } from '../common/src/index';
import Props from './props';
const { prefix } = config;
const name = `${prefix}-radio-group`;
let RadioGroup = class RadioGroup extends SuperComponent {
constructor() {
super(...arguments);
this.externalClasses = [`${prefix}-class`];
this.data = {
prefix,
classPrefix: name,
radioOptions: [],
};
this.relations = {
'../radio/radio': {
type: 'descendant',
linked(target) {
const { value, disabled } = this.data;
target.setData({
checked: value === target.data.value,
});
target.setDisabled(disabled);
},
},
};
this.properties = Object.assign(Object.assign({}, Props), { borderless: {
type: Boolean,
value: false,
} });
this.controlledProps = [
{
key: 'value',
event: 'change',
},
];
this.observers = {
value() {
this.getChilds().forEach((item) => {
item.setData({
checked: this.data.value === item.data.value,
});
});
},
options() {
this.initWithOptions();
},
};
this.methods = {
getChilds() {
let items = this.getRelationNodes('../radio/radio');
if (!(items === null || items === void 0 ? void 0 : items.length)) {
items = this.selectAllComponents(`.${prefix}-radio-option`);
}
return items;
},
updateValue(value) {
this._trigger('change', { value });
},
handleRadioChange(e) {
const { value, index } = e.target.dataset;
this._trigger('change', { value, index });
},
initWithOptions() {
const { options } = this.data;
if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options)) {
return;
}
const optionsValue = [];
try {
options.forEach((element) => {
const typeName = typeof element;
if (typeName === 'number' || typeName === 'string') {
optionsValue.push({
label: `${element}`,
value: element,
});
}
else if (typeName === 'object') {
optionsValue.push(Object.assign({}, element));
}
});
this.setData({
radioOptions: optionsValue,
});
}
catch (error) {
console.error('error', error);
}
},
};
}
};
RadioGroup = __decorate([
wxComponent()
], RadioGroup);
export default RadioGroup;