Update name variable and optimzed code

This commit is contained in:
2023-10-27 14:15:47 +07:00
parent e95ac9c4ca
commit cce36788ac
2 changed files with 32 additions and 40 deletions
+12 -3
View File
@@ -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),
},
}; };
}; };
+20 -37
View File
@@ -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