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

examples(RHF): support for default{Value,Checked} & useFieldArray #134

Merged
merged 4 commits into from Oct 30, 2022

Conversation

koichik
Copy link
Member

@koichik koichik commented Oct 29, 2022

Support for defaultValue

Use registerWithDefaultValue instead of register:

const { registerWithDefaultValue, ... } = useFormSync<FormState>(formState({ name: 'Foo' }))
...
  <input type="text" {...registerWithDefaultValue('name')} />

Even SSR/SSG, it is rendered with default values.

Support for defaultChecked

Use registerWithDefaultChecked instead of register

const { registerWithDefaultChecked, ... } = useFormSync<FormState>(formState({
  selectOne: '1', // for radio
  selectMany: ['1', '2'], // for checkbox
}))
...
  <input type="radio" {...registerWithDefaultChecked('selectOne', '1') /* defaults to checked */} />
  <input type="radio" {...registerWithDefaultChecked('selectOne', '2') /* defaults to not checked */} />
  <input type="radio" {...registerWithDefaultChecked('selectOne', '3') /* defaults to not checked */} />

  <input type="checkbox" {...registerWithDefaultChecked('selectMany', '1') /* defaults to checked */} />
  <input type="checkbox" {...registerWithDefaultChecked('selectMany', '2') /* defaults to checked */} />
  <input type="checkbox" {...registerWithDefaultChecked('selectMany', '3') /* defaults to not checked */} />

Support for useFieldArray

Use useFieldArraySync returns from useFormSync instead of useFieldArray:

const { useFieldArraySync, control, ... } = useFormSync<FormState>(formState({items: [{value: 1}, {value: 2}] })
const { fields, ...} = useFieldArraySync({ control, name: 'items' })
...
  {fields.map((field, index) => (
    <input key={field.id} {...register(`items.${index}.value` as const)} />
  ))}

Of course, it is possible to use registerWithDefaultValue instead of register.

@koichik koichik force-pushed the default-value-and-field-array branch from 4bb5e51 to fc640ff Compare October 29, 2022 06:49
@koichik koichik force-pushed the default-value-and-field-array branch from fc640ff to 12b2310 Compare October 29, 2022 09:19
@koichik koichik force-pushed the default-value-and-field-array branch from ea37744 to 4daabb9 Compare October 29, 2022 12:38
@koichik koichik changed the title example: support for default value & useFieldArray examples(RHF): support for default{Value,Checked} & useFieldArray Oct 29, 2022
@koichik koichik force-pushed the default-value-and-field-array branch from 4daabb9 to 0a1dd78 Compare October 29, 2022 18:28
Copy link
Collaborator

@AkifumiSato AkifumiSato left a comment

Choose a reason for hiding this comment

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

I asked a few questions, but LGTM.
Thanks!

}, [setFormValues, getValues])

const useFieldArraySync = (
props: UseFieldArrayProps<TFieldValues>
Copy link
Collaborator

Choose a reason for hiding this comment

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

imo: RHFのuseFieldArrayはimportして使うと思うので、↓みたくしてimprot { useFieldArraySync }して使うというのは微妙ですかね?
その方がhooksっぽいかなって気もしたものの、自信ないのでご意見いただきたいです🙇‍♂️

const { ... } = useFieldArraySync({
  onChangeForm,
  props,
})

Copy link
Member Author

Choose a reason for hiding this comment

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

最初はまさにそのつもりだったのですが、将来onChangeForm()以外を渡さないといけなくなる可能性を考えると最初からクロージャにしちゃった方がいいだろうと思いました
別案としてはuseFormSync()からラップしたcontrolを返してそれをuseFieldArraySync()に渡すというのも考えたのですが、ラップするオブジェクトが増え続けるのもどうかかと思いクロージャ案を選びました
useFormSync()するコンポーネントとuseFieldArraySync() するコンポーネントが分かれる場合はcontrolをラップするのが一番使い勝手がいい気はします
他の2案ではcontrolに加えてuseFieldArraySynconChangeFormのどちらかも一緒に子コンポーネントに渡さないといけないので

Copy link
Member 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.

将来onChangeForm()以外を渡さないといけなくなる可能性を考えると最初からクロージャにしちゃった方がいいだろうと思いました

確かにですね...
方針納得しました!

useFormSync()するコンポーネントとuseFieldArraySync() するコンポーネントが分かれる場合はcontrolをラップするのが一番使い勝手がいい気はします

一応知っておきたいんですが、ラップするというのは↓みたいなイメージであってますか?

const { control } = useFormSync(...)
// controlはRHFのcontrol + onChangeForm

Copy link
Member Author

Choose a reason for hiding this comment

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

そのイメージです

Copy link
Member Author

Choose a reason for hiding this comment

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

方針納得しました!

とのことなのでこれでマージします!

@koichik koichik merged commit 677dbe4 into main Oct 30, 2022
@koichik koichik deleted the default-value-and-field-array branch October 30, 2022 14:40
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