New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
react-hook-formとreact-selectの組み合わせ #10
Conversation
@@ -52,6 +52,7 @@ export async function updateUser(updateUserData: User): Promise<void> { | |||
name: updateUserData.name, | |||
email: updateUserData.email, | |||
description: updateUserData.description, | |||
role: updateUserData.role, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ユーザーの追加と編集にroleを追加しました。
roleはnomal_userとadminです
const { register, handleSubmit, control, formState: { errors } } = useForm<UserInputValues>(); | ||
|
||
/* --- セレクトフィールド --------------------------------------------------------------------------------------------- */ | ||
const roleOptions: SelectField.Option<UserRole>[] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらセレクトオプションです。
既存の型定義を使ってハードコーディングしないように意識したのですが、書き方は適切でしょうか?🙇♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
はい、適切な記述方法かと思います。
value | ||
})); | ||
|
||
const getSelectDefaultValue = (): SelectField.Option<UserRole> | null => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらdefaultValueのgetterです。
適切でしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
はい、良いと思います。
@@ -31,7 +38,31 @@ export const UserControlGroup: VFC<Props> = memo((props) => { | |||
|
|||
const { existingUserInfo, submitFunction, submitButtonName } = props; | |||
|
|||
const { register, handleSubmit, formState: { errors } } = useForm<UserInputValues>(); | |||
const defaultValues = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらreact-selectがdefault valueを直接だと取れないようなので、defaultValuesでまとめて定義し、useFormで設定いたしました。
こちら適切でしょうか?🙇♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
はい、良いのではないかと思います。
@@ -78,6 +106,24 @@ export const UserControlGroup: VFC<Props> = memo((props) => { | |||
})} | |||
/> | |||
{ errors.description && userDescriptionErrorMessage(errors.description) } | |||
|
|||
<Controller |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらreact-selectとreact-hook-formのくみ合わせに関して適切でしょうか?
https://codesandbox.io/s/react-hook-form-v7-controller-5h1q5?file=/src/index.js
https://react-hook-form.com/jp/api/#Controller
この辺参考にしました!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
はい、Controllerを利用して適切に記述できているかと思います。
ありがとうございます! |
お世話になっております。
今回はreact-hook-formとreact-selectを組み合わせてみました。
お忙しいところ恐縮ですが、お手隙の際にレビューお願いいたします🙇♂️