@peng_kai/kit 0.2.27 → 0.2.28

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.
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { Checkbox } from 'ant-design-vue';
2
+ import { Checkbox, FormItemRest } from 'ant-design-vue';
3
3
  import { computed, triggerRef, watch } from 'vue';
4
4
 
5
5
  interface TTree {
@@ -9,19 +9,6 @@ interface TTree {
9
9
  children?: TTree[]
10
10
  }
11
11
 
12
- export function treeToIds(tree: TTree[]) {
13
- const ids = [] as number[];
14
-
15
- const isCheckedFn = (node: TTree) => {
16
- node.checked && ids.push(node.id);
17
- node.children?.length && node.children.forEach(isCheckedFn);
18
- };
19
-
20
- tree.forEach(isCheckedFn);
21
-
22
- return ids;
23
- }
24
-
25
12
  function treeToTable(tree: TTree[]) {
26
13
  interface TD {
27
14
  id: number
@@ -79,47 +66,36 @@ function treeToStates(tree: TTree[]) {
79
66
 
80
67
  tree.forEach(assId);
81
68
 
82
- // for (const id in ids) {
83
- // const state = states[id];
84
- // state.indet = isIndet(state.children.map(cid => states[cid].checked));
85
- // state.children.forEach(cid => states[cid].checked = state.checked);
86
- // [...state.parents].reverse().forEach((pid) => {
87
- // const checks = states[pid].children.map(cid => states[cid].checked);
88
- // state.indet = isIndet(checks);
89
- // state.checked = checks.every(checked => checked);
90
- // });
91
- // }
92
-
93
- // Object.keys(states)
94
- // [...ids].forEach((id) => {
95
- // const state = states[id];
96
- // state.checked = true;
97
- // state.children.forEach((cid) => {
98
- // states[cid].checked = true;
99
- // states[cid].indet = false;
100
- // });
101
- // });
102
-
103
69
  return states;
104
70
  }
105
71
 
106
72
  function updateStates(states: ReturnType<typeof treeToStates>, id: number, checked: boolean) {
107
73
  const state = states[id];
74
+
75
+ if (!state)
76
+ return;
77
+
108
78
  state.checked = checked;
109
79
  state.indet = false;
110
80
  state.children.forEach((cid) => {
111
- states[cid].checked = checked;
112
- states[cid].indet = false;
81
+ const cState = states[cid];
82
+ if (cState) {
83
+ cState.checked = checked;
84
+ cState.indet = false;
85
+ }
113
86
  });
114
- [...state.parents].reverse().forEach((pid) => {
115
- const checks = states[pid].children.map(pid => states[pid].checked);
116
- states[pid].indet = isIndet(checks);
117
- states[pid].checked = checks.every(checked => checked);
87
+ [...state.parents].forEach((pid) => {
88
+ const pState = states[pid];
89
+ if (pState) {
90
+ const checks = pState.children.map(cid => states[cid]?.checked);
91
+ pState.indet = isIndet(checks);
92
+ pState.checked = checks.every(checked => checked);
93
+ }
118
94
  });
119
95
  }
120
96
 
121
97
  function statesToIds(states: ReturnType<typeof treeToStates>) {
122
- return Object.keys(states).map(Number).filter(id => states[id].checked);
98
+ return Object.keys(states).map(Number).filter(id => states[id]?.checked);
123
99
  }
124
100
 
125
101
  function findAllParents(tree: any[], id: number, path: any[] = []): any[] | undefined {
@@ -157,60 +133,71 @@ function isIndet(arr: boolean[]) {
157
133
 
158
134
  <script setup lang="ts">
159
135
  const props = defineProps<{
160
- treeData: TTree[]
136
+ treeData: TTree[] | null | undefined
161
137
  }>();
162
138
  const ids = defineModel<number[]>('value', { default: [] });
163
- const tableData = computed(() => treeToTable(props.treeData));
164
- const stateMap = computed(() => treeToStates(props.treeData));
139
+ const tableData = computed(() => props.treeData && treeToTable(props.treeData));
140
+ const stateMap = computed(() => props.treeData && treeToStates(props.treeData));
165
141
 
166
142
  watch(ids, (newV, oldV) => {
167
- if (newV === oldV)
143
+ if (newV === oldV || !stateMap.value)
168
144
  return;
169
145
 
170
- newV.forEach(id => updateStates(stateMap.value, id, true));
146
+ newV.forEach(id => updateStates(stateMap.value!, id, true));
171
147
  triggerRef(stateMap);
172
148
  }, { immediate: true });
173
149
 
150
+ watch(stateMap, (newV) => {
151
+ if (newV) {
152
+ ids.value.forEach(id => updateStates(newV, id, true));
153
+ triggerRef(stateMap);
154
+ }
155
+ });
156
+
174
157
  function onCheckChange(id: number) {
175
- const checked = !ids.value.includes(id);
176
- updateStates(stateMap.value, id, checked);
177
- ids.value = statesToIds(stateMap.value);
158
+ if (stateMap.value) {
159
+ const checked = !ids.value.includes(id);
160
+ updateStates(stateMap.value, id, checked);
161
+ ids.value = statesToIds(stateMap.value);
162
+ }
178
163
  }
179
164
  </script>
180
165
 
181
166
  <template>
182
167
  <table class="w-full border-collapse border-$antd-colorBorderSecondary" border>
183
- <tr v-for="(row, ri) of tableData" :key="ri">
184
- <!-- 模块 -->
185
- <td v-if="row[0]" class="w-[6em] px-[1em] py-0.5em" :rowspan="row[0].span">
186
- <Checkbox
187
- :checked="stateMap[row[0].id].indet ? false : stateMap[row[0].id].checked"
188
- :indeterminate="stateMap[row[0].id].indet" @change="onCheckChange(row[0].id)"
189
- >
190
- {{ row[0].label }}{{ row[0].id }}
191
- </Checkbox>
192
- </td>
193
- <!-- 页面 -->
194
- <td v-if="row[1]" class="w-[9em] px-[1em] py-0.5em">
195
- <Checkbox
196
- :checked="stateMap[row[1].id].indet ? false : stateMap[row[1].id].checked"
197
- :indeterminate="stateMap[row[1].id].indet" @change="onCheckChange(row[1].id)"
198
- >
199
- {{ row[1].label }}
200
- </Checkbox>
201
- </td>
202
- <!-- 功能 -->
203
- <td v-if="row[2]" class="px-[1em] py-0.5em">
204
- <div class="feat-list">
168
+ <FormItemRest>
169
+ <tr v-for="(row, ri) of tableData" :key="ri">
170
+ <!-- 模块 -->
171
+ <td v-if="row[0]" class="w-[6em] px-[1em] py-0.5em" :rowspan="row[0].span">
172
+ <Checkbox
173
+ :checked="stateMap?.[row[0].id].indet ? false : stateMap?.[row[0].id].checked"
174
+ :indeterminate="stateMap?.[row[0].id].indet" @change="onCheckChange(row[0].id)"
175
+ >
176
+ {{ row[0].label }}{{ row[0].id }}
177
+ </Checkbox>
178
+ </td>
179
+ <!-- 页面 -->
180
+ <td v-if="row[1]" class="w-[9em] px-[1em] py-0.5em">
205
181
  <Checkbox
206
- v-for="fItem of row[2]" :key="fItem?.id" :checked="stateMap[fItem.id].checked"
207
- @change="onCheckChange(fItem.id)"
182
+ :checked="stateMap?.[row[1].id].indet ? false : stateMap?.[row[1].id].checked"
183
+ :indeterminate="stateMap?.[row[1].id].indet" @change="onCheckChange(row[1].id)"
208
184
  >
209
- {{ fItem.label }}
185
+ {{ row[1].label }} {{ row[1].id }}
210
186
  </Checkbox>
211
- </div>
212
- </td>
213
- </tr>
187
+ </td>
188
+ <!-- 功能 -->
189
+ <td v-if="row[2]" class="px-[1em] py-0.5em">
190
+ <div class="feat-list">
191
+ <Checkbox
192
+ v-for="fItem of row[2]" :key="fItem?.id" :checked="stateMap?.[fItem.id].checked"
193
+ @change="onCheckChange(fItem.id)"
194
+ >
195
+ {{ fItem.label }} {{ fItem.id }}
196
+ </Checkbox>
197
+ </div>
198
+ </td>
199
+ </tr>
200
+ </FormItemRest>
214
201
  </table>
215
202
  </template>
216
203
 
@@ -1 +1 @@
1
- export { default as PermissionTreeCheckbox, treeToIds } from './PermissionTree.vue';
1
+ export { default as PermissionTreeCheckbox } from './PermissionTree.vue';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.2.27",
4
+ "version": "0.2.28",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",