Skip to content

Commit 15cc451

Browse files
committed
add TS declaration file
1 parent 1779d9b commit 15cc451

File tree

3 files changed

+109
-46
lines changed

3 files changed

+109
-46
lines changed

src/example/Form/index.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import useForm from '../../lib';
2+
import useForm from 'lib';
33
import './index.css';
44

55
function Form() {
@@ -12,50 +12,6 @@ function Form() {
1212
confirm_password: { value: '', error: '' },
1313
};
1414

15-
// Create your own validationStateSchema
16-
// stateSchema property should be the same in validationStateSchema
17-
// in-order a validation to works in your input.
18-
const stateValidatorSchema = {
19-
first_name: {
20-
required: true,
21-
validator: {
22-
func: (value) => /^[a-zA-Z]+$/.test(value),
23-
error: 'Invalid first name format.',
24-
},
25-
},
26-
last_name: {
27-
required: true,
28-
validator: {
29-
func: (value) => /^[a-zA-Z]+$/.test(value),
30-
error: 'Invalid last name format.',
31-
},
32-
},
33-
tags: {
34-
validator: {
35-
func: (value) => /^(,?\w{3,})+$/.test(value),
36-
error: 'Invalid tag format.',
37-
},
38-
},
39-
password: {
40-
required: true,
41-
compare: {
42-
to: 'confirm_password',
43-
error: 'Password does not match to confirm password',
44-
},
45-
validator: {
46-
func: (value) => /^[a-zA-Z]+$/.test(value),
47-
error: 'Password does not meet the requirement',
48-
},
49-
},
50-
confirm_password: {
51-
required: true,
52-
validator: {
53-
func: (value, values) => value === values.password,
54-
error: 'Confirm Password does not match to Password',
55-
},
56-
},
57-
};
58-
5915
const onSubmitForm = (state) => {
6016
alert(JSON.stringify(state, null, 2));
6117
};
@@ -67,7 +23,50 @@ function Form() {
6723
handleOnChange,
6824
handleOnSubmit,
6925
disable,
70-
} = useForm(stateSchema, stateValidatorSchema, onSubmitForm);
26+
} = useForm(
27+
stateSchema,
28+
{
29+
first_name: {
30+
required: true,
31+
validator: {
32+
func: (value) => /^[a-zA-Z]+$/.test(value),
33+
error: 'Invalid first name format.',
34+
},
35+
},
36+
last_name: {
37+
required: true,
38+
validator: {
39+
func: (value) => /^[a-zA-Z]+$/.test(value),
40+
error: 'Invalid last name format.',
41+
},
42+
},
43+
tags: {
44+
validator: {
45+
func: (value) => /^(,?\w{3,})+$/.test(value),
46+
error: 'Invalid tag format.',
47+
},
48+
},
49+
confirm_password: {
50+
required: true,
51+
validator: {
52+
func: (value, values) => value === values.password,
53+
error: 'Confirm Password does not match to Password',
54+
},
55+
},
56+
password: {
57+
required: true,
58+
compare: {
59+
to: 'confirm_password',
60+
error: 'Password does not match to confirm password',
61+
},
62+
validator: {
63+
func: (value) => /^[a-zA-Z]+$/.test(value),
64+
error: 'Password does not meet the requirement',
65+
},
66+
},
67+
},
68+
onSubmitForm
69+
);
7170

7271
const { first_name, last_name, tags, password, confirm_password } = values;
7372

src/lib/index.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
3+
interface StateSchemaProp {
4+
value: string | number;
5+
error: string;
6+
}
7+
8+
interface Validator {
9+
func(param: any): boolean;
10+
error: string;
11+
}
12+
13+
interface ValidatorSchema<T> {
14+
required?: boolean;
15+
validator?: Validator;
16+
compare?: { to: keyof T; error: string };
17+
}
18+
19+
type ReturnValue<T> = {
20+
values: { [P in keyof T]: any };
21+
errors: { [P in keyof T]: any };
22+
dirty: { [P in keyof T]: any };
23+
handleOnChange(event: React.ChangeEvent<HTMLInputElement>): void;
24+
handleOnSubmit(event: React.FormEvent<HTMLFormElement>): void;
25+
disable: boolean;
26+
};
27+
28+
export type StateSchema<T> = {
29+
[P in keyof T]: StateSchemaProp;
30+
};
31+
32+
export type StateValidatorSchema<T> = {
33+
[P in keyof T]: ValidatorSchema<T>;
34+
};
35+
36+
export default function useForm<T>(
37+
stateSchema: StateSchema<T>,
38+
stateValidatorSchema: StateValidatorSchema<T>,
39+
onSubmitForm?: () => void
40+
): ReturnValue<T>;

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "./src",
4+
"alwaysStrict": true,
5+
"esModuleInterop": true,
6+
"module": "ESNext",
7+
"target": "ES5",
8+
"outDir": "./dist",
9+
"lib": ["ESNext", "ES6", "DOM"],
10+
"strict": true,
11+
"allowJs": true,
12+
"skipLibCheck": true,
13+
"allowSyntheticDefaultImports": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"moduleResolution": "Node",
16+
"resolveJsonModule": true,
17+
"isolatedModules": true,
18+
},
19+
"formatCodeOptions": {
20+
"indentSize": 2,
21+
"tabSize": 2
22+
},
23+
"include": ["src"]
24+
}

0 commit comments

Comments
 (0)