2021-06-26 09:35:40 +08:00
|
|
|
import type { InjectionKey, ComputedRef } from 'vue';
|
|
|
|
import { inject, provide, computed } from 'vue';
|
|
|
|
import type { ColProps } from '../grid';
|
|
|
|
import type { RequiredMark, ValidationRule } from './Form';
|
|
|
|
import type { ValidateStatus, FieldExpose } from './FormItem';
|
2021-12-23 22:00:14 +08:00
|
|
|
import type { FormLabelAlign, ValidateMessages } from './interface';
|
|
|
|
import { defaultValidateMessages } from './utils/messages';
|
2021-06-07 17:35:03 +08:00
|
|
|
|
|
|
|
export interface FormContextProps {
|
|
|
|
model?: ComputedRef<any>;
|
|
|
|
vertical: ComputedRef<boolean>;
|
|
|
|
name?: ComputedRef<string>;
|
|
|
|
colon?: ComputedRef<boolean>;
|
|
|
|
labelAlign?: ComputedRef<FormLabelAlign>;
|
2022-03-12 09:56:32 +08:00
|
|
|
labelWrap?: ComputedRef<boolean>;
|
2021-06-07 17:35:03 +08:00
|
|
|
labelCol?: ComputedRef<ColProps>;
|
|
|
|
wrapperCol?: ComputedRef<ColProps>;
|
|
|
|
requiredMark?: ComputedRef<RequiredMark>;
|
|
|
|
//itemRef: (name: (string | number)[]) => (node: React.ReactElement) => void;
|
|
|
|
addField: (eventKey: string, field: FieldExpose) => void;
|
|
|
|
removeField: (eventKey: string) => void;
|
|
|
|
validateTrigger?: ComputedRef<string | string[]>;
|
|
|
|
rules?: ComputedRef<{ [k: string]: ValidationRule[] | ValidationRule }>;
|
2021-10-29 10:22:35 +08:00
|
|
|
onValidate: (
|
2021-12-08 14:56:57 +08:00
|
|
|
name: string | number | Array<string | number>,
|
2021-10-29 10:22:35 +08:00
|
|
|
status: boolean,
|
|
|
|
errors: string[] | null,
|
|
|
|
) => void;
|
2021-12-23 22:00:14 +08:00
|
|
|
validateMessages: ComputedRef<ValidateMessages>;
|
2021-06-07 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const FormContextKey: InjectionKey<FormContextProps> = Symbol('formContextKey');
|
|
|
|
|
|
|
|
export const useProvideForm = (state: FormContextProps) => {
|
|
|
|
provide(FormContextKey, state);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useInjectForm = () => {
|
|
|
|
return inject(FormContextKey, {
|
2022-03-16 20:39:25 +08:00
|
|
|
name: computed(() => undefined),
|
2021-06-07 17:35:03 +08:00
|
|
|
labelAlign: computed(() => 'right' as FormLabelAlign),
|
|
|
|
vertical: computed(() => false),
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
addField: (_eventKey: string, _field: FieldExpose) => {},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
removeField: (_eventKey: string) => {},
|
2021-06-20 22:53:42 +08:00
|
|
|
model: computed(() => undefined),
|
|
|
|
rules: computed(() => undefined),
|
2022-03-16 20:39:25 +08:00
|
|
|
colon: computed(() => undefined),
|
|
|
|
labelWrap: computed(() => undefined),
|
|
|
|
labelCol: computed(() => undefined),
|
2021-06-20 22:53:42 +08:00
|
|
|
requiredMark: computed(() => false),
|
2022-03-16 20:39:25 +08:00
|
|
|
validateTrigger: computed(() => undefined),
|
2021-10-29 10:22:35 +08:00
|
|
|
onValidate: () => {},
|
2021-12-23 22:00:14 +08:00
|
|
|
validateMessages: computed(() => defaultValidateMessages),
|
2022-03-16 20:39:25 +08:00
|
|
|
} as FormContextProps);
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Used for ErrorList only */
|
|
|
|
export interface FormItemPrefixContextProps {
|
|
|
|
prefixCls: ComputedRef<string>;
|
|
|
|
status?: ComputedRef<ValidateStatus>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FormItemPrefixContextKey: InjectionKey<FormItemPrefixContextProps> = Symbol(
|
|
|
|
'formItemPrefixContextKey',
|
|
|
|
);
|
|
|
|
|
|
|
|
export const useProvideFormItemPrefix = (state: FormItemPrefixContextProps) => {
|
|
|
|
provide(FormItemPrefixContextKey, state);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useInjectFormItemPrefix = () => {
|
|
|
|
return inject(FormItemPrefixContextKey, {
|
|
|
|
prefixCls: computed(() => ''),
|
|
|
|
});
|
|
|
|
};
|