Skip to content

Commit

Permalink
feat: enhance ts typing for form functions
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Dec 18, 2020
1 parent cab1329 commit 8f7d8e8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/vee-validate/src/useFormErrors.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { computed } from 'vue';
import { computed, ComputedRef } from 'vue';
import { FormErrorsSymbol } from './symbols';
import { injectWithSelf, warn } from './utils';

/**
* Gives access to all form errors
*/
export function useFormErrors() {
export function useFormErrors<TValues extends Record<string, any> = Record<string, any>>() {
const errors = injectWithSelf(FormErrorsSymbol);
if (!errors) {
warn('No vee-validate <Form /> or `useForm` was detected in the component tree');
}

return errors || computed(() => ({}));
return (errors as ComputedRef<Partial<Record<keyof TValues, string | undefined>>>) || computed(() => ({}));
}
2 changes: 1 addition & 1 deletion packages/vee-validate/src/useResetForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FormSymbol } from './symbols';
import { FormState } from './types';
import { injectWithSelf, warn } from './utils';

export function useResetForm<TValues = Record<string, any>>() {
export function useResetForm<TValues extends Record<string, any> = Record<string, any>>() {
const form = injectWithSelf(FormSymbol);
if (!form) {
warn('No vee-validate <Form /> or `useForm` was detected in the component tree');
Expand Down
10 changes: 5 additions & 5 deletions packages/vee-validate/src/useValidateForm.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { FormSymbol } from './symbols';
import { FormValidationResult } from './types';
import { FormContext, FormValidationResult } from './types';
import { injectWithSelf, warn } from './utils';

/**
* Validate multiple fields
*/
export function useValidateForm() {
const form = injectWithSelf(FormSymbol);
export function useValidateForm<TValues extends Record<string, any> = Record<string, any>>() {
const form = injectWithSelf(FormSymbol) as FormContext<TValues> | undefined;
if (!form) {
warn('No vee-validate <Form /> or `useForm` was detected in the component tree');
}

return function validateField() {
return function validateField(): Promise<FormValidationResult<TValues>> {
if (!form) {
return Promise.resolve({ errors: {}, valid: true } as FormValidationResult<any>);
return Promise.resolve({ errors: {}, valid: true });
}

return form.validate();
Expand Down

0 comments on commit 8f7d8e8

Please sign in to comment.