diff --git a/apps/portal/src/components/questions/ContributeQuestionCard.tsx b/apps/portal/src/components/questions/ContributeQuestionCard.tsx index 951510b8..d6ea805b 100644 --- a/apps/portal/src/components/questions/ContributeQuestionCard.tsx +++ b/apps/portal/src/components/questions/ContributeQuestionCard.tsx @@ -1,7 +1,4 @@ -import type { ComponentProps, ForwardedRef } from 'react'; import { useState } from 'react'; -import { forwardRef } from 'react'; -import type { UseFormRegisterReturn } from 'react-hook-form'; import { useForm } from 'react-hook-form'; import { BuildingOffice2Icon, @@ -10,6 +7,8 @@ import { } from '@heroicons/react/24/outline'; import { Button, TextInput } from '@tih/ui'; +import { useFormRegister } from '~/utils/questions/useFormRegister'; + import ContributeQuestionDialog from './ContributeQuestionDialog'; export type ContributeQuestionData = { @@ -19,27 +18,6 @@ export type ContributeQuestionData = { questionType: string; }; -type TextInputProps = ComponentProps; - -type FormTextInputProps = Omit & - Pick, 'onChange'>; - -function FormTextInputWithRef( - props: FormTextInputProps, - ref?: ForwardedRef, -) { - const { onChange, ...rest } = props; - return ( - onChange(event)} - /> - ); -} - -const FormTextInput = forwardRef(FormTextInputWithRef); - export type ContributeQuestionCardProps = { onSubmit: (data: ContributeQuestionData) => void; }; @@ -47,7 +25,9 @@ export type ContributeQuestionCardProps = { export default function ContributeQuestionCard({ onSubmit, }: ContributeQuestionCardProps) { - const { register, handleSubmit } = useForm(); + const { register: formRegister, handleSubmit } = + useForm(); + const register = useFormRegister(formRegister); const [showDraftDialog, setShowDraftDialog] = useState(false); const handleDraftDialogCancel = () => { @@ -59,7 +39,7 @@ export default function ContributeQuestionCard({
-
-
-
- (); + const register = useFormRegister(formRegister); + const [canSubmit, setCanSubmit] = useState(false); const handleCheckSimilarQuestions = (checked: boolean) => { setCanSubmit(checked); }; - const register = useCallback( - (...args: Parameters) => { - const { onChange, ...rest } = formRegister(...args); - return { - ...rest, - onChange: (value: string, event: ChangeEvent) => { - onChange(event); - }, - }; - }, - [formRegister], - ); - return ( ( + register: UseFormRegister, +) => { + const formRegister = useCallback( + (...args: Parameters) => { + const { onChange, ...rest } = register(...args); + return { + ...rest, + onChange: (value: string, event: ChangeEvent) => { + onChange(event); + }, + }; + }, + [register], + ); + return formRegister; +};