Skip to content

Commit

Permalink
fix(core): fix register type error (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini-ghost authored Jun 23, 2023
1 parent c2252f0 commit f815fc6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/composables/useFieldArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function useFieldArray<Value>(

const fields: Ref<FieldEntry<Value>[]> = ref([]);
const values = computed(() => {
return getFieldValue<Value[] | undefined>(name).value || [];
return getFieldValue(name).value || [];
});

let seed = 0;
Expand Down
18 changes: 14 additions & 4 deletions packages/core/src/composables/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import keysOf from '../utils/keysOf';
import set from '../utils/set';

import type { Reducer } from './useFormStore';
import type { InternalContextValues } from './useInternalContext';
import type {
FieldAttrs,
FieldError,
FieldMeta,
FormErrors,
FormEventHandler,
Expand Down Expand Up @@ -406,9 +408,12 @@ export function useForm<
});
};

const getFieldMeta = <Name extends Path<Values>>(
const getFieldMeta = <
Name extends Path<Values>,
Value = PathValue<Values, Name>,
>(
name: MaybeRefOrGetter<Name>,
): FieldMeta<PathValue<Values, Name>> => {
): FieldMeta<Value> => {
const error = computed(() => getFieldError(name));
const touched = computed(() => getFieldTouched(name));
const dirty = computed(() => getFieldDirty(name));
Expand All @@ -429,7 +434,12 @@ export function useForm<
}));
};

const getFieldError = (name: MaybeRefOrGetter<string>): FormErrors<any> => {
const getFieldError = <
Name extends Path<Values>,
Value = PathValue<Values, Name>,
>(
name: MaybeRefOrGetter<Name>,
): FieldError<Value> => {
name = toValue(name);
return get(state.errors.value, name);
};
Expand Down Expand Up @@ -628,7 +638,7 @@ export function useForm<
validateField,
};

provide(InternalContextKey, {
provide<InternalContextValues<Values>>(InternalContextKey, {
getFieldValue,
setFieldValue,
getFieldError,
Expand Down
32 changes: 20 additions & 12 deletions packages/core/src/composables/useInternalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
import {
FieldArrayValidator,
FieldAttrs,
FormErrors,
FieldError,
FormTouched,
FormValues,
MaybeRefOrGetter,
Path,
PathValue,
SetFieldArrayValue,
UseFormRegister,
UseFormSetFieldValue,
Expand All @@ -26,21 +28,25 @@ function injectMaybeSelf<T>(
return vm?.provides[key as any] || inject(key, defaultValue);
}

export interface InternalContextValues {
export interface InternalContextValues<Values extends FormValues> {
registerFieldArray: (
name: MaybeRefOrGetter<string>,
name: MaybeRefOrGetter<Path<Values>>,
options: {
validate?: FieldArrayValidator<any>;
validate?: FieldArrayValidator<PathValue<Values, Path<Values>>>;
reset: () => void;
},
) => void;

getFieldValue: <Value>(
name: MaybeRefOrGetter<string>,
) => WritableComputedRef<Value>;
getFieldValue: (
name: MaybeRefOrGetter<Path<Values>>,
) => WritableComputedRef<PathValue<Values, Path<Values>>>;

setFieldValue: UseFormSetFieldValue<FormValues>;

getFieldError: (name: MaybeRefOrGetter<string>) => FormErrors<any>;
getFieldError: (
name: MaybeRefOrGetter<Path<Values>>,
) => FieldError<PathValue<Values, Path<Values>>>;

getFieldTouched: (name: MaybeRefOrGetter<string>) => FormTouched<boolean>;
getFieldDirty: (name: MaybeRefOrGetter<string>) => boolean;
getFieldAttrs: (name: MaybeRefOrGetter<string>) => ComputedRef<FieldAttrs>;
Expand All @@ -50,11 +56,13 @@ export interface InternalContextValues {
register: UseFormRegister<FormValues>;
}

export const InternalContextKey: InjectionKey<InternalContextValues> = Symbol(
__DEV__ ? 'vorms internal context' : '',
);
export const InternalContextKey: InjectionKey<
InternalContextValues<FormValues>
> = Symbol(__DEV__ ? 'vorms internal context' : '');

export function useInternalContext() {
const context = injectMaybeSelf(InternalContextKey) as InternalContextValues;
const context = injectMaybeSelf(
InternalContextKey,
) as InternalContextValues<FormValues>;
return context;
}
6 changes: 3 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export type FormErrors<Values> = {
};

export type FieldError<Value> = Value extends Primitive
? string | string[] | void
: string | string[] | FormErrors<Value> | void;
? string | string[] | undefined
: string | string[] | FormErrors<Value> | undefined;

export interface FormState<Values extends FormValues> {
values: Values;
Expand Down Expand Up @@ -154,7 +154,7 @@ export type FieldAttrs = {

export type FieldMeta<Value> = {
dirty: ComputedRef<boolean>;
error: ComputedRef<FormErrors<Value>>;
error: ComputedRef<FieldError<Value>>;
touched: ComputedRef<boolean | undefined>;
};

Expand Down

0 comments on commit f815fc6

Please sign in to comment.