Navigation Menu

Skip to content
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

Merged
merged 5 commits into from May 16, 2022
Merged

Conversation

abeshi03
Copy link
Owner

お世話になっております。
今回はreact-hook-formとreact-selectを組み合わせてみました。

お忙しいところ恐縮ですが、お手隙の際にレビューお願いいたします🙇‍♂️

@@ -52,6 +52,7 @@ export async function updateUser(updateUserData: User): Promise<void> {
name: updateUserData.name,
email: updateUserData.email,
description: updateUserData.description,
role: updateUserData.role,
Copy link
Owner Author

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>[] =
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらセレクトオプションです。
既存の型定義を使ってハードコーディングしないように意識したのですが、書き方は適切でしょうか?🙇‍♂️

Copy link
Collaborator

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 => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらdefaultValueのgetterです。
適切でしょうか?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、良いと思います。

@abeshi03 abeshi03 self-assigned this May 14, 2022
@@ -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 = {
Copy link
Owner Author

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で設定いたしました。
こちら適切でしょうか?🙇‍♂️

Copy link
Collaborator

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
Copy link
Owner Author

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

この辺参考にしました!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、Controllerを利用して適切に記述できているかと思います。

@abeshi03
Copy link
Owner Author

ありがとうございます!
問題なさそうなのでmergeさせていただきます!

@abeshi03 abeshi03 merged commit f40e0c7 into main May 16, 2022
@abeshi03 abeshi03 deleted the fix/create_user branch May 16, 2022 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants