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);
|
||||
};
|
||||
|
||||
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 {
|
||||
func: func,
|
||||
error: invalidFormatMessage(error),
|
||||
required: required,
|
||||
validator: {
|
||||
func: validatorFunc,
|
||||
error: invalidFormatMessage(prop),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import FormRow from '../../components/FormRow';
|
||||
import Button from '../../commons/styles/Button.ts';
|
||||
|
||||
// Types
|
||||
import { TKeyValue, TStateSchema } from '../../globals/types';
|
||||
import { TKeyValue, TStateSchema, TValidator } from '../../globals/types';
|
||||
|
||||
// Hooks
|
||||
import useForm from '../../hooks/useForm';
|
||||
@@ -50,42 +50,25 @@ const UserForm = ({ onClose }: IUserFormProp) => {
|
||||
address: { value: '', error: '' },
|
||||
};
|
||||
|
||||
const stateValidatorSchema = {
|
||||
fullName: {
|
||||
required: true,
|
||||
validator: addValidator(
|
||||
(input: string) => isValidString(input),
|
||||
'full name',
|
||||
),
|
||||
},
|
||||
identifiedCode: {
|
||||
required: true,
|
||||
validator: addValidator(
|
||||
(input: string) => isValidNumber(input),
|
||||
'identified code',
|
||||
),
|
||||
},
|
||||
phone: {
|
||||
required: true,
|
||||
validator: addValidator(
|
||||
(input: string) => isValidPhoneNumber(input),
|
||||
'phone',
|
||||
),
|
||||
},
|
||||
room: {
|
||||
required: true,
|
||||
validator: addValidator(
|
||||
(input: string) => isValidNumber(input),
|
||||
'room number',
|
||||
),
|
||||
},
|
||||
address: {
|
||||
required: true,
|
||||
validator: addValidator(
|
||||
(input: string) => isValidAddress(input),
|
||||
'address',
|
||||
),
|
||||
},
|
||||
// prettier-ignore
|
||||
const stateValidatorSchema: TValidator = {
|
||||
fullName: addValidator({
|
||||
validatorFunc: isValidString,
|
||||
prop: 'full name' }),
|
||||
identifiedCode: addValidator({
|
||||
validatorFunc: isValidNumber,
|
||||
prop: 'identified code',
|
||||
}),
|
||||
phone: addValidator({
|
||||
validatorFunc: isValidPhoneNumber,
|
||||
prop: 'phone number',
|
||||
}),
|
||||
room: addValidator({
|
||||
validatorFunc: isValidNumber,
|
||||
prop: 'room number' }),
|
||||
address: addValidator({
|
||||
validatorFunc: isValidAddress,
|
||||
prop: 'address' }),
|
||||
};
|
||||
|
||||
// Submit form
|
||||
|
||||
Reference in New Issue
Block a user