mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
Update name variable and optimzed code
This commit is contained in:
@@ -28,10 +28,19 @@ const getPropValues = (stateSchema: TStateSchema, prop?: TPropValues) => {
|
|||||||
}, {} as TKeyValue);
|
}, {} as TKeyValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addValidator = (func: (input: string) => boolean, error: string) => {
|
type test = {
|
||||||
|
validatorFunc: (value: string) => boolean;
|
||||||
|
prop: string;
|
||||||
|
required?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const addValidator = ({ validatorFunc, prop, required = true }: test) => {
|
||||||
return {
|
return {
|
||||||
func: func,
|
required: required,
|
||||||
error: invalidFormatMessage(error),
|
validator: {
|
||||||
|
func: validatorFunc,
|
||||||
|
error: invalidFormatMessage(prop),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import FormRow from '../../components/FormRow';
|
|||||||
import Button from '../../commons/styles/Button.ts';
|
import Button from '../../commons/styles/Button.ts';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { TKeyValue, TStateSchema } from '../../globals/types';
|
import { TKeyValue, TStateSchema, TValidator } from '../../globals/types';
|
||||||
|
|
||||||
// Hooks
|
// Hooks
|
||||||
import useForm from '../../hooks/useForm';
|
import useForm from '../../hooks/useForm';
|
||||||
@@ -50,42 +50,25 @@ const UserForm = ({ onClose }: IUserFormProp) => {
|
|||||||
address: { value: '', error: '' },
|
address: { value: '', error: '' },
|
||||||
};
|
};
|
||||||
|
|
||||||
const stateValidatorSchema = {
|
// prettier-ignore
|
||||||
fullName: {
|
const stateValidatorSchema: TValidator = {
|
||||||
required: true,
|
fullName: addValidator({
|
||||||
validator: addValidator(
|
validatorFunc: isValidString,
|
||||||
(input: string) => isValidString(input),
|
prop: 'full name' }),
|
||||||
'full name',
|
identifiedCode: addValidator({
|
||||||
),
|
validatorFunc: isValidNumber,
|
||||||
},
|
prop: 'identified code',
|
||||||
identifiedCode: {
|
}),
|
||||||
required: true,
|
phone: addValidator({
|
||||||
validator: addValidator(
|
validatorFunc: isValidPhoneNumber,
|
||||||
(input: string) => isValidNumber(input),
|
prop: 'phone number',
|
||||||
'identified code',
|
}),
|
||||||
),
|
room: addValidator({
|
||||||
},
|
validatorFunc: isValidNumber,
|
||||||
phone: {
|
prop: 'room number' }),
|
||||||
required: true,
|
address: addValidator({
|
||||||
validator: addValidator(
|
validatorFunc: isValidAddress,
|
||||||
(input: string) => isValidPhoneNumber(input),
|
prop: 'address' }),
|
||||||
'phone',
|
|
||||||
),
|
|
||||||
},
|
|
||||||
room: {
|
|
||||||
required: true,
|
|
||||||
validator: addValidator(
|
|
||||||
(input: string) => isValidNumber(input),
|
|
||||||
'room number',
|
|
||||||
),
|
|
||||||
},
|
|
||||||
address: {
|
|
||||||
required: true,
|
|
||||||
validator: addValidator(
|
|
||||||
(input: string) => isValidAddress(input),
|
|
||||||
'address',
|
|
||||||
),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Submit form
|
// Submit form
|
||||||
|
|||||||
Reference in New Issue
Block a user