沙盘演示系统应用的微信小程序
zuoxiao
2024-08-28 eb3dbfdcb126beeb1d08f3306ac8f5bbc466e133
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 { SuperComponent, wxComponent } from '../common/src/index';
import config from '../common/config';
import props from './props';
const { prefix } = config;
const name = `${prefix}-checkbox-group`;
let CheckBoxGroup = class CheckBoxGroup extends SuperComponent {
    constructor() {
        super(...arguments);
        this.externalClasses = [`${prefix}-class`];
        this.relations = {
            '../checkbox/checkbox': {
                type: 'descendant',
            },
        };
        this.data = {
            prefix,
            classPrefix: name,
            checkboxOptions: [],
        };
        this.properties = props;
        this.observers = {
            value() {
                this.updateChildren();
            },
            options() {
                this.initWithOptions();
            },
            disabled(v) {
                var _a;
                if ((_a = this.data.options) === null || _a === void 0 ? void 0 : _a.length) {
                    this.initWithOptions();
                    return;
                }
                this.getChildren().forEach((item) => {
                    item.setDisabled(v);
                });
            },
        };
        this.lifetimes = {
            ready() {
                this.setCheckall();
            },
        };
        this.controlledProps = [
            {
                key: 'value',
                event: 'change',
            },
        ];
        this.$checkAll = null;
        this.methods = {
            getChildren() {
                let items = this.$children;
                if (!items.length) {
                    items = this.selectAllComponents(`.${prefix}-checkbox-option`);
                }
                return items || [];
            },
            updateChildren() {
                const items = this.getChildren();
                const { value } = this.data;
                if (items.length > 0) {
                    items.forEach((item) => {
                        !item.data.checkAll &&
                            item.setData({
                                checked: value === null || value === void 0 ? void 0 : value.includes(item.data.value),
                            });
                    });
                    if (items.some((item) => item.data.checkAll)) {
                        this.setCheckall();
                    }
                }
            },
            updateValue({ value, checked, checkAll, item, indeterminate }) {
                let { value: newValue } = this.data;
                const { max } = this.data;
                const keySet = new Set(this.getChildren().map((item) => item.data.value));
                newValue = newValue.filter((value) => keySet.has(value));
                if (max && checked && newValue.length === max)
                    return;
                if (checkAll) {
                    const items = this.getChildren();
                    newValue =
                        !checked && indeterminate
                            ? items
                                .filter(({ data }) => !(data.disabled && !newValue.includes(data.value)))
                                .map((item) => item.data.value)
                            : items
                                .filter(({ data }) => {
                                if (data.disabled) {
                                    return newValue.includes(data.value);
                                }
                                return checked && !data.checkAll;
                            })
                                .map(({ data }) => data.value);
                }
                else if (checked) {
                    newValue = newValue.concat(value);
                }
                else {
                    const index = newValue.findIndex((v) => v === value);
                    newValue.splice(index, 1);
                }
                this._trigger('change', { value: newValue, context: item });
            },
            initWithOptions() {
                const { options, value } = this.data;
                if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options))
                    return;
                const checkboxOptions = options.map((item) => {
                    const isLabel = ['number', 'string'].includes(typeof item);
                    return isLabel
                        ? {
                            label: `${item}`,
                            value: item,
                            checked: value === null || value === void 0 ? void 0 : value.includes(item),
                        }
                        : Object.assign(Object.assign({}, item), { checked: value === null || value === void 0 ? void 0 : value.includes(item.value) });
                });
                this.setData({
                    checkboxOptions,
                });
            },
            handleInnerChildChange(e) {
                var _a;
                const { item } = e.target.dataset;
                const { checked } = e.detail;
                const rect = {};
                if (item.checkAll) {
                    rect.indeterminate = (_a = this.$checkAll) === null || _a === void 0 ? void 0 : _a.data.indeterminate;
                }
                this.updateValue(Object.assign(Object.assign(Object.assign({}, item), { checked, item }), rect));
            },
            setCheckall() {
                const items = this.getChildren();
                if (!this.$checkAll) {
                    this.$checkAll = items.find((item) => item.data.checkAll);
                }
                if (!this.$checkAll)
                    return;
                const { value } = this.data;
                const valueSet = new Set(value === null || value === void 0 ? void 0 : value.filter((val) => val !== this.$checkAll.data.value));
                const isCheckall = items.every((item) => (item.data.checkAll ? true : valueSet.has(item.data.value)));
                this.$checkAll.setData({
                    checked: valueSet.size > 0,
                    indeterminate: !isCheckall,
                });
            },
        };
    }
};
CheckBoxGroup = __decorate([
    wxComponent()
], CheckBoxGroup);
export default CheckBoxGroup;