mali-applet-ui 1.0.64 → 1.0.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,14 +2,13 @@
2
2
  <view class="ml-tree" :class="[className || '']">
3
3
  <view v-if="showCheckbox && (showAllCheckbox || showCheckboxBtn)" class="ml-tree-checkbox-btns">
4
4
  <view v-if="showAllCheckbox" class="ml-tree-checkbox-btns-left">
5
- <ml-checkbox v-model="isAllCheckboxChecked" :indeterminate="isAllCheckboxIndeterminate" @change="toggleCheckboxRow()">全选</ml-checkbox>
5
+ <ml-checkbox v-model="isAllCheckboxChecked" :indeterminate="isAllCheckboxIndeterminate" @change="toggleAllCheckboxEvent">全选</ml-checkbox>
6
6
  </view>
7
7
  <view v-if="showCheckboxBtn" class="ml-tree-checkbox-btns-right">
8
8
  <ml-button size="mini" status="primary" @click="setAllTreeExpand(true)">一键展开</ml-button>
9
- <ml-button size="mini" status="primary" @click="setAllTreeExpand(false)">一键收起</ml-button>
10
9
  </view>
11
10
  </view>
12
- <ml-tree-node v-for="(item, index) in data" :key="index" :node-data="item"></ml-tree-node>
11
+ <ml-tree-node v-for="(item, index) in treeData" :key="index" :node-data="item"></ml-tree-node>
13
12
  </view>
14
13
  </template>
15
14
 
