hongluan-ui
Version:
Hongluan Component Library for Vue 3
1 lines • 4.74 kB
Source Map (JSON)
{"version":3,"file":"form.mjs","sources":["../../../../packages/tokens/form.ts"],"sourcesContent":["import type { InjectionKey, SetupContext, UnwrapRef } from 'vue'\nimport type { RuleItem, ValidateError, ValidateFieldsError } from 'async-validator'\nimport type { MaybeRef } from '@vueuse/core'\nimport type { ComponentSize } from '@hongluan-ui/constants'\nimport type {\n FormProps,\n FormEmits,\n FormItemProp,\n FormItemProps,\n FormItemValidateState,\n} from '@hongluan-ui/components/form'\nimport type { Arrayable } from '@hongluan-ui/utils'\n\nexport interface FormItemRule extends RuleItem {\n trigger?: Arrayable<string>\n}\n\ntype Primitive = null | undefined | string | number | boolean | symbol | bigint\ntype BrowserNativeObject = Date | FileList | File | Blob | RegExp\n/**\n * Check whether it is tuple\n *\n * 检查是否为元组\n *\n * @example\n * IsTuple<[1, 2, 3]> => true\n * IsTuple<Array[number]> => false\n */\ntype IsTuple<T extends ReadonlyArray<any>> = number extends T['length']\n ? false\n : true\n/**\n * Array method key\n *\n * 数组方法键\n */\ntype ArrayMethodKey = keyof any[]\n/**\n * Tuple index key\n *\n * 元组下标键\n *\n * @example\n * TupleKey<[1, 2, 3]> => '0' | '1' | '2'\n */\ntype TupleKey<T extends ReadonlyArray<any>> = Exclude<keyof T, ArrayMethodKey>\n/**\n * Array index key\n *\n * 数组下标键\n */\ntype ArrayKey = number\n/**\n * Helper type for recursively constructing paths through a type\n *\n * 用于通过一个类型递归构建路径的辅助类型\n */\ntype PathImpl<K extends string | number, V> = V extends\n| Primitive\n| BrowserNativeObject\n ? `${K}`\n : `${K}` | `${K}.${Path<V>}`\n/**\n * Type which collects all paths through a type\n *\n * 通过一个类型收集所有路径的类型\n *\n * @see {@link FieldPath}\n */\ntype Path<T> = T extends ReadonlyArray<infer V>\n ? IsTuple<T> extends true\n ? {\n [K in TupleKey<T>]-?: PathImpl<Exclude<K, symbol>, T[K]>\n }[TupleKey<T>] // tuple\n : PathImpl<ArrayKey, V> // array\n : {\n [K in keyof T]-?: PathImpl<Exclude<K, symbol>, T[K]>\n }[keyof T] // object\n/**\n * Type which collects all paths through a type\n *\n * 通过一个类型收集所有路径的类型\n *\n * @example\n * FieldPath<{ 1: number; a: number; b: string; c: { d: number; e: string }; f: [{ value: string }]; g: { value: string }[]; h: Date; i: FileList; j: File; k: Blob; l: RegExp }> => '1' | 'a' | 'b' | 'c' | 'f' | 'g' | 'c.d' | 'c.e' | 'f.0' | 'f.0.value' | 'g.number' | 'g.number.value' | 'h' | 'i' | 'j' | 'k' | 'l'\n */\ntype FieldPath<T> = T extends object ? Path<T> : never\nexport type FormRules<\n T extends MaybeRef<Record<string, any> | string> = string\n> = Partial<\nRecord<\nUnwrapRef<T> extends string ? UnwrapRef<T> : FieldPath<UnwrapRef<T>>,\nArrayable<FormItemRule>\n>\n>\n\n\nexport type FormValidationResult = Promise<boolean>\nexport type FormValidateCallback = (\n isValid: boolean,\n invalidFields?: ValidateFieldsError\n) => Promise<void> | void\nexport interface FormValidateFailure {\n errors: ValidateError[] | null\n fields: ValidateFieldsError\n}\n\nexport type FormContext = FormProps & {\n emit: SetupContext<FormEmits>['emit']\n\n getField: (prop: string) => FormItemContext | undefined\n addField: (field: FormItemContext) => void\n removeField: (field: FormItemContext) => void\n resetFields: (props?: Arrayable<FormItemProp>) => void\n clearValidate: (props?: Arrayable<FormItemProp>) => void\n validateField: (\n props?: Arrayable<FormItemProp>,\n callback?: FormValidateCallback\n ) => FormValidationResult\n}\n\nexport interface FormItemContext extends FormItemProps {\n $el: { $el: HTMLElement | undefined; }\n size: ComponentSize\n validateState: FormItemValidateState\n isGroup: boolean\n labelId: string\n inputIds: string[]\n fieldValue: any\n addInputId: (id: string) => void\n removeInputId: (id: string) => void\n validate: (trigger: string, callback?: FormValidateCallback) => FormValidationResult\n resetField(): void\n clearValidate(): void\n}\n\n// TODO: change it to symbol\nexport const formContextKey: InjectionKey<FormContext> = Symbol('Form')\n\nexport const formItemContextKey: InjectionKey<FormItemContext> = Symbol('FormItem')\n\nexport const FormEvents = {\n addField: 'form.addField',\n removeField: 'form.removeField',\n} as const\n\nexport const FormItemEvents = {\n blur: 'blur',\n change: 'change',\n} as const\n"],"names":[],"mappings":"AAAY,MAAC,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE;AACjC,MAAC,kBAAkB,GAAG,MAAM,CAAC,UAAU,EAAE;AACzC,MAAC,UAAU,GAAG;AAC1B,EAAE,QAAQ,EAAE,eAAe;AAC3B,EAAE,WAAW,EAAE,kBAAkB;AACjC,EAAE;AACU,MAAC,cAAc,GAAG;AAC9B,EAAE,IAAI,EAAE,MAAM;AACd,EAAE,MAAM,EAAE,QAAQ;AAClB;;;;"}