UNPKG

11.2 kBTypeScriptView Raw
1import { RenderFunction, SetupContext, ComponentPublicInstance, Ref, ComputedRef } from 'vue'
2import { VXEComponent, VxeComponentBase, VxeEvent, SizeType, ValueOf, SlotVNodeType } from './component'
3import { VxeFormItemProps, VxeFormItemPropTypes } from './form-item'
4import { VxeGridConstructor } from './grid'
5
6/* eslint-disable no-use-before-define */
7
8/**
9 * 组件 - 表单
10 * @example import { VxeForm } from 'vxe-table'
11 */
12export const VxeForm: VXEComponent<VxeFormProps, VxeFormEventProps>
13/**
14 * 组件 - 表单
15 */
16export const Form: typeof VxeForm
17
18export type VxeFormInstance = ComponentPublicInstance<VxeFormProps, VxeFormConstructor>
19
20export interface VxeFormConstructor extends VxeComponentBase, VxeFormMethods {
21 props: VxeFormProps
22 context: SetupContext<VxeFormEmits>
23 reactData: FormReactData
24 internalData: FormInternalData
25 getRefMaps(): FormPrivateRef
26 getComputeMaps(): FormPrivateComputed
27 renderVN: RenderFunction
28
29 xegrid: VxeGridConstructor | null
30}
31
32export interface FormPrivateRef {
33 refElem: Ref<HTMLFormElement>
34}
35export interface VxeFormPrivateRef extends FormPrivateRef { }
36
37export interface FormPrivateComputed {
38 computeSize: ComputedRef<VxeFormPropTypes.Size>
39 computeValidOpts: ComputedRef<VxeFormPropTypes.ValidOpts>
40 computeTooltipOpts: ComputedRef<VxeFormPropTypes.TooltipOpts>
41}
42export interface VxeFormPrivateComputed extends FormPrivateComputed { }
43
44export interface FormReactData {
45 collapseAll: boolean
46 staticItems: any[]
47 formItems: VxeFormDefines.ItemInfo[]
48}
49
50export interface FormInternalData {
51 tooltipTimeout: any
52 tooltipStore: {
53 item: VxeFormDefines.ItemInfo | null
54 visible: boolean
55 }
56}
57
58export type VxeFormEmits = [
59 'update:collapseStatus',
60 'collapse',
61 'toggle-collapse',
62 'submit',
63 'submit-invalid',
64 'reset'
65]
66
67export namespace VxeFormPropTypes {
68 export type Size = SizeType
69 export type CollapseStatus = boolean
70 export type Loading = boolean
71 export type Data = any
72 export type Span = string | number
73 export type Align = 'left' | 'center' | 'right' | '' | null
74 export type TitleAlign = Align
75 export type TitleWidth = string | number
76 export type TitleColon = boolean
77 export type TitleAsterisk = boolean
78 export type TitleOverflow = boolean | 'ellipsis' | 'title' | 'tooltip' | '' | null
79 export type Vertical = boolean
80
81 interface ClassNameParams {
82 $form: VxeFormConstructor
83 data: any
84 items: VxeFormDefines.ItemInfo[]
85 }
86 export type ClassName = string | ((params: ClassNameParams) => string)
87
88 export type Items = VxeFormItemProps[]
89
90 export type Readonly = boolean
91
92 /**
93 * 校验规则配置项
94 */
95 export interface Rules {
96 [field: string]: VxeFormDefines.FormRule[]
97 }
98
99 export type PreventSubmit = boolean
100 export type ValidConfig = {
101 autoPos?: boolean
102 showMessage?: boolean
103 }
104 export interface ValidOpts extends ValidConfig { }
105
106 /**
107 * 提示信息配置项
108 */
109 export interface TooltipConfig {
110 theme?: 'dark' | 'light'
111 enterable?: boolean
112 leaveDelay?: number
113 leaveMethod?: (params: { $event: Event }) => boolean
114 }
115 export interface TooltipOpts extends TooltipConfig { }
116
117 export type CustomLayout = boolean
118}
119
120export type VxeFormProps<D = any> = {
121 size?: VxeFormPropTypes.Size
122 collapseStatus?: VxeFormPropTypes.CollapseStatus
123 loading?: VxeFormPropTypes.Loading
124 data?: D
125 span?: VxeFormPropTypes.Span
126 align?: VxeFormPropTypes.Align
127 titleAlign?: VxeFormPropTypes.TitleAlign
128 titleWidth?: VxeFormPropTypes.TitleWidth
129 titleColon?: VxeFormPropTypes.TitleColon
130 titleAsterisk?: VxeFormPropTypes.TitleAsterisk
131 titleOverflow?: VxeFormPropTypes.TitleOverflow
132 vertical?: VxeFormPropTypes.Vertical
133 className?: VxeFormPropTypes.ClassName
134 readonly?: VxeFormPropTypes.Readonly
135 items?: VxeFormPropTypes.Items
136 rules?: VxeFormPropTypes.Rules
137 preventSubmit?: VxeFormPropTypes.PreventSubmit
138 validConfig?: VxeFormPropTypes.ValidConfig
139 tooltipConfig?: VxeFormPropTypes.TooltipConfig
140 customLayout?: VxeFormPropTypes.CustomLayout
141}
142
143export interface FormMethods {
144 dispatchEvent(type: ValueOf<VxeFormEmits>, params: any, evnt: Event): void
145 /**
146 * 重置表单
147 */
148 reset(): Promise<any>
149 /**
150 * 对表单进行校验,参数为一个回调函数。该回调函数会在校验结束后被调用 callback(errMap)。若不传入回调函数,则会返回一个 promise
151 * @param callback 回调函数
152 */
153 validate(callback?: (errMap?: VxeFormDefines.ValidateErrorMapParams) => void): Promise<VxeFormDefines.ValidateErrorMapParams>
154 /**
155 * 对表单指定项进行校验,参数为一个回调函数。该回调函数会在校验结束后被调用 callback(errMap)。若不传入回调函数,则会返回一个 promise
156 * @param callback 回调函数
157 */
158 validateField(field: VxeFormItemPropTypes.Field | VxeFormItemPropTypes.Field[] | VxeFormDefines.ItemInfo | VxeFormDefines.ItemInfo[], callback?: (errMap?: VxeFormDefines.ValidateErrorMapParams) => void): Promise<VxeFormDefines.ValidateErrorMapParams>
159 /**
160 * 手动清除校验状态,如果指定 field 则清除指定的项,否则清除整个表单
161 * @param field 字段名
162 */
163 clearValidate(field?: VxeFormItemPropTypes.Field | VxeFormItemPropTypes.Field[] | VxeFormDefines.ItemInfo | VxeFormDefines.ItemInfo[]): Promise<any>
164 /**
165 * 更新项状态
166 * 当使用自定义渲染时可能会用到
167 * @param params 插槽对象
168 */
169 updateStatus(
170 params: {
171 field: VxeFormItemPropTypes.Field
172 },
173 itemValue?: any
174 ): void
175 /**
176 * 获取表单项列表
177 */
178 getItems(): VxeFormDefines.ItemInfo[]
179 /**
180 * 根据列的字段名获取表单项
181 * @param field 字段名
182 *
183 */
184 getItemByField(field: VxeFormItemPropTypes.Field): VxeFormDefines.ItemInfo | null
185 /**
186 * 关闭 tooltip 提示
187 */
188 closeTooltip(): Promise<any>
189 /**
190 * 手动切换折叠状态
191 */
192 toggleCollapse(): Promise<any>
193}
194export interface VxeFormMethods extends FormMethods { }
195
196export interface FormPrivateMethods {
197 callSlot<T>(slotFunc: ((params: T) => SlotVNodeType | SlotVNodeType[]) | string | null, params: T): SlotVNodeType[]
198 triggerItemEvent(evnt: Event | { type: string }, field: string, itemValue?: any): Promise<any>
199 toggleCollapseEvent(evnt: Event): void
200 triggerTitleTipEvent(evnt: MouseEvent, params: {
201 item: VxeFormDefines.ItemInfo
202 }): void
203 handleTitleTipLeaveEvent(): void
204}
205export interface VxeFormPrivateMethods extends FormPrivateMethods { }
206
207export namespace VxeFormDefines {
208 export class ItemInfo {
209 id: string
210
211 title: VxeFormItemPropTypes.Title
212 field: VxeFormItemPropTypes.Field
213 span: VxeFormItemPropTypes.Span
214 align: VxeFormItemPropTypes.Align
215 titleAlign: VxeFormItemPropTypes.TitleAlign
216 titleWidth: VxeFormItemPropTypes.TitleWidth
217 titleColon: VxeFormItemPropTypes.TitleColon
218 titleAsterisk: VxeFormItemPropTypes.TitleAsterisk
219 titlePrefix: VxeFormItemPropTypes.TitlePrefix
220 titleSuffix: VxeFormItemPropTypes.TitleSuffix
221 titleOverflow: VxeFormItemPropTypes.TitleOverflow
222 showTitle: VxeFormItemPropTypes.ShowTitle
223 vertical: VxeFormItemPropTypes.Vertical
224 resetValue: VxeFormItemPropTypes.ResetValue
225 visibleMethod: VxeFormItemPropTypes.VisibleMethod
226 visible: VxeFormItemPropTypes.Visible
227 folding: VxeFormItemPropTypes.Folding
228 collapseNode: VxeFormItemPropTypes.CollapseNode
229 className: VxeFormItemPropTypes.ClassName
230 contentClassName: VxeFormItemPropTypes.ContentClassName
231 contentStyle: VxeFormItemPropTypes.ContentStyle
232 titleClassName: VxeFormItemPropTypes.TitleClassName
233 titleStyle: VxeFormItemPropTypes.TitleStyle
234 readonly: VxeFormItemPropTypes.Readonly
235 itemRender: VxeFormItemPropTypes.ItemRender
236 // 渲染属性
237 showError: boolean
238 errRule: any
239 slots: VxeFormItemPropTypes.Slots
240 children: ItemInfo[]
241 }
242
243 export interface FormRule {
244 /**
245 * 是否必填
246 */
247 required?: boolean
248 /**
249 * 最小长度/值
250 */
251 min?: number
252 /**
253 * 最大长度/值
254 */
255 max?: number
256 /**
257 * 数据类型
258 */
259 type?: 'number' | 'string' | 'array' | '' | null
260 /**
261 * 使用正则表达式校验
262 */
263 pattern?: string | RegExp
264 /**
265 * 使用自定义校验函数,接收一个 Promise
266 * @param params 参数
267 */
268 validator?: string | ((params: RuleValidatorParams) => void | Error | Promise<any>)
269 /**
270 * 提示消息
271 */
272 content?: string
273 trigger?: 'change' | '' | null
274 maxWidth?: number
275 /**
276 * @deprecated 已废弃,请使用 content
277 */
278 message?: string
279 }
280
281 export interface RuleValidatorParams {
282 $form: VxeFormConstructor
283 itemValue: any
284 rule: VxeFormDefines.FormRule
285 rules: VxeFormDefines.FormRule[]
286 data: any
287 field: string
288 }
289 export interface ValidateErrorParams {
290 $form: VxeFormConstructor,
291 rule: VxeFormDefines.FormRule
292 data: any
293 field: string
294 /**
295 * @deprecated
296 */
297 property: string
298 }
299
300 export interface ProvideItemInfo {
301 itemConfig: ItemInfo
302 }
303
304 export interface ValidateErrorMapParams {
305 [field: string]: ValidateErrorParams[]
306 }
307
308 interface FormEventParams extends VxeEvent {
309 $form: VxeFormConstructor
310 }
311
312 interface FormBaseParams {
313 data: any
314 }
315
316 export interface CollapseParams extends FormBaseParams { }
317 export interface CollapseEventParams extends FormEventParams, CollapseParams { }
318
319 export interface SubmitParams extends FormBaseParams { }
320 export interface SubmitEventParams extends FormEventParams, SubmitParams { }
321
322 export interface SubmitInvalidParams extends FormBaseParams { }
323 export interface SubmitInvalidEventParams extends FormEventParams, SubmitInvalidParams { }
324
325 export interface ResetParams extends FormBaseParams { }
326 export interface ResetEventParams extends FormEventParams, ResetParams { }
327}
328
329export type VxeFormEventProps = {
330 onCollapse?: VxeFormEvents.Collapse
331 onSubmit?: VxeFormEvents.Submit
332 onSubmitInvalid?: VxeFormEvents.SubmitInvalid
333 onReset?: VxeFormEvents.Reset
334}
335
336export interface VxeFormListeners {
337 collapse?: VxeFormEvents.Collapse
338 submit?: VxeFormEvents.Submit
339 submitInvalid?: VxeFormEvents.SubmitInvalid
340 reset?: VxeFormEvents.Reset
341}
342
343export namespace VxeFormEvents {
344 export type Collapse = (params: VxeFormDefines.CollapseEventParams) => void
345 export type Submit = (params: VxeFormDefines.SubmitEventParams) => void
346 export type SubmitInvalid = (params: VxeFormDefines.SubmitInvalidEventParams) => void
347 export type Reset = (params: VxeFormDefines.ResetEventParams) => void
348}
349
350export interface VxeFormSlots {
351 /**
352 * 自定义插槽模板
353 */
354 [key: string]: ((params: {
355 [key: string]: any
356 }) => any) | undefined
357}