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

Commit

Permalink
Refactor(type): enhance types
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jan 2, 2021
1 parent ce365f7 commit 9c1e7ce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-apricots-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Refactor(type): enhance types
2 changes: 1 addition & 1 deletion app/src/Playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface FormValues {
const Playground = (): JSX.Element => {
const { form, controller } = useForm<FormValues>({
defaultValues: { age: "", slider: 0 },
onSubmit: (values) => alert(JSON.stringify(values, undefined, 2)),
onSubmit: (values, e) => alert(JSON.stringify(values, undefined, 2)),
});

const formatter: Formatter<number, number> = (value) => value / 2;
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/3rd-party-ui-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Life is hard but coding can be easier. The reason that we ❤️ open-source sof

1. Coming soon...
2. Coming soon...
3. Coming soon...
3. Coming soon...
14 changes: 7 additions & 7 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ interface Options<V> {
}

interface ResetHandler<V> {
(values: V, options: Options<V>, event?: Event | SyntheticEvent<any>): void;
(values: V, options: Options<V>, event?: Event | SyntheticEvent): void;
}

interface SubmitHandler<V> {
(
values: V,
options: Options<V>,
event?: Event | SyntheticEvent<any>
event?: Event | SyntheticEvent
): void | Promise<void>;
}

interface ErrorHandler<V> {
(
errors: FormErrors<V>,
options: Options<V>,
event?: Event | SyntheticEvent<any>
event?: Event | SyntheticEvent
): void;
}

Expand Down Expand Up @@ -186,12 +186,12 @@ export interface Reset<V> {
(
values?: ValuesArg<V> | null,
exclude?: (keyof FormState<V>)[] | null,
event?: SyntheticEvent<any>
event?: SyntheticEvent
): void;
}

export interface Submit<V> {
(event?: SyntheticEvent<any>): Promise<{
(event?: SyntheticEvent): Promise<{
values?: V;
errors?: FormErrors<V>;
}>;
Expand All @@ -215,13 +215,13 @@ export interface Controller<V, E = any> {
parse?: Parser;
format?: Formatter;
onChange?: (event: E, value: any) => void;
onBlur?: (event: FocusEvent<any>) => void;
onBlur?: (event: FocusEvent) => void;
}
): {
name: string;
value: any;
onChange: (event: E) => void;
onBlur: (event: FocusEvent<any>) => void;
onBlur: (event: FocusEvent) => void;
} | void;
}

Expand Down
22 changes: 11 additions & 11 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ declare module "react-cool-form" {
[K in keyof V]?: V[K] extends T ? T : DeepProps<V[K]>;
};

type FormErrors<V> = DeepProps<V>;

interface Options<V> {
formState: FormState<V>;
setErrors: SetErrors<V>;
Expand Down Expand Up @@ -52,7 +50,7 @@ declare module "react-cool-form" {
}

interface SetErrors<V> {
(errors?: FormErrors<V> | PreviousErrorsFn): void;
(errors?: FormErrors<V> | PreviousErrorsFn<FormErrors<V>>): void;
}

interface SetFieldError {
Expand Down Expand Up @@ -86,19 +84,21 @@ declare module "react-cool-form" {
(
values?: V | PreviousValuesFn<V> | null,
exclude?: (keyof FormState<V>)[] | null,
event?: SyntheticEvent<any>
event?: SyntheticEvent
): void;
}

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

export type FormValues = Record<string, any>;

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

export type FormState<V = FormValues> = Readonly<{
values: V;
touched: DeepProps<V, boolean>;
Expand Down Expand Up @@ -132,8 +132,8 @@ declare module "react-cool-form" {
(previousValue: any): any;
}

export interface PreviousErrorsFn {
(previousErrors: FormErrors<V>): FormErrors<V> | undefined;
export interface PreviousErrorsFn<E = FormErrors> {
(previousErrors: E): E | undefined;
}

export interface PreviousErrorFn {
Expand All @@ -153,26 +153,26 @@ declare module "react-cool-form" {
}

export interface BlurHandler {
(event: FocusEvent<any>): void;
(event: FocusEvent): void;
}

export interface ResetHandler<V = FormValues> {
(values: V, options: Options<V>, event?: Event | SyntheticEvent<any>): void;
(values: V, options: Options<V>, event?: Event | SyntheticEvent): void;
}

export interface SubmitHandler<V = FormValues> {
(
values: V,
options: Options<V>,
event?: Event | SyntheticEvent<any>
event?: Event | SyntheticEvent
): void | Promise<void>;
}

export interface ErrorHandler<V = FormValues> {
(
errors: FormErrors<V>,
options: Options<V>,
event?: Event | SyntheticEvent<any>
event?: Event | SyntheticEvent
): void;
}

Expand Down

0 comments on commit 9c1e7ce

Please sign in to comment.