Example: login form
./schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import joi from 'joi';
// import { Input, Select, Textarea, CheckList, RadioList } from '@ferdotnet/reactive-form-builder';
// or, import here your custom inputs if needed
const schema = {
email: {
value: '',
label: 'Email',
input: Input,
type: 'email',
joi: joi.string().regex(emailRegex).required().messages({
'string.pattern.base': messages.INVALID_EMAIL,
'string.empty': messages.REQUIRED
})
},
password: {
value: '',
label: 'Password',
input: Input,
type: 'password',
joi: joi.string().regex(passwordRegex).required().messages({
'string.pattern.base': messages.INVALID_PASSWORD,
'string.empty': messages.REQUIRED
})
}
}
export default schema;
./yourComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import React from 'react';
import { useReactiveFormBuilder, ReactiveFormBuilder } from '@ferdotnet/reactive-form-builder';
import schema from './schema';
const MyComponent = () => {
const handler = useReactiveFormBuilder(schema, true); // Second param is to debug
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
alert(JSON.stringify(handler.data));
};
return (
<form onSubmit={handleSubmit}>
<ReactiveFormBuilder handler={handler} />
<button type="submit" disabled={!handler.valid}>
Submit
</button>
</form>
);
};
export default MyComponent;