Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #619 from wellyshen/feature/export-more-types
Browse files Browse the repository at this point in the history
Refactor(types): export more types
  • Loading branch information
wellyshen committed May 18, 2021
2 parents 3d3d72a + 8807efd commit 8a1c1f5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-crews-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Refactor(types): export more types
50 changes: 23 additions & 27 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { FocusEventHandler, MutableRefObject, SyntheticEvent } from "react";
// Utils
export type ObjMap<T = boolean> = Record<string, T>;

type DeepProps<V, T = any> = {
[K in keyof V]?: V[K] extends T ? T : DeepProps<V[K]>;
};

// Global
export type Methods<V = any> = {
validateOnChange: boolean;
Expand All @@ -23,10 +27,6 @@ export type Methods<V = any> = {
} & FormMethods<V>;

// useState
type DeepProps<V, T = any> = {
[K in keyof V]?: V[K] extends T ? T : DeepProps<V[K]>;
};

export type FormErrors<V> = DeepProps<V>;

export interface FormState<V = any> {
Expand Down Expand Up @@ -85,6 +85,8 @@ export type Fields = Map<
}
>;

export type Parsers = ObjMap<Omit<FieldOptions, "validate">>;

export type FieldArray = ObjMap<{ fields: ObjMap; reset: () => void }>;

interface EventOptions<V> {
Expand Down Expand Up @@ -150,17 +152,17 @@ interface FieldParser {
(value: any): any;
}

interface FieldOptions<V = any> {
validate?: FieldValidator<V>;
valueAsNumber?: boolean;
valueAsDate?: boolean;
parse?: FieldParser;
}

export interface RegisterField<V = any> {
(
value:
| FieldValidator<V>
| {
validate?: FieldValidator<V>;
valueAsNumber?: boolean;
valueAsDate?: boolean;
parse?: FieldParser;
}
): (field: FieldElement | null) => void;
(value: FieldValidator<V> | FieldOptions<V>): (
field: FieldElement | null
) => void;
}

export interface HandleChangeEvent {
Expand Down Expand Up @@ -201,13 +203,6 @@ export interface GetFormState<V> {
): any;
}

export interface Use<V> {
(
path: Path,
options?: { defaultValues?: V; errorWithTouched?: boolean }
): any;
}

export interface Focus {
(name: string, delay?: number): void;
}
Expand All @@ -219,6 +214,13 @@ export interface RemoveField {
): void;
}

export interface Use<V> {
(
path: Path,
options?: { defaultValues?: V; errorWithTouched?: boolean }
): any;
}

export interface GetState {
(path?: string | string[] | ObjMap<string>): any;
}
Expand Down Expand Up @@ -268,12 +270,6 @@ export interface Submit<V> {
}>;
}

export type Parsers = ObjMap<{
valueAsNumber?: boolean;
valueAsDate?: boolean;
parse?: FieldParser;
}>;

