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