mali-applet-ui 1.0.64 → 1.0.65

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,14 @@
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
9
  <ml-button size="mini" status="primary" @click="setAllTreeExpand(false)">一键收起</ml-button>
10
10
  </view>
11
11
  </view>
12
- <ml-tree-node v-for="(item, index) in data" :key="index" :node-data="item"></ml-tree-node>
12
+ <ml-tree-node v-for="(item, index) in treeData" :key="index" :node-data="item"></ml-tree-node>
13
13
  </view>
14
14
  </template>
15
15
 
@@ -24,10 +24,7 @@ export default {
24
24
  checkStrictly: Boolean,
25
25
  showRadio: Boolean,
26
26
  showCheckbox: Boolean,
27
- showAllCheckbox: {
28
- type: Boolean,
29
- default: true
30
- },
27
+ showAllCheckbox: Boolean,
31
28
  indeterminateToChecked: Boolean,
32
29
  showCheckboxBtn: Boolean,
33
30
  checkRowKeys: Array,
@@ -40,6 +37,7 @@ export default {
40
37
  },
41
38
  data () {
42
39
  return {
40
+ treeData: [],
43
41
  isAllCheckboxChecked: false,
44
42
  isAllCheckboxIndeterminate: false,
45
43
  indeterminateRowKeys: [],
@@ -70,19 +68,72 @@ export default {
70
68
  }
71
69
  },
72
70
  watch: {
73
- checkRowKeys () {
74
-
71
+ data (value) {
72
+ this.treeData = value
73
+ },
74
+ checkRowKeys (value) {
75
+ if (!this.isCheckboxModelUpdate) {
76
+ this.selectCheckboxRowKeys = []
77
+ this.setCheckboxRow(value, true)
78
+ }
79
+ this.isCheckboxModelUpdate = false
75
80
  }
76
81
  },
82
+ created () {
83
+ this.treeData = this.data
84
+ },
77
85
  methods: {
78
86
  selectCheckboxNodeEvent ({ nodeData, nodeKey }) {
79
- const nodeIndex = this.selectCheckboxRowKeys.indexOf(nodeKey)
80
- const checked = nodeIndex === -1
87
+ const { keyField, childrenField } = this
88
+ const checked = this.selectCheckboxRowKeys.indexOf(nodeKey) === -1
81
89
  if (checked) {
82
- this.selectCheckboxRowKeys.push(nodeKey)
90
+ if (this.checkStrictly) {
91
+ if (!this.selectCheckboxRowKeys.includes(nodeKey)) {
92
+ this.selectCheckboxRowKeys.push(nodeKey)
93
+ }
94
+ const halfIndex = this.indeterminateRowKeys.indexOf(nodeKey)
95
+ if (halfIndex > -1) {
96
+ this.indeterminateRowKeys.splice(halfIndex, 1)
97
+ }
98
+ } else {
99
+ XEUtils.eachTree([nodeData], (item) => {
100
+ if (!this.selectCheckboxRowKeys.includes(item[keyField])) {
101
+ this.selectCheckboxRowKeys.push(item[keyField])
102
+ }
103
+ const halfIndex = this.indeterminateRowKeys.indexOf(item[keyField])
104
+ if (halfIndex > -1) {
105
+ this.indeterminateRowKeys.splice(halfIndex, 1)
106
+ }
107
+ }, { children: childrenField })
108
+ }
83
109
  } else {
84
- this.selectCheckboxRowKeys.splice(nodeIndex, 1)
110
+ if (this.checkStrictly) {
111
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(nodeKey)
112
+ if (nodeIndex > -1) {
113
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
114
+ }
115
+ const halfIndex = this.indeterminateRowKeys.indexOf(nodeKey)
116
+ if (halfIndex > -1) {
117
+ this.indeterminateRowKeys.splice(halfIndex, 1)
118
+ }
119
+ } else {
120
+ XEUtils.eachTree([nodeData], (item) => {
121
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(item[keyField])
122
+ if (nodeIndex > -1) {
123
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
124
+ }
125
+ const halfIndex = this.indeterminateRowKeys.indexOf(item[keyField])
126
+ if (halfIndex > -1) {
127
+ this.indeterminateRowKeys.splice(halfIndex, 1)
128
+ }
129
+ }, { children: childrenField })
130
+ }
85
131
  }
132
+ if (!this.checkStrictly) {
133
+ this.updateParentIndeterminateStatus(nodeData)
134
+ }
135
+ this.updateCheckStatus()
136
+ this.isCheckboxModelUpdate = true
86
137
  this.$emit('checkbox-change', { row: nodeData, checked, checkedKeys: this.selectCheckboxRowKeys })
87
138
  this.$emit('update:checkRowKeys', [...this.selectCheckboxRowKeys])
88
139
  },
@@ -96,27 +147,86 @@ export default {
96
147
  }
97
148
  this.$emit('expand-change', { row: nodeData, expanded, expandedKeys: this.expandRowKeys })
98
149
  },
99
- setCheckboxRow (row) {
100
- // let rows = row
101
- // if (!XEUtils.isArray(row)) {
102
- // rows = [row]
103
- // }
150
+ updateParentIndeterminateStatus (nodeData) {
151
+ const { keyField, childrenField } = this
152
+ const rest = XEUtils.findTree(this.treeData, item => item[keyField] === nodeData[keyField])
153
+ if (rest) {
154
+ XEUtils.lastEach(rest.nodes.slice(0, rest.nodes.length - 1), (item) => {
155
+ const currKey = item[keyField]
156
+ const childs = item[childrenField]
157
+ const isAllChecked = childs.every(item => this.selectCheckboxRowKeys.includes(item[keyField]))
158
+ const isAllIndeterminate = !isAllChecked && childs.some(item => this.selectCheckboxRowKeys.includes(item[keyField]) || this.indeterminateRowKeys.includes(item[keyField]))
159
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(currKey)
160
+ if (isAllChecked) {
161
+ if (nodeIndex === -1) {
162
+ this.selectCheckboxRowKeys.push(currKey)
163
+ }
164
+ } else {
165
+ if (nodeIndex > -1) {
166
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
167
+ }
168
+ }
169
+ const halfIndex = this.indeterminateRowKeys.indexOf(currKey)
170
+ if (isAllIndeterminate) {
171
+ if (halfIndex === -1) {
172
+ this.indeterminateRowKeys.push(currKey)
173
+ }
174
+ } else {
175
+ if (halfIndex > -1) {
176
+ this.indeterminateRowKeys.splice(halfIndex, 1)
177
+ }
178
+ }
179
+ }, { children: childrenField })
180
+ }
181
+ },
182
+ updateCheckStatus () {
183
+ const { keyField } = this
184
+ this.isAllCheckboxChecked = this.treeData.every(item => this.selectCheckboxRowKeys.includes(item[keyField]))
185
+ this.isAllCheckboxIndeterminate = !this.isAllCheckboxChecked && this.treeData.some(item => this.selectCheckboxRowKeys.includes(item[keyField]))
186
+ },
187
+ setCheckboxRow (key, checked) {
188
+ let keys = key
189
+ if (!XEUtils.isArray(key)) {
190
+ keys = [key]
191
+ }
192
+ if (checked) {
193
+ keys.forEach(nodeKey => {
194
+ if (!this.selectCheckboxRowKeys.includes(nodeKey)) {
195
+ this.selectCheckboxRowKeys.push(nodeKey)
196
+ }
197
+ })
198
+ } else {
199
+ keys.forEach(nodeKey => {
200
+ const nodeIndex = this.selectCheckboxRowKeys.indexOf(nodeKey)
201
+ if (nodeIndex > -1) {
202
+ this.selectCheckboxRowKeys.splice(nodeIndex, 1)
203
+ }
204
+ })
205
+ }
206
+ this.updateCheckStatus()
207
+ },
208
+ toggleAllCheckboxEvent () {
209
+ this.toggleAllCheckboxRow()
210
+ this.isCheckboxModelUpdate = true
211
+ this.$emit('checkbox-all', { checkedKeys: this.selectCheckboxRowKeys, checkedRows: [] })
212
+ this.$emit('update:checkRowKeys', [...this.selectCheckboxRowKeys])
104
213
  },
105
- toggleCheckboxRow () {
214
+ toggleAllCheckboxRow () {
106
215
  const { keyField, childrenField } = this
107
216
  const rowKeys = []
108
217
  if (this.isAllCheckboxChecked) {
109
- XEUtils.eachTree(this.data, (item) => {
218
+ XEUtils.eachTree(this.treeData, (item) => {
110
219
  rowKeys.push(item[keyField])
111
220
  }, { children: childrenField })
112
221
  }
113
222
  this.selectCheckboxRowKeys = rowKeys
223
+ this.updateCheckStatus()
114
224
  },
115
225
  setAllTreeExpand (expanded) {
116
226
  const { keyField, childrenField } = this
117
227
  const rowKeys = []
118
228
  if (expanded) {
119
- XEUtils.eachTree(this.data, (item) => {
229
+ XEUtils.eachTree(this.treeData, (item) => {
120
230
  if (item[childrenField] && item[childrenField].length > 0) {
121
231
  rowKeys.push(item[keyField])
122
232
  }
@@ -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.65",
4
4
  "description": "码里云小程序组件库",
5
5
  "files": [
6
6
  "lib",