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
| Component({
| properties: {
| show: Boolean,
| title: String,
| options: {
| type: Object,
| observer() {
| this.init();
| },
| },
| multiple: {
| type: Boolean,
| observer() {
| this.init();
| },
| },
| showConfirmButton: Boolean,
| showCloseButton: Boolean,
| confirmButtonText: {
| type: String,
| value: '确定',
| },
| cancelButtonText: {
| type: String,
| value: '取消',
| },
| emptyTip: {
| type: String,
| value: '请选择',
| },
| },
|
| data: {
| _options: [],
| checkedIndexes: [],
| },
|
| methods: {
| attached() {
| this.toast = this.selectComponent('#t-toast');
| },
|
| init() {
| const checkedIndexes = [];
| const _options = this.properties.options.map((opt, i) => {
| const checked = !!opt.checked;
| if (checked) {
| if (this.properties.multiple) checkedIndexes[0] = i;
| else checkedIndexes.push(i);
| }
| return {
| title: opt.title,
| checked,
| };
| });
| this.setData({ checkedIndexes, _options });
| },
|
| onOptionTap(e) {
| const { index } = e.currentTarget.dataset;
| const { checkedIndexes } = this.data;
| let data = {};
| if (this.properties.multiple) {
| if (checkedIndexes.includes(index)) {
| checkedIndexes.splice(index, 1);
| data = { checkedIndexes, [`_options[${index}].checked`]: false };
| } else {
| checkedIndexes.push(index);
| data = { checkedIndexes, [`_options[${index}].checked`]: true };
| }
| } else {
| if (checkedIndexes[0] === index) {
| // 单选不可取消选择
| return;
| }
| data = {
| [`_options[${index}].checked`]: true,
| checkedIndexes: [index],
| };
| if (checkedIndexes[0] !== undefined) {
| data[`_options[${checkedIndexes[0]}].checked`] = false;
| }
| }
| this.setData(data);
| this.triggerEvent('select', { index });
| this._onOptionTap && this._onOptionTap(index);
| if (!this.properties.showConfirmButton && !this.properties.multiple) {
| // 没有确认按钮且是单选的情况下,选择选项则自动确定
| this._onConfirm && this._onConfirm([index]);
| this.setData({ show: false });
| }
| },
|
| onCancel() {
| this.triggerEvent('cancel');
| this._onCancel && this._onCancel();
| this.setData({ show: false });
| },
|
| onConfirm() {
| if (this.data.checkedIndexes.length === 0) {
| this.toast.show({
| icon: '',
| text: this.properties.emptyTip,
| });
| return;
| }
| const indexed = this.data.checkedIndexes;
| this.triggerEvent('confirm', { indexed });
| this._onConfirm && this._onConfirm(indexed);
| this.setData({ show: false });
| },
| },
| });
|
|