Commit 3fd6193ac1a7d2b3d9df0301697a80c61926355a

Authored by xingyu4j
1 parent 61f647b3

perf: 提取tree props

yudao-ui-admin-vue3/src/utils/tree.ts
... ... @@ -8,6 +8,11 @@ const DEFAULT_CONFIG: TreeHelperConfig = {
8 8 children: 'children',
9 9 pid: 'pid'
10 10 }
  11 +export const defaultProps = {
  12 + children: 'children',
  13 + label: 'name',
  14 + value: 'id'
  15 +}
11 16  
12 17 const getConfig = (config: Partial<TreeHelperConfig>) => Object.assign({}, DEFAULT_CONFIG, config)
13 18  
... ... @@ -214,6 +219,10 @@ export const eachTree = (treeDatas: any[], callBack: Fn, parentNode = {}) =&gt; {
214 219 * @param {*} children 孩子节点字段 默认 'children'
215 220 */
216 221 export const handleTree = (data: any[], id?: string, parentId?: string, children?: string) => {
  222 + if (!Array.isArray(data)) {
  223 + console.warn('data must be an array')
  224 + return []
  225 + }
217 226 const config = {
218 227 id: id || 'id',
219 228 parentId: parentId || 'parentId',
... ...
yudao-ui-admin-vue3/src/views/infra/codegen/components/GenInfoForm.vue
... ... @@ -6,7 +6,7 @@ import { onMounted, PropType, reactive, ref, watch } from &#39;vue&#39;
6 6 import { Form } from '@/components/Form'
7 7 import { useForm } from '@/hooks/web/useForm'
8 8 import { required } from '@/utils/formRules'
9   -import { handleTree } from '@/utils/tree'
  9 +import { handleTree, defaultProps } from '@/utils/tree'
10 10 import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
11 11 import { listSimpleMenusApi } from '@/api/system/menu'
12 12 import { CodegenTableVO } from '@/api/infra/codegen/types'
... ... @@ -17,12 +17,6 @@ const props = defineProps({
17 17 default: () => null
18 18 }
19 19 })
20   -const menuProps = {
21   - checkStrictly: true,
22   - children: 'children',
23   - label: 'name',
24   - value: 'id'
25   -}
26 20 const rules = reactive({
27 21 templateType: [required],
28 22 scene: [required],
... ... @@ -104,7 +98,7 @@ const schema = reactive&lt;FormSchema[]&gt;([
104 98 component: 'TreeSelect',
105 99 componentProps: {
106 100 data: menuOptions,
107   - props: menuProps,
  101 + props: defaultProps,
108 102 checkStrictly: true,
109 103 nodeKey: 'id'
110 104 },
... ...
yudao-ui-admin-vue3/src/views/system/dept/index.vue
... ... @@ -78,7 +78,7 @@
78 78 import { nextTick, onMounted, reactive, ref, unref } from 'vue'
79 79 import { ElSelect, ElTreeSelect, ElOption } from 'element-plus'
80 80 import { VxeGridInstance } from 'vxe-table'
81   -import { handleTree } from '@/utils/tree'
  81 +import { handleTree, defaultProps } from '@/utils/tree'
82 82 import { required } from '@/utils/formRules.js'
83 83 import { useI18n } from '@/hooks/web/useI18n'
84 84 import { useMessage } from '@/hooks/web/useMessage'
... ... @@ -115,13 +115,6 @@ const rules = reactive({
115 115 status: [required]
116 116 })
117 117  
118   -// 下拉框[上级]的配置项目
119   -const defaultProps = {
120   - checkStrictly: true,
121   - children: 'children',
122   - label: 'name',
123   - value: 'id'
124   -}
125 118 const getUserList = async () => {
126 119 const res = await getListSimpleUsersApi()
127 120 userOption.value = res
... ...
yudao-ui-admin-vue3/src/views/system/menu/index.vue
... ... @@ -103,7 +103,7 @@
103 103 <el-tree-select
104 104 node-key="id"
105 105 v-model="menuForm.parentId"
106   - :props="menuProps"
  106 + :props="defaultProps"
107 107 :data="menuOptions"
108 108 :default-expanded-keys="[0]"
109 109 check-strictly
... ... @@ -237,8 +237,7 @@ import * as MenuApi from &#39;@/api/system/menu&#39;
237 237 import { required } from '@/utils/formRules.js'
238 238 import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
239 239 import { SystemMenuTypeEnum, CommonStatusEnum } from '@/utils/constants'
240   -import { handleTree } from '@/utils/tree'
241   -import { deepCopy } from 'windicss/utils'
  240 +import { handleTree, defaultProps } from '@/utils/tree'
242 241  
243 242 const { t } = useI18n() // 国际化
244 243 const message = useMessage() // 消息弹窗
... ... @@ -253,7 +252,7 @@ const actionType = ref(&#39;&#39;) // 操作按钮的类型
253 252 const actionLoading = ref(false) // 遮罩层
254 253 // 新增和修改的表单值
255 254 const formRef = ref<FormInstance>()
256   -const menuFormNull = {
  255 +const menuForm = ref<MenuApi.MenuVO>({
257 256 id: 0,
258 257 name: '',
259 258 permission: '',
... ... @@ -266,9 +265,8 @@ const menuFormNull = {
266 265 status: CommonStatusEnum.ENABLE,
267 266 visible: true,
268 267 keepAlive: true,
269   - createTime: ''
270   -}
271   -const menuForm = ref<MenuApi.MenuVO>(menuFormNull)
  268 + createTime: new Date()
  269 +})
272 270 // 新增和修改的表单校验
273 271 const rules = reactive({
274 272 name: [required],
... ... @@ -278,13 +276,6 @@ const rules = reactive({
278 276 })
279 277  
280 278 // ========== 下拉框[上级菜单] ==========
281   -// 下拉框[上级菜单]的配置项目
282   -const menuProps = {
283   - checkStrictly: true,
284   - children: 'children',
285   - label: 'name',
286   - value: 'id'
287   -}
288 279 const menuOptions = ref<any[]>([]) // 树形结构
289 280 // 获取下拉框[上级菜单]的数据
290 281 const getTree = async () => {
... ... @@ -335,7 +326,21 @@ const handleCreate = () =&gt; {
335 326 setDialogTile('create')
336 327 // 重置表单
337 328 formRef.value?.resetFields()
338   - menuForm.value = deepCopy(menuFormNull)
  329 + menuForm.value = {
  330 + id: 0,
  331 + name: '',
  332 + permission: '',
  333 + type: SystemMenuTypeEnum.DIR,
  334 + sort: 1,
  335 + parentId: 0,
  336 + path: '',
  337 + icon: '',
  338 + component: '',
  339 + status: CommonStatusEnum.ENABLE,
  340 + visible: true,
  341 + keepAlive: true,
  342 + createTime: new Date()
  343 + }
339 344 }
340 345  
341 346 // 修改操作
... ...
yudao-ui-admin-vue3/src/views/system/role/index.vue
... ... @@ -164,7 +164,7 @@ import { FormExpose } from &#39;@/components/Form&#39;
164 164 import { useI18n } from '@/hooks/web/useI18n'
165 165 import { useMessage } from '@/hooks/web/useMessage'
166 166 import { useVxeGrid } from '@/hooks/web/useVxeGrid'
167   -import { handleTree } from '@/utils/tree'
  167 +import { handleTree, defaultProps } from '@/utils/tree'
168 168 import { SystemDataScopeEnum } from '@/utils/constants'
169 169 import { DICT_TYPE, getDictOptions } from '@/utils/dict'
170 170 import { rules, allSchemas } from './role.data'
... ... @@ -259,11 +259,6 @@ const dataScopeForm = reactive({
259 259 dataScope: 0,
260 260 checkList: []
261 261 })
262   -const defaultProps = {
263   - children: 'children',
264   - label: 'name',
265   - value: 'id'
266   -}
267 262 const treeOptions = ref<any[]>([]) // 菜单树形结构
268 263 const treeRef = ref<InstanceType<typeof ElTree>>()
269 264 const dialogScopeVisible = ref(false)
... ...
yudao-ui-admin-vue3/src/views/system/tenantPackage/index.vue
... ... @@ -66,7 +66,7 @@
66 66 </template>
67 67 <script setup lang="ts" name="TenantPackage">
68 68 import { onMounted, ref, unref } from 'vue'
69   -import { handleTree } from '@/utils/tree'
  69 +import { handleTree, defaultProps } from '@/utils/tree'
70 70 import { useI18n } from '@/hooks/web/useI18n'
71 71 import { useMessage } from '@/hooks/web/useMessage'
72 72 import { useVxeGrid } from '@/hooks/web/useVxeGrid'
... ... @@ -93,12 +93,6 @@ const actionType = ref(&#39;&#39;) // 操作按钮的类型
93 93 const dialogVisible = ref(false) // 是否显示弹出层
94 94 const dialogTitle = ref('edit') // 弹出层标题
95 95  
96   -const defaultProps = {
97   - children: 'children',
98   - label: 'name',
99   - value: 'id'
100   -}
101   -
102 96 // 全选/全不选
103 97 const handleCheckedTreeNodeAll = () => {
104 98 treeRef.value!.setCheckedNodes(treeNodeAll.value ? menuOptions.value : [])
... ...
yudao-ui-admin-vue3/src/views/system/user/index.vue
... ... @@ -284,7 +284,7 @@ import {
284 284 } from 'element-plus'
285 285 import { useRouter } from 'vue-router'
286 286 import { VxeGridInstance } from 'vxe-table'
287   -import { handleTree } from '@/utils/tree'
  287 +import { handleTree, defaultProps } from '@/utils/tree'
288 288 import download from '@/utils/download'
289 289 import { CommonStatusEnum } from '@/utils/constants'
290 290 import { getAccessToken, getTenantId } from '@/utils/auth'
... ... @@ -306,11 +306,6 @@ import {
306 306 const { t } = useI18n() // 国际化
307 307 const message = useMessage() // 消息弹窗
308 308  
309   -const defaultProps = {
310   - children: 'children',
311   - label: 'name',
312   - value: 'id'
313   -}
314 309 const queryParams = reactive({
315 310 deptId: null
316 311 })
... ... @@ -327,7 +322,7 @@ const { gridOptions, getList, deleteData, exportList } = useVxeGrid&lt;UserApi.User
327 322 })
328 323 // ========== 创建部门树结构 ==========
329 324 const filterText = ref('')
330   -const deptOptions = ref<any[]>([]) // 树形结构
  325 +const deptOptions = ref<Tree[]>([]) // 树形结构
331 326 const treeRef = ref<InstanceType<typeof ElTree>>()
332 327 const getTree = async () => {
333 328 const res = await listSimpleDeptApi()
... ...