forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormItemContext.ts
106 lines (99 loc) · 3.19 KB
/
FormItemContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type { ComputedRef, InjectionKey, ConcreteComponent } from 'vue';
import {
watch,
computed,
inject,
provide,
ref,
onBeforeUnmount,
getCurrentInstance,
defineComponent,
} from 'vue';
import devWarning from '../vc-util/devWarning';
export type FormItemContext = {
id: ComputedRef<string>;
onFieldBlur: () => void;
onFieldChange: () => void;
clearValidate: () => void;
};
type InternalFormItemContext = {
addFormItemField: (key: Symbol, type: ConcreteComponent) => void;
removeFormItemField: (key: Symbol) => void;
};
const ContextKey: InjectionKey<FormItemContext> = Symbol('ContextProps');
const InternalContextKey: InjectionKey<InternalFormItemContext> = Symbol('InternalContextProps');
export const useProvideFormItemContext = (
props: FormItemContext,
useValidation: ComputedRef<boolean> = computed(() => true),
) => {
const formItemFields = ref(new Map<Symbol, ConcreteComponent>());
const addFormItemField = (key: Symbol, type: ConcreteComponent) => {
formItemFields.value.set(key, type);
formItemFields.value = new Map(formItemFields.value);
};
const removeFormItemField = (key: Symbol) => {
formItemFields.value.delete(key);
formItemFields.value = new Map(formItemFields.value);
};
const instance = getCurrentInstance();
watch([useValidation, formItemFields], () => {
if (process.env.NODE_ENV !== 'production') {
if (useValidation.value && formItemFields.value.size > 1) {
devWarning(
false,
'Form.Item',
`FormItem can only collect one field item, you haved set ${[
...formItemFields.value.values(),
]
.map(v => `\`${v.name}\``)
.join(', ')} ${formItemFields.value.size} field items.
You can set not need to be collected fields into \`a-form-item-rest\``,
);
let cur = instance;
while (cur.parent) {
console.warn('at', cur.type);
cur = cur.parent;
}
}
}
});
provide(ContextKey, props);
provide(InternalContextKey, {
addFormItemField,
removeFormItemField,
});
};
const defaultContext: FormItemContext = {
id: computed(() => undefined),
onFieldBlur: () => {},
onFieldChange: () => {},
clearValidate: () => {},
};
const defaultInternalContext: InternalFormItemContext = {
addFormItemField: () => {},
removeFormItemField: () => {},
};
export const useInjectFormItemContext = () => {
const internalContext = inject(InternalContextKey, defaultInternalContext);
const formItemFieldKey = Symbol('FormItemFieldKey');
const instance = getCurrentInstance();
internalContext.addFormItemField(formItemFieldKey, instance.type);
onBeforeUnmount(() => {
internalContext.removeFormItemField(formItemFieldKey);
});
// We should prevent the passing of context for children
provide(InternalContextKey, defaultInternalContext);
provide(ContextKey, defaultContext);
return inject(ContextKey, defaultContext);
};
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'AFormItemRest',
setup(_, { slots }) {
provide(InternalContextKey, defaultInternalContext);
provide(ContextKey, defaultContext);
return () => {
return slots.default?.();
};
},
});