@aspire-ui/element-component-pro 1.0.0

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.
@@ -0,0 +1,114 @@
1
+ import { ref, watch, unref, type Ref } from 'vue'
2
+ import type { ProFormSchema, ProFormProps, FormActionType } from '../types'
3
+
4
+ export interface UseFormProps extends ProFormProps {
5
+ schemas?: ProFormSchema[]
6
+ }
7
+
8
+ /** 支持 ref/computed 的 props(参考 Vben Admin:参数 props 内的值可以是 computed 或者 ref 类型) */
9
+ export type UseFormPropsReactive = UseFormProps | Ref<UseFormProps | undefined>
10
+
11
+ export interface UseFormReturn {
12
+ /** 用于 @register,接收表单实例的 formAction */
13
+ register: (formAction: FormActionType) => void
14
+ formAction: { value: FormActionType | null }
15
+ getFieldsValue: () => Record<string, unknown>
16
+ setFieldsValue: (values: Record<string, unknown>) => Promise<void>
17
+ resetFields: () => Promise<void>
18
+ validate: (nameList?: string[]) => Promise<unknown>
19
+ validateFields: (nameList?: string[]) => Promise<unknown>
20
+ submit: () => Promise<void>
21
+ scrollToField: (name: string, options?: import('../types').ScrollToFieldOptions) => Promise<void>
22
+ clearValidate: (name?: string | string[]) => void
23
+ updateSchema: (data: Partial<ProFormSchema> | Partial<ProFormSchema>[]) => Promise<void>
24
+ appendSchemaByField: (schema: ProFormSchema, prefixField?: string, first?: boolean) => Promise<void>
25
+ removeSchemaByField: (field: string | string[]) => Promise<void>
26
+ setProps: (props: Partial<ProFormProps>) => Promise<void>
27
+ }
28
+
29
+ export function useForm(props?: UseFormPropsReactive): [UseFormReturn['register'], Omit<UseFormReturn, 'register'>] {
30
+ const formAction = ref<FormActionType | null>(null)
31
+ const formPropsRef = ref<UseFormProps | undefined>(props ? unref(props as Ref<UseFormProps | undefined>) : undefined)
32
+
33
+ const getFormProps = () => (props ? unref(props as Ref<UseFormProps | undefined>) : undefined) as UseFormProps | undefined
34
+
35
+ const register = (action: FormActionType) => {
36
+ formAction.value = action
37
+ const formProps = getFormProps()
38
+ debugger
39
+ if (formProps && Object.keys(formProps).length > 0) {
40
+ action.setProps(formProps)
41
+ }
42
+ }
43
+
44
+ if (props) {
45
+ watch(
46
+ () => getFormProps(),
47
+ (formProps) => {
48
+ formPropsRef.value = formProps
49
+ if (formProps && formAction.value) {
50
+ formAction.value.setProps(formProps)
51
+ }
52
+ },
53
+ { deep: true }
54
+ )
55
+ }
56
+
57
+ const getFieldsValue = () => formAction.value?.getFieldsValue() ?? {}
58
+
59
+ const setFieldsValue = async (values: Record<string, unknown>) => {
60
+ await formAction.value?.setFieldsValue(values)
61
+ }
62
+
63
+ const resetFields = async () => {
64
+ await formAction.value?.resetFields()
65
+ }
66
+
67
+ const validate = (nameList?: string[]) =>
68
+ formAction.value?.validate(nameList) ?? Promise.resolve()
69
+
70
+ const validateFields = (nameList?: string[]) =>
71
+ formAction.value?.validateFields(nameList) ?? Promise.resolve()
72
+
73
+ const submit = () => formAction.value?.submit() ?? Promise.resolve()
74
+
75
+ const scrollToField = (name: string, options?: import('../types').ScrollToFieldOptions) =>
76
+ formAction.value?.scrollToField(name, options) ?? Promise.resolve()
77
+
78
+ const clearValidate = (name?: string | string[]) => {
79
+ formAction.value?.clearValidate(name)
80
+ }
81
+
82
+ const updateSchema = (data: Partial<ProFormSchema> | Partial<ProFormSchema>[]) =>
83
+ formAction.value?.updateSchema(data) ?? Promise.resolve()
84
+
85
+ const appendSchemaByField = (schema: ProFormSchema, prefixField?: string, first?: boolean) =>
86
+ formAction.value?.appendSchemaByField(schema, prefixField, first) ?? Promise.resolve()
87
+
88
+ const removeSchemaByField = (field: string | string[]) =>
89
+ formAction.value?.removeSchemaByField(field) ?? Promise.resolve()
90
+
91
+ const setProps = async (formProps: Partial<ProFormProps>) => {
92
+ formPropsRef.value = { ...formPropsRef.value, ...formProps }
93
+ await formAction.value?.setProps(formProps)
94
+ }
95
+
96
+ const result: UseFormReturn = {
97
+ register,
98
+ formAction,
99
+ getFieldsValue,
100
+ setFieldsValue,
101
+ resetFields,
102
+ validate,
103
+ validateFields,
104
+ submit,
105
+ scrollToField,
106
+ clearValidate,
107
+ updateSchema,
108
+ appendSchemaByField,
109
+ removeSchemaByField,
110
+ setProps,
111
+ }
112
+
113
+ return [register, result]
114
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { VueConstructor } from 'vue'
2
+ // import ProTable from './ProTable'
3
+ import ProForm, { ProFormItem, FormActions } from './ProForm'
4
+ // import ProCard from './ProCard'
5
+ // import ProDescriptions from './ProDescriptions'
6
+ import { useForm } from './ProForm/useForm'
7
+
8
+ export { ProForm, ProFormItem, FormActions, useForm }
9
+ export * from './types'
10
+
11
+ const components = [
12
+ // { name: 'ProTable', component: ProTable },
13
+ { name: 'ProForm', component: ProForm },
14
+ { name: 'ProFormItem', component: ProFormItem },
15
+ { name: 'FormActions', component: FormActions }
16
+ // { name: 'ProCard', component: ProCard },
17
+ // { name: 'ProDescriptions', component: ProDescriptions },
18
+ ]
19
+
20
+ export function install(Vue: VueConstructor) {
21
+ components.forEach(({ name, component }) => {
22
+ Vue.component(name, component)
23
+ })
24
+ }
25
+
26
+ export default {
27
+ install,
28
+ // ProTable,
29
+ ProForm,
30
+ // ProCard,
31
+ // ProDescriptions,
32
+ }
@@ -0,0 +1,5 @@
1
+ declare module '*.vue' {
2
+ import type { DefineComponent } from 'vue'
3
+ const component: DefineComponent<object, object, unknown>
4
+ export default component
5
+ }
@@ -0,0 +1,179 @@
1
+ import type { VNode } from 'vue'
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, { text: string; status?: string }>
27
+ /** 自定义渲染 */
28
+ customRender?: (params: { text: unknown; record: Record<string, unknown>; index: number }) => VNode | string
29
+ /** 表单项配置(用于搜索) */
30
+ fieldProps?: Record<string, unknown>
31
+ /** 表单项占位符 */
32
+ placeholder?: string
33
+ }
34
+
35
+ /** ProTable 工具栏配置 */
36
+ export interface ProTableToolBarConfig {
37
+ /** 是否显示刷新按钮 */
38
+ reload?: boolean
39
+ /** 是否显示密度切换 */
40
+ density?: boolean
41
+ /** 是否显示列设置 */
42
+ columnSetting?: boolean
43
+ /** 是否显示全屏 */
44
+ fullScreen?: boolean
45
+ }
46
+
47
+ /** 栅格配置 */
48
+ export interface ColEx {
49
+ span?: number
50
+ offset?: number
51
+ pull?: number
52
+ push?: number
53
+ xs?: number
54
+ sm?: number
55
+ md?: number
56
+ lg?: number
57
+ xl?: number
58
+ }
59
+
60
+ /** Render 回调参数 */
61
+ export interface RenderCallbackParams {
62
+ schema: ProFormSchema
63
+ values: Record<string, unknown>
64
+ model: Record<string, unknown>
65
+ field: string
66
+ }
67
+
68
+ /** 滚动到字段的配置 */
69
+ export interface ScrollToFieldOptions {
70
+ /** 滚动行为 */
71
+ behavior?: ScrollBehavior
72
+ /** 块级对齐 */
73
+ block?: ScrollLogicalPosition
74
+ /** 行内对齐 */
75
+ inline?: ScrollLogicalPosition
76
+ }
77
+
78
+ /** 表单操作类型(参考 Vben Admin FormActionType) */
79
+ export interface FormActionType {
80
+ getFieldsValue: () => Record<string, unknown>
81
+ setFieldsValue: (values: Record<string, unknown>) => Promise<void>
82
+ resetFields: () => Promise<void>
83
+ validate: (nameList?: string[]) => Promise<unknown>
84
+ validateFields: (nameList?: string[]) => Promise<unknown>
85
+ submit: () => Promise<void>
86
+ scrollToField: (name: string, options?: ScrollToFieldOptions) => Promise<void>
87
+ clearValidate: (name?: string | string[]) => void
88
+ updateSchema: (data: Partial<ProFormSchema> | Partial<ProFormSchema>[]) => Promise<void>
89
+ appendSchemaByField: (schema: ProFormSchema, prefixField?: string, first?: boolean) => Promise<void>
90
+ removeSchemaByField: (field: string | string[]) => Promise<void>
91
+ setProps: (props: Partial<ProFormProps>) => Promise<void>
92
+ }
93
+
94
+ /** 时间字段映射 [表单字段, [开始字段, 结束字段], 格式?] */
95
+ export type FieldMapToTime = [string, [string, string], string?]
96
+
97
+ /** 表单事件监听器 */
98
+ export type FormListeners = Record<string, ((...args: unknown[]) => unknown) | ((...args: unknown[]) => unknown)[]>
99
+
100
+ /** ProForm Props */
101
+ export interface ProFormProps {
102
+ schemas?: ProFormSchema[]
103
+ initialValues?: Record<string, unknown>
104
+ labelWidth?: string
105
+ labelPosition?: 'left' | 'right' | 'top'
106
+ gutter?: number
107
+ size?: 'medium' | 'small' | 'large'
108
+ disabled?: boolean
109
+ baseColProps?: ColEx
110
+ showSubmitButton?: boolean
111
+ showResetButton?: boolean
112
+ submitButtonText?: string
113
+ resetButtonText?: string
114
+ submitButtonIcon?: string
115
+ resetButtonIcon?: string
116
+ actionColOptions?: ColEx
117
+ showAdvancedButton?: boolean
118
+ autoAdvancedLine?: number
119
+ alwaysShowLines?: number
120
+ submitFunc?: () => Promise<void>
121
+ resetFunc?: () => Promise<void>
122
+ fieldMapToTime?: FieldMapToTime[]
123
+ /** 透传给 el-form 的事件监听器 */
124
+ formListeners?: FormListeners
125
+ }
126
+
127
+ /** ProForm 表单项配置 */
128
+ export interface ProFormSchema {
129
+ /** 字段名 */
130
+ field: string
131
+ /** 标签 */
132
+ label: string
133
+ /** 组件类型 */
134
+ component?: 'input' | 'select' | 'date-picker' | 'date-range' | 'input-number' | 'switch' | 'cascader' | 'checkbox' | 'radio'
135
+ /** 组件属性,支持函数 */
136
+ componentProps?: Record<string, unknown> | ((params: RenderCallbackParams & { formActionType?: FormActionType }) => Record<string, unknown>)
137
+ /** 占位符 */
138
+ placeholder?: string
139
+ /** 默认值 */
140
+ defaultValue?: unknown
141
+ /** 是否必填 */
142
+ required?: boolean
143
+ /** 校验规则 */
144
+ rules?: Array<Record<string, unknown>>
145
+ /** 栅格占位 */
146
+ colProps?: ColEx
147
+ /** 是否隐藏(静态) */
148
+ hidden?: boolean
149
+ /** 自定义渲染(替代 component) */
150
+ render?: (params: RenderCallbackParams) => VNode | VNode[] | string
151
+ /** 自定义 slot 名称,对应父组件的具名插槽 */
152
+ slot?: string
153
+ /** 动态显示,css 控制,不删除 dom */
154
+ show?: boolean | ((params: RenderCallbackParams) => boolean)
155
+ /** 动态显示,js 控制,会删除 dom */
156
+ ifShow?: boolean | ((params: RenderCallbackParams) => boolean)
157
+ /** 动态禁用 */
158
+ dynamicDisabled?: boolean | ((params: RenderCallbackParams) => boolean)
159
+ /** 动态校验规则 */
160
+ dynamicRules?: Array<Record<string, unknown>> | ((params: RenderCallbackParams) => Array<Record<string, unknown>>)
161
+ /** 依赖字段 */
162
+ dependencies?: string[]
163
+ /** 标签右侧温馨提示,支持 string 或 string[] */
164
+ helpMessage?: string | string[]
165
+ /** 温馨提示组件的 props */
166
+ helpComponentProps?: Record<string, unknown>
167
+ }
168
+
169
+ /** ProDescriptions 描述项配置 */
170
+ export interface ProDescriptionsSchema {
171
+ /** 标签 */
172
+ label: string
173
+ /** 数据字段 */
174
+ dataIndex: string
175
+ /** 自定义渲染 */
176
+ render?: (value: unknown, record?: Record<string, unknown>) => VNode | string
177
+ /** 栅格占位 */
178
+ span?: number
179
+ }
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />