mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(@vant/use): add useCustomFieldValue (#9200)
* feat(@vant/use): add useCustomFieldValue * chore: upd
This commit is contained in:
parent
86f6692284
commit
a4a2a3cb0f
@ -8,4 +8,5 @@ export * from './useWindowSize';
|
|||||||
export * from './useScrollParent';
|
export * from './useScrollParent';
|
||||||
export * from './useEventListener';
|
export * from './useEventListener';
|
||||||
export * from './usePageVisibility';
|
export * from './usePageVisibility';
|
||||||
|
export * from './useCustomFieldValue';
|
||||||
export * from './onMountedOrActivated';
|
export * from './onMountedOrActivated';
|
||||||
|
24
packages/vant-use/src/useCustomFieldValue/index.ts
Normal file
24
packages/vant-use/src/useCustomFieldValue/index.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user