mirror of
https://github.com/youzan/vant.git
synced 2026-02-27 02:00:20 +08:00
feat(@vant/use): add useCustomFieldValue (#9200)
* feat(@vant/use): add useCustomFieldValue * chore: upd
This commit is contained in:
@@ -8,4 +8,5 @@ export * from './useWindowSize';
|
||||
export * from './useScrollParent';
|
||||
export * from './useEventListener';
|
||||
export * from './usePageVisibility';
|
||||
export * from './useCustomFieldValue';
|
||||
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');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user