@antsoo-lib/core 2.0.5 → 3.0.1

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/core.css +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.js +6 -5
  5. package/dist/types/BaseSearch/index.d.ts +1 -1
  6. package/dist/types/BaseTable/index.d.ts +4 -480
  7. package/dist/types/Form/CoreForm.d.ts +82 -0
  8. package/dist/types/SSelectPage/index.d.ts +102 -0
  9. package/package.json +5 -5
  10. package/src/BaseSearch/index.vue +371 -371
  11. package/src/BaseTable/index.vue +910 -910
  12. package/src/Form/CoreForm.vue +782 -782
  13. package/src/Form/types.ts +86 -86
  14. package/src/SSelectPage/index.vue +607 -607
  15. package/src/index.ts +17 -17
  16. package/src/render/AreaCascader.tsx +64 -64
  17. package/src/render/AutoComplete.tsx +101 -101
  18. package/src/render/Button.tsx +62 -62
  19. package/src/render/Cascader.tsx +45 -45
  20. package/src/render/Checkbox.tsx +65 -65
  21. package/src/render/CheckboxGroup.tsx +57 -57
  22. package/src/render/Custom.tsx +19 -19
  23. package/src/render/DatePicker.tsx +83 -83
  24. package/src/render/Input.tsx +140 -140
  25. package/src/render/InputGroup.tsx +115 -115
  26. package/src/render/InputNumber.tsx +205 -205
  27. package/src/render/InputPassword.tsx +81 -81
  28. package/src/render/InputRange.tsx +154 -154
  29. package/src/render/RadioGroup.tsx +63 -63
  30. package/src/render/Select.tsx +96 -96
  31. package/src/render/SselectPage.tsx +107 -107
  32. package/src/render/Switch.tsx +60 -60
  33. package/src/render/Tree.tsx +136 -136
  34. package/src/render/TreeSelect.tsx +81 -81
  35. package/src/render/Upload.tsx +91 -91
  36. package/src/render/helper.tsx +221 -221
  37. package/src/render/index.ts +108 -108
  38. package/src/render/registry.ts +20 -20
  39. package/src/render/state.ts +37 -37
  40. package/src/render/types.ts +567 -567
  41. package/src/utils/attrMapping.ts +106 -106
  42. package/vite.config.ts +61 -61
  43. package/.turbo/turbo-build.log +0 -40
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- import BaseSearch from './BaseSearch/index.vue'
2
- import BaseTable from './BaseTable/index.vue'
3
- import CoreForm from './Form/CoreForm.vue'
4
- import SSelectPage from './SSelectPage/index.vue'
5
- import { registerRenderer } from './render'
6
- import type { Renderer } from './render'
7
-
8
- export { BaseSearch, BaseTable, CoreForm, registerRenderer, SSelectPage }
9
- export type { Renderer }
10
-
11
- export const version = '0.0.0'
12
-
13
- export * from './render/registry'
14
- export * from './render/types'
15
- export * from './render/state'
16
- export * from './render'
17
- export * from './Form/types'
1
+ import BaseSearch from './BaseSearch/index.vue'
2
+ import BaseTable from './BaseTable/index.vue'
3
+ import CoreForm from './Form/CoreForm.vue'
4
+ import SSelectPage from './SSelectPage/index.vue'
5
+ import { registerRenderer } from './render'
6
+ import type { Renderer } from './render'
7
+
8
+ export { BaseSearch, BaseTable, CoreForm, registerRenderer, SSelectPage }
9
+ export type { Renderer }
10
+
11
+ export const version = '0.0.0'
12
+
13
+ export * from './render/registry'
14
+ export * from './render/types'
15
+ export * from './render/state'
16
+ export * from './render'
17
+ export * from './Form/types'
@@ -1,64 +1,64 @@
1
- import type { AnyObject } from '@antsoo-lib/shared'
2
- import { isFunction } from '@antsoo-lib/utils'
3
-
4
- import { markRaw } from 'vue'
5
- import type { VNode } from 'vue'
6
-
7
- import { getRegisteredComponent } from './registry'
8
- import type { ToolbarState } from './state'
9
- import type { AreaCascaderProps, AreaCascaderToolbarItem } from './types'
10
-
11
- // 渲染地区级联选择器
12
- export function renderAreaCascader(
13
- config: AreaCascaderToolbarItem,
14
- toolbarState: ToolbarState,
15
- allValues?: AnyObject,
16
- ): VNode {
17
- const { key = '', props = {}, events = {}, disabled = false } = config
18
-
19
- // 处理动态禁用逻辑
20
- const isDisabled = isFunction(disabled)
21
- ? disabled(toolbarState.getAllValues(), toolbarState)
22
- : disabled
23
- const finalProps: AreaCascaderProps = { ...props, disabled: isDisabled }
24
-
25
- // 计算安全的当前值(优先取 toolbarState,再兜底 props.value),不做任何业务级聚合
26
- const safeValue =
27
- toolbarState.getValue(key) !== undefined ? toolbarState.getValue(key) : props.value
28
-
29
- // 处理事件,统一注入上下文,保持中立
30
- const processedEvents: AnyObject = {}
31
- Object.keys(events).forEach((eventName) => {
32
- const handler = events[eventName as keyof typeof events]
33
- if (!handler || !isFunction(handler)) return
34
- const normalizedName = eventName.startsWith('on')
35
- ? eventName
36
- : `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`
37
- processedEvents[normalizedName] = (...args: any[]) => {
38
- handler(...args, allValues ?? toolbarState.getAllValues(), toolbarState)
39
- }
40
- })
41
-
42
- // 默认的 v-model 更新(仅当存在 key 时写入状态)
43
- if (!processedEvents['onUpdate:value']) {
44
- processedEvents['onUpdate:value'] = (val: any) => {
45
- if (key) toolbarState.setValue(key, val)
46
- }
47
- }
48
-
49
- const AreaCascaderComp = getRegisteredComponent('AreaCascader')
50
- if (!AreaCascaderComp) {
51
- return <span style={{ color: 'red' }}>AreaCascader component missing</span>
52
- }
53
-
54
- const Comp = markRaw(AreaCascaderComp) as any
55
- return (
56
- <Comp
57
- key={key}
58
- value={safeValue}
59
- {...finalProps}
60
- {...(config.attr ? { attr: config.attr } : {})}
61
- {...processedEvents}
62
- />
63
- )
64
- }
1
+ import type { AnyObject } from '@antsoo-lib/shared'
2
+ import { isFunction } from '@antsoo-lib/utils'
3
+
4
+ import { markRaw } from 'vue'
5
+ import type { VNode } from 'vue'
6
+
7
+ import { getRegisteredComponent } from './registry'
8
+ import type { ToolbarState } from './state'
9
+ import type { AreaCascaderProps, AreaCascaderToolbarItem } from './types'
10
+
11
+ // 渲染地区级联选择器
12
+ export function renderAreaCascader(
13
+ config: AreaCascaderToolbarItem,
14
+ toolbarState: ToolbarState,
15
+ allValues?: AnyObject,
16
+ ): VNode {
17
+ const { key = '', props = {}, events = {}, disabled = false } = config
18
+
19
+ // 处理动态禁用逻辑
20
+ const isDisabled = isFunction(disabled)
21
+ ? disabled(toolbarState.getAllValues(), toolbarState)
22
+ : disabled
23
+ const finalProps: AreaCascaderProps = { ...props, disabled: isDisabled }
24
+
25
+ // 计算安全的当前值(优先取 toolbarState,再兜底 props.value),不做任何业务级聚合
26
+ const safeValue =
27
+ toolbarState.getValue(key) !== undefined ? toolbarState.getValue(key) : props.value
28
+
29
+ // 处理事件,统一注入上下文,保持中立
30
+ const processedEvents: AnyObject = {}
31
+ Object.keys(events).forEach((eventName) => {
32
+ const handler = events[eventName as keyof typeof events]
33
+ if (!handler || !isFunction(handler)) return
34
+ const normalizedName = eventName.startsWith('on')
35
+ ? eventName
36
+ : `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`
37
+ processedEvents[normalizedName] = (...args: any[]) => {
38
+ handler(...args, allValues ?? toolbarState.getAllValues(), toolbarState)
39
+ }
40
+ })
41
+
42
+ // 默认的 v-model 更新(仅当存在 key 时写入状态)
43
+ if (!processedEvents['onUpdate:value']) {
44
+ processedEvents['onUpdate:value'] = (val: any) => {
45
+ if (key) toolbarState.setValue(key, val)
46
+ }
47
+ }
48
+
49
+ const AreaCascaderComp = getRegisteredComponent('AreaCascader')
50
+ if (!AreaCascaderComp) {
51
+ return <span style={{ color: 'red' }}>AreaCascader component missing</span>
52
+ }
53
+
54
+ const Comp = markRaw(AreaCascaderComp) as any
55
+ return (
56
+ <Comp
57
+ key={key}
58
+ value={safeValue}
59
+ {...finalProps}
60
+ {...(config.attr ? { attr: config.attr } : {})}
61
+ {...processedEvents}
62
+ />
63
+ )
64
+ }
@@ -1,101 +1,101 @@
1
- import type { VoidFunction } from '@antsoo-lib/shared'
2
- import { isFunction } from '@antsoo-lib/utils'
3
-
4
- import { computed, markRaw, nextTick } from 'vue'
5
- import type { ComponentPublicInstance, VNode } from 'vue'
6
-
7
- import { getRegisteredComponent } from './registry'
8
- import type { ToolbarState } from './state'
9
- import type { AutoCompleteToolbarItem, ToolbarItem } from './types'
10
-
11
- export function renderAutoComplete(item: ToolbarItem, toolbarState: ToolbarState): VNode {
12
- const { key, events = {} } = item
13
- const { props = {}, disabled = false } = item as AutoCompleteToolbarItem
14
-
15
- const isDisabled = isFunction(disabled)
16
- ? disabled(toolbarState.getAllValues(), toolbarState)
17
- : disabled
18
-
19
- // 处理事件,注入工具栏状态
20
- const processedEvents: Record<string, VoidFunction> = {}
21
- Object.keys(events).forEach((eventName) => {
22
- processedEvents[eventName] = (...args: any[]) => {
23
- // 对于 SselectPage,先更新状态,再调用外部事件处理函数
24
- if (eventName === 'onUpdate:value' || eventName === 'onChange') {
25
- const value = args[0] // SselectPage 的第一个参数是选中的值
26
- // 立即更新工具栏状态,确保外部事件处理函数能获取到最新值
27
- toolbarState.setValue(key, value)
28
-
29
- // 对于 onChange 事件,使用 nextTick 确保在下一个 tick 中执行外部处理函数
30
- if (eventName === 'onChange') {
31
- nextTick(() => {
32
- events[eventName]?.(...args, toolbarState.getAllValues(), toolbarState)
33
- })
34
- return // 提前返回,避免重复执行
35
- }
36
- }
37
-
38
- // 调用原始事件处理函数,传入完整的上下文
39
- events[eventName]?.(...args, toolbarState.getAllValues(), toolbarState)
40
- }
41
- })
42
-
43
- // 如果没有设置 onChange 事件,添加默认的状态更新
44
- if (!processedEvents['onUpdate:value'] && !processedEvents.onChange) {
45
- processedEvents['onUpdate:value'] = (value: any) => {
46
- toolbarState.setValue(key, value)
47
- }
48
- }
49
-
50
- // 处理 extraParams 的响应式更新
51
- // 如果 props.extraParams 是函数,则动态计算
52
- const computedProps = computed(() => {
53
- const allValues = toolbarState.getAllValues()
54
- const dynamicProps: any = { ...props, disabled: isDisabled }
55
-
56
- // 动态计算 extraParams
57
- if (isFunction(dynamicProps.extraParams)) {
58
- dynamicProps.extraParams = dynamicProps.extraParams(allValues, toolbarState)
59
- }
60
-
61
- return dynamicProps
62
- })
63
-
64
- // 如果需要自动聚焦,添加 ref 处理
65
- if (props.autoFocus) {
66
- processedEvents.ref = (el: Element | ComponentPublicInstance | null) => {
67
- const component = el as ComponentPublicInstance
68
- if (component) {
69
- // 使用 nextTick 确保 DOM 已经渲染完成
70
- nextTick(() => {
71
- try {
72
- ;(component as any).$refs.selectRef?.focus?.()
73
- } catch {
74
- // 忽略焦点设置失败的错误
75
- }
76
- })
77
- }
78
- }
79
- }
80
-
81
- // 计算安全的默认值
82
- const rawVal = toolbarState.getValue(key)
83
- const propVal = props.value
84
- const safeValue =
85
- rawVal !== undefined && rawVal !== null
86
- ? rawVal
87
- : propVal !== undefined && propVal !== null
88
- ? propVal
89
- : undefined
90
-
91
- const AutoCompleteComponent = getRegisteredComponent('AutoComplete')
92
- if (!AutoCompleteComponent) {
93
- console.warn('BaseTable: AutoComplete component is not registered.')
94
- return <span style={{ color: 'red' }}>AutoComplete component missing</span>
95
- }
96
-
97
- const Comp = markRaw(AutoCompleteComponent) as any
98
- return (
99
- <Comp key={key} value={safeValue} allowClear {...computedProps.value} {...processedEvents} />
100
- )
101
- }
1
+ import type { VoidFunction } from '@antsoo-lib/shared'
2
+ import { isFunction } from '@antsoo-lib/utils'
3
+
4
+ import { computed, markRaw, nextTick } from 'vue'
5
+ import type { ComponentPublicInstance, VNode } from 'vue'
6
+
7
+ import { getRegisteredComponent } from './registry'
8
+ import type { ToolbarState } from './state'
9
+ import type { AutoCompleteToolbarItem, ToolbarItem } from './types'
10
+
11
+ export function renderAutoComplete(item: ToolbarItem, toolbarState: ToolbarState): VNode {
12
+ const { key, events = {} } = item
13
+ const { props = {}, disabled = false } = item as AutoCompleteToolbarItem
14
+
15
+ const isDisabled = isFunction(disabled)
16
+ ? disabled(toolbarState.getAllValues(), toolbarState)
17
+ : disabled
18
+
19
+ // 处理事件,注入工具栏状态
20
+ const processedEvents: Record<string, VoidFunction> = {}
21
+ Object.keys(events).forEach((eventName) => {
22
+ processedEvents[eventName] = (...args: any[]) => {
23
+ // 对于 SselectPage,先更新状态,再调用外部事件处理函数
24
+ if (eventName === 'onUpdate:value' || eventName === 'onChange') {
25
+ const value = args[0] // SselectPage 的第一个参数是选中的值
26
+ // 立即更新工具栏状态,确保外部事件处理函数能获取到最新值
27
+ toolbarState.setValue(key, value)
28
+
29
+ // 对于 onChange 事件,使用 nextTick 确保在下一个 tick 中执行外部处理函数
30
+ if (eventName === 'onChange') {
31
+ nextTick(() => {
32
+ events[eventName]?.(...args, toolbarState.getAllValues(), toolbarState)
33
+ })
34
+ return // 提前返回,避免重复执行
35
+ }
36
+ }
37
+
38
+ // 调用原始事件处理函数,传入完整的上下文
39
+ events[eventName]?.(...args, toolbarState.getAllValues(), toolbarState)
40
+ }
41
+ })
42
+
43
+ // 如果没有设置 onChange 事件,添加默认的状态更新
44
+ if (!processedEvents['onUpdate:value'] && !processedEvents.onChange) {
45
+ processedEvents['onUpdate:value'] = (value: any) => {
46
+ toolbarState.setValue(key, value)
47
+ }
48
+ }
49
+
50
+ // 处理 extraParams 的响应式更新
51
+ // 如果 props.extraParams 是函数,则动态计算
52
+ const computedProps = computed(() => {
53
+ const allValues = toolbarState.getAllValues()
54
+ const dynamicProps: any = { ...props, disabled: isDisabled }
55
+
56
+ // 动态计算 extraParams
57
+ if (isFunction(dynamicProps.extraParams)) {
58
+ dynamicProps.extraParams = dynamicProps.extraParams(allValues, toolbarState)
59
+ }
60
+
61
+ return dynamicProps
62
+ })
63
+
64
+ // 如果需要自动聚焦,添加 ref 处理
65
+ if (props.autoFocus) {
66
+ processedEvents.ref = (el: Element | ComponentPublicInstance | null) => {
67
+ const component = el as ComponentPublicInstance
68
+ if (component) {
69
+ // 使用 nextTick 确保 DOM 已经渲染完成
70
+ nextTick(() => {
71
+ try {
72
+ ;(component as any).$refs.selectRef?.focus?.()
73
+ } catch {
74
+ // 忽略焦点设置失败的错误
75
+ }
76
+ })
77
+ }
78
+ }
79
+ }
80
+
81
+ // 计算安全的默认值
82
+ const rawVal = toolbarState.getValue(key)
83
+ const propVal = props.value
84
+ const safeValue =
85
+ rawVal !== undefined && rawVal !== null
86
+ ? rawVal
87
+ : propVal !== undefined && propVal !== null
88
+ ? propVal
89
+ : undefined
90
+
91
+ const AutoCompleteComponent = getRegisteredComponent('AutoComplete')
92
+ if (!AutoCompleteComponent) {
93
+ console.warn('BaseTable: AutoComplete component is not registered.')
94
+ return <span style={{ color: 'red' }}>AutoComplete component missing</span>
95
+ }
96
+
97
+ const Comp = markRaw(AutoCompleteComponent) as any
98
+ return (
99
+ <Comp key={key} value={safeValue} allowClear {...computedProps.value} {...processedEvents} />
100
+ )
101
+ }
@@ -1,62 +1,62 @@
1
- import { Button } from '@antsoo-lib/components'
2
- import type { VoidFunction } from '@antsoo-lib/shared'
3
-
4
- import { reactive } from 'vue'
5
- import type { VNode } from 'vue'
6
-
7
- import type { ToolbarState } from './state'
8
- import type { ButtonToolbarItem, ToolbarItem } from './types'
9
-
10
- // 按钮渲染函数
11
- export function renderButton(item: ToolbarItem, toolbarState: ToolbarState): VNode {
12
- const buttonItem = item as ButtonToolbarItem
13
- const { key, props = {}, events = {}, disabled = false } = buttonItem
14
- const reactiveProps = reactive({ ...props })
15
-
16
- const isDisabled =
17
- typeof disabled === 'function'
18
- ? disabled(toolbarState.getAllValues(), toolbarState)
19
- : !!disabled
20
-
21
- // 为每个按钮创建独立的 loading 状态键
22
- const loadingKey = `${key}_loading`
23
-
24
- // 初始化按钮的 loading 状态(如果还未设置)
25
- if (toolbarState.getValue(loadingKey) === undefined) {
26
- toolbarState.setValue(loadingKey, false)
27
- }
28
-
29
- // 创建 loading 控制对象,传入事件方法中
30
- const loadingControl = {
31
- setLoading: (loading: boolean) => {
32
- toolbarState.setValue(loadingKey, loading)
33
- },
34
- getLoading: () => {
35
- return toolbarState.getValue(loadingKey) || false
36
- },
37
- }
38
-
39
- // 处理事件,注入工具栏状态和 loading 控制
40
- const processedEvents: Record<string, VoidFunction> = {}
41
- Object.keys(events).forEach((eventName) => {
42
- const eventHandler = events[eventName]
43
- if (eventHandler) {
44
- processedEvents[eventName] = (...args: any[]) => {
45
- // 调用原始事件处理函数,并传入 loading 控制对象
46
- eventHandler(loadingControl, toolbarState.getAllValues(), toolbarState, ...args)
47
- }
48
- }
49
- })
50
-
51
- const finalProps = {
52
- ...reactiveProps,
53
- disabled: isDisabled || reactiveProps.disabled || false,
54
- loading: toolbarState.getValue(loadingKey) || reactiveProps.loading || false,
55
- }
56
-
57
- return (
58
- <Button key={key} {...finalProps} {...processedEvents}>
59
- {item.label || '按钮'}
60
- </Button>
61
- )
62
- }
1
+ import { Button } from '@antsoo-lib/components'
2
+ import type { VoidFunction } from '@antsoo-lib/shared'
3
+
4
+ import { reactive } from 'vue'
5
+ import type { VNode } from 'vue'
6
+
7
+ import type { ToolbarState } from './state'
8
+ import type { ButtonToolbarItem, ToolbarItem } from './types'
9
+
10
+ // 按钮渲染函数
11
+ export function renderButton(item: ToolbarItem, toolbarState: ToolbarState): VNode {
12
+ const buttonItem = item as ButtonToolbarItem
13
+ const { key, props = {}, events = {}, disabled = false } = buttonItem
14
+ const reactiveProps = reactive({ ...props })
15
+
16
+ const isDisabled =
17
+ typeof disabled === 'function'
18
+ ? disabled(toolbarState.getAllValues(), toolbarState)
19
+ : !!disabled
20
+
21
+ // 为每个按钮创建独立的 loading 状态键
22
+ const loadingKey = `${key}_loading`
23
+
24
+ // 初始化按钮的 loading 状态(如果还未设置)
25
+ if (toolbarState.getValue(loadingKey) === undefined) {
26
+ toolbarState.setValue(loadingKey, false)
27
+ }
28
+
29
+ // 创建 loading 控制对象,传入事件方法中
30
+ const loadingControl = {
31
+ setLoading: (loading: boolean) => {
32
+ toolbarState.setValue(loadingKey, loading)
33
+ },
34
+ getLoading: () => {
35
+ return toolbarState.getValue(loadingKey) || false
36
+ },
37
+ }
38
+
39
+ // 处理事件,注入工具栏状态和 loading 控制
40
+ const processedEvents: Record<string, VoidFunction> = {}
41
+ Object.keys(events).forEach((eventName) => {
42
+ const eventHandler = events[eventName]
43
+ if (eventHandler) {
44
+ processedEvents[eventName] = (...args: any[]) => {
45
+ // 调用原始事件处理函数,并传入 loading 控制对象
46
+ eventHandler(loadingControl, toolbarState.getAllValues(), toolbarState, ...args)
47
+ }
48
+ }
49
+ })
50
+
51
+ const finalProps = {
52
+ ...reactiveProps,
53
+ disabled: isDisabled || reactiveProps.disabled || false,
54
+ loading: toolbarState.getValue(loadingKey) || reactiveProps.loading || false,
55
+ }
56
+
57
+ return (
58
+ <Button key={key} {...finalProps} {...processedEvents}>
59
+ {item.label || '按钮'}
60
+ </Button>
61
+ )
62
+ }
@@ -1,45 +1,45 @@
1
- import type { AnyObject } from '@antsoo-lib/shared'
2
- import { isFunction } from '@antsoo-lib/utils'
3
-
4
- import { markRaw } from 'vue'
5
- import type { VNode } from 'vue'
6
-
7
- import { getRegisteredComponent } from './registry'
8
- import type { ToolbarState } from './state'
9
- import type { CascaderToolbarItem } from './types'
10
-
11
- // 渲染级联选择器
12
- export function renderCascader(
13
- config: CascaderToolbarItem,
14
- toolbarState: ToolbarState,
15
- allValues: AnyObject,
16
- ): VNode {
17
- const { key = '', props = {}, events = {} } = config
18
- const value = toolbarState.getValue(key)
19
-
20
- // 处理事件
21
- const cascaderEvents: AnyObject = {}
22
- Object.keys(events).forEach((eventName) => {
23
- const handler = events[eventName as keyof typeof events]
24
- if (handler && isFunction(handler)) {
25
- cascaderEvents[eventName] = (...args: any[]) => {
26
- handler(...args, allValues, toolbarState)
27
- }
28
- }
29
- })
30
-
31
- // 特殊处理 update:value 事件
32
- cascaderEvents['update:value'] = (val: any) => {
33
- toolbarState.setValue(key, val)
34
- events['onUpdate:value']?.(val, allValues, toolbarState)
35
- }
36
-
37
- // 渲染组件
38
- const CascaderComp = getRegisteredComponent('Cascader')
39
- if (!CascaderComp) {
40
- return <span style={{ color: 'red' }}>Cascader component missing</span>
41
- }
42
-
43
- const Comp = markRaw(CascaderComp) as any
44
- return <Comp value={value} {...props} {...cascaderEvents} />
45
- }
1
+ import type { AnyObject } from '@antsoo-lib/shared'
2
+ import { isFunction } from '@antsoo-lib/utils'
3
+
4
+ import { markRaw } from 'vue'
5
+ import type { VNode } from 'vue'
6
+
7
+ import { getRegisteredComponent } from './registry'
8
+ import type { ToolbarState } from './state'
9
+ import type { CascaderToolbarItem } from './types'
10
+
11
+ // 渲染级联选择器
12
+ export function renderCascader(
13
+ config: CascaderToolbarItem,
14
+ toolbarState: ToolbarState,
15
+ allValues: AnyObject,
16
+ ): VNode {
17
+ const { key = '', props = {}, events = {} } = config
18
+ const value = toolbarState.getValue(key)
19
+
20
+ // 处理事件
21
+ const cascaderEvents: AnyObject = {}
22
+ Object.keys(events).forEach((eventName) => {
23
+ const handler = events[eventName as keyof typeof events]
24
+ if (handler && isFunction(handler)) {
25
+ cascaderEvents[eventName] = (...args: any[]) => {
26
+ handler(...args, allValues, toolbarState)
27
+ }
28
+ }
29
+ })
30
+
31
+ // 特殊处理 update:value 事件
32
+ cascaderEvents['update:value'] = (val: any) => {
33
+ toolbarState.setValue(key, val)
34
+ events['onUpdate:value']?.(val, allValues, toolbarState)
35
+ }
36
+
37
+ // 渲染组件
38
+ const CascaderComp = getRegisteredComponent('Cascader')
39
+ if (!CascaderComp) {
40
+ return <span style={{ color: 'red' }}>Cascader component missing</span>
41
+ }
42
+
43
+ const Comp = markRaw(CascaderComp) as any
44
+ return <Comp value={value} {...props} {...cascaderEvents} />
45
+ }