antd-mini
Version:
antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。
57 lines (54 loc) • 1.33 kB
text/typescript
import mixinValue from '../mixins/value';
import {
Component,
getValueFromProps,
triggerEventValues,
} from '../_util/simply';
import { ChecklistDefaultProps } from './props';
Component({
props: ChecklistDefaultProps,
methods: {
onChange(item) {
const [multiple, options] = getValueFromProps(this, [
'multiple',
'options',
]);
let value;
value = item.value;
if (multiple) {
let currentValue = this.getValue();
if (currentValue.indexOf(value) > -1) {
currentValue = currentValue.filter((v) => v !== value);
} else {
currentValue = [...currentValue, value];
}
if (!this.isControlled()) {
this.update(currentValue);
}
triggerEventValues(this, 'change', [
currentValue,
options.filter((v) => currentValue.indexOf(v.value) > -1),
]);
} else {
if (!this.isControlled()) {
this.update(value);
}
triggerEventValues(this, 'change', [
value,
options.find((v) => v.value === value),
]);
}
},
},
mixins: [
mixinValue({
transformValue(val) {
const value = val || [];
return {
needUpdate: true,
value,
};
},
}),
],
});