@aspire-ui/element-component-pro 1.0.1 → 1.0.3

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.
package/dist/style.css CHANGED
@@ -1 +1 @@
1
- .ecp-pro-form-item__help-icon[data-v-cfc968c5]{margin-left:4px;color:#909399;cursor:help;font-size:14px}.ecp-pro-form-item__help-icon[data-v-cfc968c5]:hover{color:#409eff}.ecp-pro-form-item__help-item[data-v-cfc968c5]{margin-bottom:4px}.ecp-pro-form-item__help-item[data-v-cfc968c5]:last-child{margin-bottom:0}.ecp-form-actions[data-v-489c88d2]{text-align:right}.ecp-form-actions__advance[data-v-489c88d2]{margin-right:8px}.el-icon-d-arrow-left.up[data-v-489c88d2]{transform:rotate(90deg)}.el-icon-d-arrow-left.down[data-v-489c88d2]{transform:rotate(-90deg)}.ecp-pro-form[data-v-af9453ad]{padding:16px;position:relative}.ecp-pro-form__advance[data-v-af9453ad]{margin-bottom:16px}.ecp-pro-form_col[data-v-af9453ad]{position:relative;float:right}.el-icon-d-arrow-left.up[data-v-af9453ad]{transform:rotate(90deg)}.el-icon-d-arrow-left.down[data-v-af9453ad]{transform:rotate(-90deg)}.ecp-form-actions__advance[data-v-af9453ad]{position:absolute;bottom:0;left:50%;transform:translate(-50%,-50%)}
1
+ .ecp-pro-table[data-v-c5638c20]{padding:16px;background:#fff;width:100%;box-sizing:border-box}.ecp-pro-table[data-v-c5638c20] .el-table{width:100%!important}.ecp-pro-table__header[data-v-c5638c20]{display:flex;justify-content:space-between;align-items:center;margin-bottom:16px}.ecp-pro-table__title-wrapper[data-v-c5638c20]{display:flex;align-items:center;gap:4px}.ecp-pro-table__title[data-v-c5638c20]{font-size:16px;font-weight:600}.ecp-pro-table__help[data-v-c5638c20]{color:#909399;cursor:help}.ecp-pro-table__toolbar[data-v-c5638c20]{display:flex;align-items:center;gap:8px}.ecp-pro-table__body[data-v-c5638c20]{width:100%}.ecp-pro-table__pagination[data-v-c5638c20]{margin-top:16px;display:flex;justify-content:flex-end}.ecp-pro-table__col-help[data-v-c5638c20]{margin-left:4px;color:#909399;cursor:help}.ecp-table-action[data-v-1b2d6c42],.ecp-table-action__item[data-v-1b2d6c42]{display:inline-flex;align-items:center;gap:4px}.ecp-table-action__icon[data-v-1b2d6c42]{margin-right:4px}.ecp-table-action__more[data-v-1b2d6c42]{display:inline-flex;align-items:center}.ecp-table-action__dropdown-item[data-v-1b2d6c42]{display:inline-flex;align-items:center;gap:4px}.ecp-tree-select[data-v-f30bba11]{position:relative;width:100%}.ecp-tree-select__filter-inner[data-v-f30bba11]{margin-bottom:8px}.ecp-tree-select__dropdown[data-v-f30bba11]{position:absolute;top:100%;left:0;right:0;max-height:280px;overflow:auto;background:#fff;border:1px solid #dcdfe6;border-radius:4px;margin-top:4px;z-index:1000;padding:8px}.ecp-tree-select__loading[data-v-f30bba11]{padding:24px;text-align:center;color:#909399;font-size:14px}.ecp-pro-form-item__help-icon[data-v-0dcbe9b0]{margin-left:4px;color:#909399;cursor:help;font-size:14px}.ecp-pro-form-item__help-icon[data-v-0dcbe9b0]:hover{color:#409eff}.ecp-pro-form-item__help-item[data-v-0dcbe9b0]{margin-bottom:4px}.ecp-pro-form-item__help-item[data-v-0dcbe9b0]:last-child{margin-bottom:0}.ecp-form-actions[data-v-489c88d2]{text-align:right}.ecp-form-actions__advance[data-v-489c88d2]{margin-right:8px}.el-icon-d-arrow-left.up[data-v-489c88d2]{transform:rotate(90deg)}.el-icon-d-arrow-left.down[data-v-489c88d2]{transform:rotate(-90deg)}.ecp-pro-form[data-v-4ee1cb87]{padding:16px;position:relative}.ecp-pro-form__advance[data-v-4ee1cb87]{margin-bottom:16px}.ecp-pro-form_col[data-v-4ee1cb87]{position:relative;float:right}.el-icon-d-arrow-left.up[data-v-4ee1cb87]{transform:rotate(90deg)}.el-icon-d-arrow-left.down[data-v-4ee1cb87]{transform:rotate(-90deg)}.ecp-form-actions__advance[data-v-4ee1cb87]{position:absolute;bottom:0;left:50%;transform:translate(-50%,-50%)}
@@ -1,54 +1,5 @@
1
1
  import { VNode } from 'vue';
2
2
 
3
- /** ProTable 列配置 */
4
- export interface ProColumn {
5
- /** 列标题 */
6
- title: string;
7
- /** 数据字段名 */
8
- dataIndex: string;
9
- /** 列宽度 */
10
- width?: number | string;
11
- /** 最小宽度 */
12
- minWidth?: number | string;
13
- /** 固定列 */
14
- fixed?: 'left' | 'right';
15
- /** 是否可排序 */
16
- sortable?: boolean;
17
- /** 对齐方式 */
18
- align?: 'left' | 'center' | 'right';
19
- /** 是否可调整宽度 */
20
- resizable?: boolean;
21
- /** 是否隐藏 */
22
- hideInTable?: boolean;
23
- /** 值类型 */
24
- valueType?: 'text' | 'date' | 'dateTime' | 'option' | 'select' | 'index';
25
- /** 枚举值映射 */
26
- valueEnum?: Record<string | number, {
27
- text: string;
28
- status?: string;
29
- }>;
30
- /** 自定义渲染 */
31
- customRender?: (params: {
32
- text: unknown;
33
- record: Record<string, unknown>;
34
- index: number;
35
- }) => VNode | string;
36
- /** 表单项配置(用于搜索) */
37
- fieldProps?: Record<string, unknown>;
38
- /** 表单项占位符 */
39
- placeholder?: string;
40
- }
41
- /** ProTable 工具栏配置 */
42
- export interface ProTableToolBarConfig {
43
- /** 是否显示刷新按钮 */
44
- reload?: boolean;
45
- /** 是否显示密度切换 */
46
- density?: boolean;
47
- /** 是否显示列设置 */
48
- columnSetting?: boolean;
49
- /** 是否显示全屏 */
50
- fullScreen?: boolean;
51
- }
52
3
  /** 栅格配置 */
53
4
  export interface ColEx {
54
5
  span?: number;
@@ -121,15 +72,26 @@ export interface ProFormProps {
121
72
  fieldMapToTime?: FieldMapToTime[];
122
73
  /** 透传给 el-form 的事件监听器 */
123
74
  formListeners?: FormListeners;
75
+ /** 自定义组件映射(组件名 -> 组件定义),供 schema.component 使用 */
76
+ components?: Record<string, unknown>;
124
77
  }
78
+ /** ProForm 内置表单项组件类型 */
79
+ export type ProFormBuiltInComponent = 'input' | 'select' | 'api-select' | 'tree-select' | 'date-picker' | 'date-range' | 'input-number' | 'switch' | 'cascader' | 'checkbox' | 'radio';
80
+ /** 自定义组件:组件名(string)或 Vue 组件选项/构造函数(object | Function) */
81
+ export type ProFormCustomComponent = string | object | ((...args: unknown[]) => unknown);
125
82
  /** ProForm 表单项配置 */
126
83
  export interface ProFormSchema {
127
84
  /** 字段名 */
128
85
  field: string;
129
86
  /** 标签 */
130
87
  label: string;
131
- /** 组件类型 */
132
- component?: 'input' | 'select' | 'date-picker' | 'date-range' | 'input-number' | 'switch' | 'cascader' | 'checkbox' | 'radio';
88
+ /**
89
+ * 组件类型:
90
+ * - 内置:'input' | 'select' | 'date-picker' | 'date-range' | 'input-number' | 'switch' | 'cascader' | 'checkbox' | 'radio'
91
+ * - 自定义组件名:任意字符串,对应全局或 ProForm 传入的 components 中注册的组件
92
+ * - 内联组件:直接传入 Vue 组件选项对象或构造函数
93
+ */
94
+ component?: ProFormBuiltInComponent | ProFormCustomComponent;
133
95
  /** 组件属性,支持函数 */
134
96
  componentProps?: Record<string, unknown> | ((params: RenderCallbackParams & {
135
97
  formActionType?: FormActionType;
@@ -0,0 +1,12 @@
1
+ export interface UseComponentSettingReturn {
2
+ /** 获取组件默认配置;不传参时返回全部组件的配置 */
3
+ getSetting: (componentName?: string) => Record<string, unknown>;
4
+ /** 设置组件默认配置(与已有配置浅合并) */
5
+ setSetting: (componentName: string, config: Record<string, unknown>) => void;
6
+ }
7
+ /**
8
+ * 组件默认配置:供所有组件统一获取/设置默认配置
9
+ * - getSetting:获取组件默认配置,用于初始化或合并 props
10
+ * - setSetting:设置组件默认配置,可在应用入口或按需调用
11
+ */
12
+ export declare function useComponentSetting(): UseComponentSettingReturn;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aspire-ui/element-component-pro",
3
- "version": "1.0.01",
3
+ "version": "1.0.03",
4
4
  "description": "Element UI 二次封装组件库,基于 Vue 2.7 + TypeScript + setup 语法糖,实现 VbenAdmin 风格的 Pro 组件",
5
5
  "type": "module",
6
6
  "main": "./dist/element-component-pro.umd.js",
@@ -21,20 +21,22 @@
21
21
  "scripts": {
22
22
  "dev": "vite",
23
23
  "build": "vite build",
24
- "preview": "vite preview"
24
+ "preview": "vite preview",
25
+ "test:loading": "node scripts/test-loading.cjs"
25
26
  },
26
27
  "peerDependencies": {
27
- "vue": "^2.7.0",
28
- "element-ui": "^2.15.0"
28
+ "element-ui": "^2.15.0",
29
+ "vue": "^2.7.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@vitejs/plugin-vue2": "^2.3.1",
32
- "vue-router": "^3.6.5",
33
33
  "element-ui": "^2.15.14",
34
+ "playwright": "^1.40.0",
34
35
  "typescript": "^5.3.0",
35
36
  "vite": "^4.5.0",
36
37
  "vite-plugin-dts": "^3.7.0",
37
38
  "vue": "^2.7.16",
39
+ "vue-router": "^3.6.5",
38
40
  "vue-tsc": "^1.8.27"
39
41
  },
40
42
  "keywords": [
@@ -0,0 +1,85 @@
1
+ <template>
2
+ <el-select :value="value" :placeholder="placeholder" :disabled="disabled" :loading="loading" :clearable="clearable"
3
+ :filterable="filterable" :multiple="multiple" v-bind="$attrs" @input="$emit('input', $event)"
4
+ @visible-change="onVisibleChange">
5
+ <el-option v-for="opt in options" :key="String(opt.value)" :label="opt.label" :value="opt.value" />
6
+ </el-select>
7
+ </template>
8
+
9
+ <script setup lang="ts">
10
+ import { ref, watch, onMounted } from 'vue'
11
+
12
+ const props = withDefaults(
13
+ defineProps<{
14
+ value?: unknown
15
+ /** 拉取选项的接口,返回 Promise<Array<{ label, value }>> 或 { list: [] };可接收当前 params 作为参数 */
16
+ api?: (params?: Record<string, unknown>) => Promise<unknown>
17
+ /** 请求参数,变化时会重新拉取 options */
18
+ params?: Record<string, unknown>
19
+ /** 是否懒加载:为 true 时在展开下拉时再请求数据,不在挂载时请求 */
20
+ lazy?: boolean
21
+ /** 接口返回列表中「标签」字段名,默认 'label' */
22
+ labelField?: string
23
+ /** 接口返回列表中「值」字段名,默认 'value' */
24
+ valueField?: string
25
+ placeholder?: string
26
+ disabled?: boolean
27
+ clearable?: boolean
28
+ filterable?: boolean
29
+ multiple?: boolean
30
+ }>(),
31
+ { labelField: 'label', valueField: 'value', lazy: false }
32
+ )
33
+
34
+ defineEmits<{ (e: 'input', value: unknown): void }>()
35
+
36
+ const loading = ref(false)
37
+ const options = ref<Array<{ label: string; value: unknown }>>([])
38
+
39
+ const onVisibleChange = (visible: boolean) => {
40
+ if (props.lazy && visible) fetchOptions()
41
+ }
42
+
43
+ const fetchOptions = async () => {
44
+ if (!props.api) return
45
+ loading.value = true
46
+ try {
47
+ const res = await props.api(props.params)
48
+ const raw = Array.isArray(res)
49
+ ? res
50
+ : ((res as Record<string, unknown>)?.list as unknown[]) ??
51
+ ((res as Record<string, unknown>)?.data as unknown[]) ??
52
+ []
53
+ const labelKey = props.labelField ?? 'label'
54
+ const valueKey = props.valueField ?? 'value'
55
+ options.value = raw.map((item: unknown) => {
56
+ const o = item as Record<string, unknown>
57
+ return {
58
+ label: String(o[labelKey] ?? o.label ?? ''),
59
+ value: o[valueKey] ?? o.value,
60
+ }
61
+ })
62
+ } finally {
63
+ loading.value = false
64
+ }
65
+ }
66
+
67
+ onMounted(() => {
68
+ if (!props.lazy) fetchOptions()
69
+ })
70
+ watch(() => props.api, () => {
71
+ if (!props.lazy) {
72
+ fetchOptions()
73
+ } else {
74
+ options.value = []
75
+ }
76
+ }, { deep: true }
77
+ )
78
+ watch(() => props.params, () => {
79
+ if (props.lazy) {
80
+ options.value = []
81
+ } else {
82
+ fetchOptions()
83
+ }
84
+ }, { deep: true })
85
+ </script>
@@ -9,7 +9,8 @@
9
9
  <el-col v-if="shouldShow(schema)" :key="schema.field" v-bind="getColProps(schema)"
10
10
  :offset="schema.colProps?.offset ?? effectiveProps.baseColProps?.offset ?? 0" :data-field="schema.field">
11
11
  <ProFormItem :schema="schema" :form-model="formModel" :form-disabled="effectiveProps.disabled"
12
- :auto-placeholder="effectiveProps.autoSetPlaceholder" :form-action-type="formActionRef">
12
+ :auto-placeholder="effectiveProps.autoSetPlaceholder" :form-action-type="formActionRef"
13
+ :custom-components="formCustomComponents">
13
14
  <template v-if="slots[getSlotName(schema)]">
14
15
  <slot :name="getSlotName(schema)" :model="formModel" :schema="schema" :field="schema.field"
15
16
  :values="formModel" />
@@ -56,6 +57,7 @@
56
57
 
57
58
  <script setup lang="ts">
58
59
  import { ref, computed, watch, useSlots, onMounted, onUnmounted } from 'vue'
60
+ import { useComponentSetting } from '../useComponentSetting'
59
61
  import ProFormItem from './ProFormItem.vue'
60
62
  import FormActions from './FormActions.vue'
61
63
  import type { ProFormSchema, ProFormProps, FormActionType, ColEx, ScrollToFieldOptions, FormListeners } from '../types'
@@ -87,6 +89,8 @@ const props = withDefaults(
87
89
  resetFunc?: () => Promise<void>
88
90
  submitOnReset?: boolean
89
91
  formListeners?: FormListeners
92
+ /** 自定义组件映射(组件名 -> 组件),供 schema.component 使用 */
93
+ components?: Record<string, unknown>
90
94
  }>(),
91
95
  {
92
96
  labelWidth: '120px',
@@ -142,7 +146,14 @@ const getEffectiveSpan = (colProps?: ColEx | null, baseColProps?: ColEx | null,
142
146
  return col.xs ?? base.xs ?? col.span ?? fallback
143
147
  }
144
148
 
145
- const effectiveProps = computed(() => ({ ...props, ...innerProps.value }))
149
+ const { getSetting: getComponentSetting } = useComponentSetting()
150
+ const effectiveProps = computed(() => ({ ...getComponentSetting('ProForm'), ...props, ...innerProps.value }))
151
+ /** 传给 ProFormItem 的自定义组件映射:显式合并 setSetting 与 props,避免响应式代理导致组件引用丢失 */
152
+ const formCustomComponents = computed(() => ({
153
+ ...(getComponentSetting('ProForm').components as Record<string, unknown> | undefined) ?? {},
154
+ ...(props.components ?? {}),
155
+ ...(innerProps.value.components ?? {}),
156
+ }))
146
157
  const effectiveActionColOptions = computed(() => effectiveProps.value.actionColOptions ?? { span: 24 })
147
158
 
148
159
  /** 当前视口宽度,用于响应式计算 span */
@@ -347,7 +358,6 @@ const setProps = async (formProps: Partial<ProFormProps>) => {
347
358
  innerProps.value = { ...innerProps.value, ...formProps }
348
359
  if (formProps.schemas) {
349
360
  innerSchemas.value = [...formProps.schemas]
350
- debugger
351
361
  initForm()
352
362
  }
353
363
  }
@@ -33,8 +33,19 @@
33
33
  <slot v-else-if="hasSlot" :model="formModel" :schema="schema" :field="schema.field" :values="formModel" />
34
34
  <!-- 默认组件渲染 -->
35
35
  <template v-else>
36
+ <!-- 自定义 component:组件名或内联组件 -->
37
+ <component
38
+ v-if="resolvedCustomComponent"
39
+ :is="resolvedCustomComponent"
40
+ :value="formModel[schema.field]"
41
+ @input="setFieldValue"
42
+ :placeholder="schema.placeholder || (autoPlaceholder ? `请输入${schema.label}` : undefined)"
43
+ :disabled="effectiveDisabled"
44
+ v-bind="effectiveComponentProps"
45
+ v-on="effectiveComponentListeners"
46
+ />
36
47
  <el-input
37
- v-if="schema.component === 'input' || !schema.component"
48
+ v-else-if="schema.component === 'input' || !schema.component"
38
49
  v-model="formModel[schema.field]"
39
50
  :placeholder="schema.placeholder || (autoPlaceholder ? `请输入${schema.label}` : undefined)"
40
51
  :disabled="effectiveDisabled"
@@ -65,6 +76,24 @@
65
76
  :value="opt.value"
66
77
  />
67
78
  </el-select>
79
+ <ApiSelect
80
+ v-else-if="schema.component === 'api-select'"
81
+ :value="formModel[schema.field]"
82
+ :placeholder="schema.placeholder || (autoPlaceholder ? `请选择${schema.label}` : undefined)"
83
+ :disabled="effectiveDisabled"
84
+ v-bind="effectiveComponentProps"
85
+ v-on="effectiveComponentListeners"
86
+ @input="setFieldValue"
87
+ />
88
+ <TreeSelect
89
+ v-else-if="schema.component === 'tree-select'"
90
+ :value="formModel[schema.field]"
91
+ :placeholder="schema.placeholder || (autoPlaceholder ? `请选择${schema.label}` : undefined)"
92
+ :disabled="effectiveDisabled"
93
+ v-bind="effectiveComponentProps"
94
+ v-on="effectiveComponentListeners"
95
+ @input="setFieldValue"
96
+ />
68
97
  <el-date-picker
69
98
  v-else-if="schema.component === 'date-picker'"
70
99
  v-model="formModel[schema.field]"
@@ -136,14 +165,23 @@
136
165
 
137
166
  <script setup lang="ts">
138
167
  import { computed, useSlots, h } from 'vue'
168
+ import ApiSelect from './ApiSelect.vue'
169
+ import TreeSelect from './TreeSelect.vue'
139
170
  import type { ProFormSchema, RenderCallbackParams } from '../types'
140
171
 
172
+ const BUILT_IN_COMPONENTS: Set<string> = new Set([
173
+ 'input', 'select', 'api-select', 'tree-select', 'date-picker', 'date-range', 'input-number',
174
+ 'switch', 'cascader', 'checkbox', 'radio',
175
+ ])
176
+
141
177
  const props = defineProps<{
142
178
  schema: ProFormSchema
143
179
  formModel: Record<string, unknown>
144
180
  formDisabled?: boolean
145
181
  autoPlaceholder?: boolean
146
182
  formActionType?: import('../types').FormActionType
183
+ /** 自定义组件映射(由 ProForm 传入) */
184
+ customComponents?: Record<string, unknown>
147
185
  }>()
148
186
 
149
187
  const slots = useSlots()
@@ -216,6 +254,20 @@ const getOptions = (props: Record<string, unknown>): Array<{ label: string; valu
216
254
  return Array.isArray(opts) ? opts : undefined
217
255
  }
218
256
 
257
+ const resolvedCustomComponent = computed(() => {
258
+ const c = props.schema.component
259
+ if (c == null) return null
260
+ if (typeof c === 'string') {
261
+ if (BUILT_IN_COMPONENTS.has(c)) return null
262
+ return (props.customComponents && props.customComponents[c]) || c
263
+ }
264
+ return c
265
+ })
266
+
267
+ const setFieldValue = (v: unknown) => {
268
+ props.formModel[props.schema.field] = v
269
+ }
270
+
219
271
  const renderComponent = computed(() => {
220
272
  const renderFn = props.schema.render
221
273
  if (!renderFn) return null
@@ -0,0 +1,272 @@
1
+ <template>
2
+ <div class="ecp-tree-select" ref="rootRef">
3
+ <el-input
4
+ :value="displayText"
5
+ :placeholder="placeholder"
6
+ :disabled="disabled"
7
+ :clearable="clearable"
8
+ readonly
9
+ suffix-icon="el-icon-arrow-down"
10
+ class="ecp-tree-select__input"
11
+ @focus="openDropdown"
12
+ @clear="clearValue"
13
+ />
14
+ <transition name="el-zoom-in-top">
15
+ <div v-show="dropdownVisible" class="ecp-tree-select__dropdown">
16
+ <div v-if="filterable" class="ecp-tree-select__filter-inner">
17
+ <el-input
18
+ v-model="filterText"
19
+ size="small"
20
+ placeholder="搜索节点"
21
+ prefix-icon="el-icon-search"
22
+ clearable
23
+ @click.native.stop
24
+ />
25
+ </div>
26
+ <el-tree
27
+ v-show="!loading"
28
+ ref="treeRef"
29
+ :data="treeData"
30
+ :props="treeProps"
31
+ :node-key="valueField"
32
+ :filter-node-method="filterable ? filterNodeMethod : undefined"
33
+ :highlight-current="true"
34
+ default-expand-all
35
+ @node-click="onNodeClick"
36
+ />
37
+ <div v-if="loading" class="ecp-tree-select__loading">
38
+ <i class="el-icon-loading" /> 加载中...
39
+ </div>
40
+ </div>
41
+ </transition>
42
+ </div>
43
+ </template>
44
+
45
+ <script setup lang="ts">
46
+ import { ref, watch, onMounted, computed, nextTick } from 'vue'
47
+
48
+ interface TreeNode {
49
+ label?: string
50
+ value?: unknown
51
+ children?: TreeNode[]
52
+ [key: string]: unknown
53
+ }
54
+
55
+ const props = withDefaults(
56
+ defineProps<{
57
+ value?: unknown
58
+ /** 树数据,直接传入时优先使用,不请求 api */
59
+ treeData?: unknown[]
60
+ api?: (params?: Record<string, unknown>) => Promise<unknown>
61
+ params?: Record<string, unknown>
62
+ lazy?: boolean
63
+ labelField?: string
64
+ valueField?: string
65
+ childrenField?: string
66
+ filterable?: boolean
67
+ placeholder?: string
68
+ disabled?: boolean
69
+ clearable?: boolean
70
+ }>(),
71
+ {
72
+ labelField: 'label',
73
+ valueField: 'value',
74
+ childrenField: 'children',
75
+ lazy: false,
76
+ }
77
+ )
78
+
79
+ const emit = defineEmits<{ (e: 'input', value: unknown): void }>()
80
+
81
+ const rootRef = ref<HTMLElement>()
82
+ const treeRef = ref()
83
+ const dropdownVisible = ref(false)
84
+ const loading = ref(false)
85
+ const filterText = ref('')
86
+ const treeData = ref<TreeNode[]>([])
87
+ const flatLabelMap = ref<Record<string, string>>({})
88
+
89
+ const treeProps = computed(() => ({
90
+ label: props.labelField,
91
+ children: props.childrenField,
92
+ }))
93
+
94
+ function normalizeNode(node: Record<string, unknown>): TreeNode {
95
+ const labelKey = props.labelField ?? 'label'
96
+ const valueKey = props.valueField ?? 'value'
97
+ const childrenKey = props.childrenField ?? 'children'
98
+ const children = node[childrenKey] as unknown[]
99
+ const out: TreeNode = {}
100
+ out[props.labelField ?? 'label'] = node[labelKey] ?? node.label
101
+ out[props.valueField ?? 'value'] = node[valueKey] ?? node.value
102
+ if (Array.isArray(children) && children.length) {
103
+ out[props.childrenField ?? 'children'] = children.map((c) => normalizeNode(c as Record<string, unknown>))
104
+ }
105
+ return out
106
+ }
107
+
108
+ function buildFlatLabelMap(nodes: TreeNode[], prefix = ''): Record<string, string> {
109
+ const map: Record<string, string> = {}
110
+ const labelKey = props.labelField ?? 'label'
111
+ const valueKey = props.valueField ?? 'value'
112
+ const childrenKey = props.childrenField ?? 'children'
113
+ for (const node of nodes) {
114
+ const label = String(node[labelKey] ?? node.label ?? '')
115
+ const value = node[valueKey] ?? node.value
116
+ if (value !== undefined && value !== null) {
117
+ map[String(value)] = prefix ? prefix + ' / ' + label : label
118
+ }
119
+ const children = node[childrenKey] ?? node.children
120
+ if (Array.isArray(children) && children.length) {
121
+ Object.assign(map, buildFlatLabelMap(children as TreeNode[], label))
122
+ }
123
+ }
124
+ return map
125
+ }
126
+
127
+ function hasTreeDataProp() {
128
+ const td = props.treeData
129
+ return Array.isArray(td) && td.length > 0
130
+ }
131
+
132
+ function applyTreeData(nodes: TreeNode[]) {
133
+ treeData.value = nodes
134
+ flatLabelMap.value = buildFlatLabelMap(nodes)
135
+ }
136
+
137
+ function syncFromTreeDataProp() {
138
+ const td = props.treeData
139
+ if (!Array.isArray(td) || td.length === 0) return
140
+ const normalized = td.map((item) => normalizeNode(item as Record<string, unknown>))
141
+ applyTreeData(normalized)
142
+ }
143
+
144
+ const displayText = computed(() => {
145
+ if (props.value == null || props.value === '') return ''
146
+ return flatLabelMap.value[String(props.value)] ?? String(props.value)
147
+ })
148
+
149
+ const filterNodeMethod = (value: string, data: TreeNode) => {
150
+ if (!value) return true
151
+ const labelKey = props.labelField ?? 'label'
152
+ const label = String(data[labelKey] ?? data.label ?? '')
153
+ return label.toLowerCase().includes(value.toLowerCase())
154
+ }
155
+
156
+ watch(filterText, (val) => {
157
+ treeRef.value?.filter(val)
158
+ })
159
+
160
+ let clickOutsideHandler: ((e: MouseEvent) => void) | null = null
161
+
162
+ function openDropdown() {
163
+ if (props.disabled) return
164
+ dropdownVisible.value = true
165
+ if (props.lazy && !hasTreeDataProp()) fetchData()
166
+ nextTick(() => {
167
+ clickOutsideHandler = (e: MouseEvent) => {
168
+ if (rootRef.value && !rootRef.value.contains(e.target as Node)) {
169
+ closeDropdown()
170
+ }
171
+ }
172
+ document.addEventListener('click', clickOutsideHandler)
173
+ })
174
+ }
175
+
176
+ function closeDropdown() {
177
+ dropdownVisible.value = false
178
+ filterText.value = ''
179
+ if (clickOutsideHandler) {
180
+ document.removeEventListener('click', clickOutsideHandler)
181
+ clickOutsideHandler = null
182
+ }
183
+ }
184
+
185
+ function clearValue() {
186
+ emit('input', undefined)
187
+ }
188
+
189
+ function onNodeClick(data: TreeNode) {
190
+ const valueKey = props.valueField ?? 'value'
191
+ const val = data[valueKey] ?? data.value
192
+ emit('input', val)
193
+ closeDropdown()
194
+ }
195
+
196
+ async function fetchData() {
197
+ if (!props.api || hasTreeDataProp()) return
198
+ loading.value = true
199
+ try {
200
+ const res = await props.api(props.params)
201
+ const raw = Array.isArray(res)
202
+ ? res
203
+ : ((res as Record<string, unknown>)?.list as unknown[]) ??
204
+ ((res as Record<string, unknown>)?.data as unknown[]) ??
205
+ []
206
+ treeData.value = raw.map((item) => normalizeNode(item as Record<string, unknown>))
207
+ flatLabelMap.value = buildFlatLabelMap(treeData.value)
208
+ } finally {
209
+ loading.value = false
210
+ }
211
+ }
212
+
213
+ onMounted(() => {
214
+ if (hasTreeDataProp()) {
215
+ syncFromTreeDataProp()
216
+ } else if (!props.lazy) {
217
+ fetchData()
218
+ }
219
+ })
220
+
221
+ watch(() => props.treeData, () => {
222
+ if (hasTreeDataProp()) {
223
+ syncFromTreeDataProp()
224
+ } else {
225
+ treeData.value = []
226
+ flatLabelMap.value = {}
227
+ if (!props.lazy && props.api) fetchData()
228
+ }
229
+ }, { deep: true })
230
+
231
+ watch(() => props.api, () => {
232
+ if (hasTreeDataProp()) return
233
+ if (!props.lazy) fetchData()
234
+ else { treeData.value = []; flatLabelMap.value = {} }
235
+ }, { deep: true })
236
+
237
+ watch(() => props.params, () => {
238
+ if (hasTreeDataProp()) return
239
+ if (props.lazy) { treeData.value = []; flatLabelMap.value = {} }
240
+ else fetchData()
241
+ }, { deep: true })
242
+ </script>
243
+
244
+ <style scoped>
245
+ .ecp-tree-select {
246
+ position: relative;
247
+ width: 100%;
248
+ }
249
+ .ecp-tree-select__filter-inner {
250
+ margin-bottom: 8px;
251
+ }
252
+ .ecp-tree-select__dropdown {
253
+ position: absolute;
254
+ top: 100%;
255
+ left: 0;
256
+ right: 0;
257
+ max-height: 280px;
258
+ overflow: auto;
259
+ background: #fff;
260
+ border: 1px solid #dcdfe6;
261
+ border-radius: 4px;
262
+ margin-top: 4px;
263
+ z-index: 1000;
264
+ padding: 8px;
265
+ }
266
+ .ecp-tree-select__loading {
267
+ padding: 24px;
268
+ text-align: center;
269
+ color: #909399;
270
+ font-size: 14px;
271
+ }
272
+ </style>
@@ -35,7 +35,6 @@ export function useForm(props?: UseFormPropsReactive): [UseFormReturn['register'
35
35
  const register = (action: FormActionType) => {
36
36
  formAction.value = action
37
37
  const formProps = getFormProps()
38
- debugger
39
38
  if (formProps && Object.keys(formProps).length > 0) {
40
39
  action.setProps(formProps)
41
40
  }