export type FormConfig<V = any> = Partial<{
id: string;
defaultValues: V;
Expand Down
200 changes: 103 additions & 97 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ declare module "react-cool-form" {
// Type utils
type ObjMap<T = boolean> = Record<string, T>;

// useForm
type DeepProps<V, T = any> = {
[K in keyof V]?: V[K] extends T ? T : DeepProps<V[K]>;
};

interface EventOptions<V> {
// useForm
export type FormValues = ObjMap<any>;

export interface EventOptions<V extends FormValues = FormValues> {
removeField: RemoveField;
getState: GetState;
setValue: SetValue;
Expand All @@ -23,95 +25,6 @@ declare module "react-cool-form" {
submit: Submit<V>;
}

interface Use<V> {
(
path: string | string[] | ObjMap<string>,
options?: { defaultValues?: V; errorWithTouched?: boolean }
): any;
}

interface Focus {
(name: string, delay?: number): void;
}

interface RemoveField {
(
name: string,
exclude?: ("defaultValue" | "value" | "touched" | "dirty" | "error")[]
): void;
}

interface GetState {
(path?: string | string[] | ObjMap<string>): any;
}

interface SetValue {
(
name: string,
value: any | PreviousValueFn,
options?: {
[k in "shouldValidate" | "shouldTouched" | "shouldDirty"]?: boolean;
}
): void;
}

interface SetTouched {
(name: string, isTouched?: boolean, shouldValidate?: boolean): void;
}

interface SetDirty {
(name: string, isDirty?: boolean): void;
}

interface SetError {
(name: string, error: any | PreviousErrorFn): void;
}

interface ClearErrors {
(name?: string | string[]): void;
}

interface RegisterForm {
(element: HTMLElement | null): void;
}

interface RegisterFieldReturn {
(
field: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null
): void;
}

interface RegisterField<V> {
(validate: FieldValidator<V>): RegisterFieldReturn;
(options: {
validate?: FieldValidator<V>;
valueAsNumber?: boolean;
valueAsDate?: boolean;
parse?: FieldParser;
}): RegisterFieldReturn;
}

interface RunValidation {
(name?: string | string[]): Promise<boolean>;
}

interface Reset<V> {
(
values?: V | PreviousValuesFn<V> | null,
exclude?: (keyof FormState<V>)[] | null,
event?: SyntheticEvent
): void;
}

interface Submit<V> {
(event?: SyntheticEvent): Promise<{
values?: V;
errors?: FormErrors<V>;
}>;
}

export type FormValues = ObjMap<any>;

export type FormErrors<E extends FormValues = FormValues> = DeepProps<E>;

export type FormState<V extends FormValues = FormValues> = Readonly<{
Expand Down Expand Up @@ -183,6 +96,99 @@ declare module "react-cool-form" {
(formState: FormState<V>): void;
}

export interface RegisterForm {
(element: HTMLElement | null): void;
}

export interface RegisterFieldReturn {
(
field: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null
): void;
}

export interface FieldOptions<V extends FormValues = FormValues> {
validate?: FieldValidator<V>;
valueAsNumber?: boolean;
valueAsDate?: boolean;
parse?: FieldParser;
}

export interface RegisterField<V extends FormValues = FormValues> {
(validate: FieldValidator<V>): RegisterFieldReturn;
(options: FieldOptions<V>): RegisterFieldReturn;
}

export interface Focus {
(name: string, delay?: number): void;
}

export interface RemoveField {
(
name: string,
exclude?: ("defaultValue" | "value" | "touched" | "dirty" | "error")[]
): void;
}

export interface UseOptions<V extends FormValues = FormValues> {
defaultValues?: V;
errorWithTouched?: boolean;
}

export interface Use<V extends FormValues = FormValues> {
(path: string | string[] | ObjMap<string>, options?: UseOptions<V>): any;
}

export interface GetState {
(path?: string | string[] | ObjMap<string>): any;
}

export type SetValueOptions = {
[k in "shouldValidate" | "shouldTouched" | "shouldDirty"]?: boolean;
};

export interface SetValue {
(
name: string,
value: any | PreviousValueFn,
options?: SetValueOptions
): void;
}

export interface SetTouched {
(name: string, isTouched?: boolean, shouldValidate?: boolean): void;
}

export interface SetDirty {
(name: string, isDirty?: boolean): void;
}

export interface SetError {
(name: string, error: any | PreviousErrorFn): void;
}

export interface ClearErrors {
(name?: string | string[]): void;
}

export interface RunValidation {
(name?: string | string[]): Promise<boolean>;
}

export interface Reset<V extends FormValues = FormValues> {
(
values?: V | PreviousValuesFn<V> | null,
exclude?: (keyof FormState<V>)[] | null,
event?: SyntheticEvent
): void;
}

export interface Submit<V extends FormValues = FormValues> {
(event?: SyntheticEvent): Promise<{
values?: V;
errors?: FormErrors<V>;
}>;
}

export type FormConfig<V extends FormValues = FormValues> = Partial<{
id: string;
defaultValues: V;
Expand Down Expand Up @@ -286,28 +292,28 @@ declare module "react-cool-form" {
): ControlledReturn;

// useFieldArray
type HelperOptions = Partial<{
export type HelperOptions = Partial<{
shouldTouched: boolean;
shouldDirty: boolean;
}>;

interface Push<T> {
export interface Push<T = any> {
(value: T, options?: HelperOptions): void;
}

interface Insert<T> {
export interface Insert<T = any> {
(index: number, value: T, options?: HelperOptions): void;
}

interface Remove<T> {
export interface Remove<T = any> {
(index: number): T | void;
}

interface Swap {
export interface Swap {
(indexA: number, indexB: number): void;
}

interface Move {
export interface Move {
(from: number, to: number): void;
}

Expand Down

0 comments on commit 8a1c1f5

Please sign in to comment.