@@ -24,10 +23,7 @@ export default {
24
23
  checkStrictly: Boolean,
25
24
  showRadio: Boolean,
26
25
  showCheckbox: Boolean,
27
- showAllCheckbox: {
28
- type: Boolean,
29
- default: true
30
- },
26
+ showAllCheckbox: Boolean,
31
27
  indeterminateToChecked: Boolean,
32
28
  showCheckboxBtn: Boolean,
33
29
  checkRowKeys: Array,
@@ -40,6 +36,7 @@ export default {
40
36
  },
41
37
  data () {
42
38
  return {
39
+ treeData: [],
43
40
  isAllCheckboxChecked: false,
44
41
  isAllCheckboxIndeterminate: false,
45
42
  indeterminateRowKeys: [],
@@ -70,19 +67,72 @@ export default {
70
67
  }
71
68
  },
72
69
  watch: {
73
- checkRowKeys () {
74
-
70
+ data (value) {
71
+ this.treeData = value
72
+ },
73
+ checkRowKeys (value) {
74
+ if (!this.isCheckboxModelUpdate) {
75
+ this.selectCheckboxRowKeys = []
76
+ this.setCheckboxRow(value, true)
77
+ }
78
+ this.isCheckboxModelUpdate = false
75
79
  }
76
80
  },
81
+ created () {
82
+ this.treeData = this.data
83
+ },
77
84
  methods: {
78
85
  selectCheckboxNodeEvent ({ nodeData, nodeKey }) {
79
- const nodeIndex = this.selectCheckboxRowKeys.indexOf(nodeKey)
80
- const checked = nodeIndex === -1
86
+ const { keyField, childrenField } = this
87
+ const checked = this.selectCheckboxRowKeys.indexOf(nodeKey) === -1
81
88
  if (checked) {
82
- this.selectCheckboxRowKeys.push(nodeKey)
89
+ if (this.checkStrictly) {
90
+ if (!this.selectCheckboxRowKeys.includes(nodeKey)) {
91
+ this.selectCheckboxRowKeys.push(nodeKey)
92
+ }
93
+ const halfIndex = this.indeterminateRowKeys.indexOf(nodeKey)
94
+ if (halfIndex > -1) {
95
+ this.indeterminateRowKeys.splice(halfIndex, 1)
96
+ }
97
+ } else {
98
+ XEUtils.eachTree([nodeData], (item) => {
99
+ if (!this.selectCheckboxRowKeys.includes(item[keyField])) {
100
+ this.selectCheckboxRowKeys.push(item[keyField])
101
+ }
102
+ const halfIndex = this.indeterminateRowKeys.indexOf(item[keyField])
103
+ if (halfIndex > -1) {
104
+ this.indeterminateRowKeys.splice(halfIndex, 1)
105
+ }
106
+ }, { children: childrenField })
107
+ }
83
108
  } else {
84
- this.selectCheckboxRowKeys.splice(nodeIndex, 1)
109
+ if (this.checkStrictly) {
110
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(nodeKey)
111
+ if (nodeIndex > -1) {
112
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
113
+ }
114
+ const halfIndex = this.indeterminateRowKeys.indexOf(nodeKey)
115
+ if (halfIndex > -1) {
116
+ this.indeterminateRowKeys.splice(halfIndex, 1)
117
+ }
118
+ } else {
119
+ XEUtils.eachTree([nodeData], (item) => {
120
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(item[keyField])
121
+ if (nodeIndex > -1) {
122
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
123
+ }
124
+ const halfIndex = this.indeterminateRowKeys.indexOf(item[keyField])
125
+ if (halfIndex > -1) {
126
+ this.indeterminateRowKeys.splice(halfIndex, 1)
127
+ }
128
+ }, { children: childrenField })
129
+ }
85
130
  }
131
+ if (!this.checkStrictly) {
132
+ this.updateParentIndeterminateStatus(nodeData)
133
+ }
134
+ this.updateCheckStatus()
135
+ this.isCheckboxModelUpdate = true
86
136
  this.$emit('checkbox-change', { row: nodeData, checked, checkedKeys: this.selectCheckboxRowKeys })
87
137
  this.$emit('update:checkRowKeys', [...this.selectCheckboxRowKeys])
88
138
  },
@@ -96,27 +146,86 @@ export default {
96
146
  }
97
147
  this.$emit('expand-change', { row: nodeData, expanded, expandedKeys: this.expandRowKeys })
98
148
  },
99
- setCheckboxRow (row) {
100
- // let rows = row
101
- // if (!XEUtils.isArray(row)) {
102
- // rows = [row]
103
- // }
149
+ updateParentIndeterminateStatus (nodeData) {
150
+ const { keyField, childrenField } = this
151
+ const rest = XEUtils.findTree(this.treeData, item => item[keyField] === nodeData[keyField])
152
+ if (rest) {
153
+ XEUtils.lastEach(rest.nodes.slice(0, rest.nodes.length - 1), (item) => {
154
+ const currKey = item[keyField]
155
+ const childs = item[childrenField]
156
+ const isAllChecked = childs.every(item => this.selectCheckboxRowKeys.includes(item[keyField]))
157
+ const isAllIndeterminate = !isAllChecked && childs.some(item => this.selectCheckboxRowKeys.includes(item[keyField]) || this.indeterminateRowKeys.includes(item[keyField]))
158
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(currKey)
159
+ if (isAllChecked) {
160
+ if (nodeIndex === -1) {
161
+ this.selectCheckboxRowKeys.push(currKey)
162
+ }
163
+ } else {
164
+ if (nodeIndex > -1) {
165
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
166
+ }
167
+ }
168
+ const halfIndex = this.indeterminateRowKeys.indexOf(currKey)
169
+ if (isAllIndeterminate) {
170
+ if (halfIndex === -1) {
171
+ this.indeterminateRowKeys.push(currKey)
172
+ }
173
+ } else {
174
+ if (halfIndex > -1) {
175
+ this.indeterminateRowKeys.splice(halfIndex, 1)
176
+ }
177
+ }
178
+ }, { children: childrenField })
179
+ }
180
+ },
181
+ updateCheckStatus () {
182
+ const { keyField } = this
183
+ this.isAllCheckboxChecked = this.treeData.every(item => this.selectCheckboxRowKeys.includes(item[keyField]))
184
+ this.isAllCheckboxIndeterminate = !this.isAllCheckboxChecked && this.treeData.some(item => this.selectCheckboxRowKeys.includes(item[keyField]))
185
+ },
186
+ setCheckboxRow (key, checked) {
187
+ let keys = key
188
+ if (!XEUtils.isArray(key)) {
189
+ keys = [key]
190
+ }
191
+ if (checked) {
192
+ keys.forEach(nodeKey => {
193
+ if (!this.selectCheckboxRowKeys.includes(nodeKey)) {
194
+ this.selectCheckboxRowKeys.push(nodeKey)
195
+ }
196
+ })
197
+ } else {
198
+ keys.forEach(nodeKey => {
199
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(nodeKey)
200
+ if (nodeIndex > -1) {
201
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
202
+ }
203
+ })
204
+ }
205
+ this.updateCheckStatus()
206
+ },
207
+ toggleAllCheckboxEvent () {
208
+ this.toggleAllCheckboxRow()
209
+ this.isCheckboxModelUpdate = true
210
+ this.$emit('checkbox-all', { checkedKeys: this.selectCheckboxRowKeys, checkedRows: [] })
211
+ this.$emit('update:checkRowKeys', [...this.selectCheckboxRowKeys])
104
212
  },
105
- toggleCheckboxRow () {
213
+ toggleAllCheckboxRow () {
106
214
  const { keyField, childrenField } = this
107
215
  const rowKeys = []
108
216
  if (this.isAllCheckboxChecked) {
109
- XEUtils.eachTree(this.data, (item) => {
217
+ XEUtils.eachTree(this.treeData, (item) => {
110
218
  rowKeys.push(item[keyField])
111
219
  }, { children: childrenField })
112
220
  }
113
221
  this.selectCheckboxRowKeys = rowKeys
222
+ this.updateCheckStatus()
114
223
  },
115
224
  setAllTreeExpand (expanded) {
116
225
  const { keyField, childrenField } = this
117
226
  const rowKeys = []
118
227
  if (expanded) {
119
- XEUtils.eachTree(this.data, (item) => {
228
+ XEUtils.eachTree(this.treeData, (item) => {
120
229
  if (item[childrenField] && item[childrenField].length > 0) {
121
230
  rowKeys.push(item[keyField])
122
231
  }
@@ -1,7 +1,7 @@
1
1
  <template>
2
2
  <view class="ml-tree-node" :class="[className || '']">
3
3
  <view class="ml-tree-node-warpper">
4
- <view v-if="showCheckbox" class="ml-tree-node-checkbox" :class="{'is-checked': checkCheckbox}">
4
+ <view v-if="showCheckbox" class="ml-tree-node-checkbox" :class="{'is-checked': checkCheckbox, 'is-indeterminate': indeterminate}">
5
5
  <view class="ml-tree-node-checkbox-icon" @click="changeCheckboxEvent">
6
6
  <ml-icon :name="checkCheckbox ? 'checkbox-checked' : (indeterminate ? 'checkbox-indeterminate' : 'checkbox-unchecked')"></ml-icon>
7
7
  </view>
@@ -122,7 +122,8 @@ export default {
122
122
  .ml-tree-node-checkbox {
123
123
  width: 50rpx;
124
124
  text-align: center;
125
- &.is-checked {
125
+ &.is-checked,
126
+ &.is-indeterminate {
126
127
  color: $uni-color-primary;
127
128
  }
128
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mali-applet-ui",
3
- "version": "1.0.64",
3
+ "version": "1.0.66",
4
4
  "description": "码里云小程序组件库",
5
5
  "files": [
6
6
  "lib",