mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-30 10:19:15 +08:00
24 lines
701 B
TypeScript
24 lines
701 B
TypeScript
import { watch, inject, InjectionKey, Ref } from 'vue';
|
|
|
|
export type CustomFieldInjectionValue = {
|
|
customValue: Ref<(() => unknown) | undefined>;
|
|
resetValidation: () => void;
|
|
validateWithTrigger: (trigger: 'onBlur' | 'onChange' | 'onSubmit') => void;
|
|
};
|
|
|
|
export const CUSTOM_FIELD_INJECTION_KEY: InjectionKey<CustomFieldInjectionValue> =
|
|
Symbol('van-field');
|
|
|
|
export function useCustomFieldValue(customValue: () => unknown) {
|
|
const field = inject(CUSTOM_FIELD_INJECTION_KEY, null);
|
|
|
|
if (field && !field.customValue.value) {
|
|
field.customValue.value = customValue;
|
|
|
|
watch(customValue, () => {
|
|
field.resetValidation();
|
|
field.validateWithTrigger('onChange');
|
|
});
|
|
}
|
|
}
|