From 425610d654f33c516634c1f9af29beb0dcf6e6ae Mon Sep 17 00:00:00 2001 From: Loi Phan Date: Sat, 4 Nov 2023 13:23:20 +0700 Subject: [PATCH] Apply form hook for room page --- .../src/components/Select/index.tsx | 78 +++- .../src/components/SortBy/index.tsx | 2 +- hotel-management/src/constants/path.ts | 2 +- hotel-management/src/globals/types.ts | 1 - hotel-management/src/helpers/utils.ts | 25 +- hotel-management/src/helpers/validators.ts | 4 +- hotel-management/src/hooks/useFetch.ts | 5 +- hotel-management/src/hooks/useForm.ts | 170 -------- hotel-management/src/pages/Room/Form.tsx | 383 ++++++------------ hotel-management/src/pages/Room/Table.tsx | 6 +- hotel-management/src/pages/User/Form.tsx | 5 +- 11 files changed, 208 insertions(+), 473 deletions(-) delete mode 100644 hotel-management/src/hooks/useForm.ts diff --git a/hotel-management/src/components/Select/index.tsx b/hotel-management/src/components/Select/index.tsx index 7317b3e..fc047d0 100644 --- a/hotel-management/src/components/Select/index.tsx +++ b/hotel-management/src/components/Select/index.tsx @@ -1,5 +1,11 @@ -import { FieldValues, RegisterOptions, useFormContext } from 'react-hook-form'; +import { + FieldValues, + RegisterOptions, + UseFormRegister, + useFormContext, +} from 'react-hook-form'; import { StyledSelect } from './styled'; +import React, { ReactNode } from 'react'; export interface ISelectOptions { value: string; @@ -8,31 +14,81 @@ export interface ISelectOptions { interface ISelect { options: ISelectOptions[]; optionsConfigForm?: RegisterOptions | undefined; - value?: number; - id: string; + value?: string; + onChange?: React.ChangeEventHandler | undefined; + id?: string; + ariaLabel: string; } -const Select = ({ options, id, optionsConfigForm }: ISelect) => { - const { register } = useFormContext() ?? {}; +interface IRender extends ISelect { + register: UseFormRegister; + children: ReactNode[]; +} - if(!register) { - return null; +const RenderSelect = ({ + options, + id, + value, + optionsConfigForm, + onChange, + register, + children, + ariaLabel, +}: IRender) => { + if (options && !register) { + return ( + + {children} + + ); } + return ( + + {children} + + ); +}; + +const Select = ({ + options, + id, + optionsConfigForm, + value, + onChange, + ariaLabel +}: ISelect) => { + const { register } = useFormContext() ?? {}; + if (!options) return; return ( - {options.map((option) => ( ))} - + ); }; diff --git a/hotel-management/src/components/SortBy/index.tsx b/hotel-management/src/components/SortBy/index.tsx index 1255c8a..def89c0 100644 --- a/hotel-management/src/components/SortBy/index.tsx +++ b/hotel-management/src/components/SortBy/index.tsx @@ -20,12 +20,12 @@ const SortBy = memo(({ options }: ISortByProps) => { setSearchParams(searchParams); }; - // prettier-ignore return ( - // - // - // - // - // - // +
+ + + + isValidString(value) || INVALID_FIELD, + }, + onChange: () => trigger('name'), + })} + /> + - // - // - // + + isValidNumber(v) || INVALID_FIELD, + }, + onChange: () => trigger('price'), + })} + /> + - // - // - // + + isValidNumber(v) || INVALID_DISCOUNT, + }, + onChange: () => trigger('discount'), + })} + /> + - // - //