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

Update checkbox docs #42

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions docs/src/guides/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,77 @@ const MyFormContent = () => {
Checkbox and radio inputs are different to all other field types as their state is declared by the `checked`
attribute rather than `value` ([more info here](https://github.com/andyrichardson/fielder/issues/23#issuecomment-576352847)).

To prevent hacking around with refs, you'll need to manually set the value and checked attributes.

```tsx
const [hobbiesProps] = useField({
name: 'hobbies'
name: 'hobbies',
initialValue: ['sports'],
});
const [termsProps] = useField({
name: 'terms'
});

const hobbiesOptions = useMemo(() => [
{ label: "Sports", value: "sports" },
{ label: "Coding", value: "coding" },
{ label: "Other", value: "other" },
], []);

const termsOptions = useMemo(() => [
{ label: "Yes", value: "yes" },
{ label: "No", value: "no" },
], [])

return (
<>
<fieldset>
<p>What are your hobbies</p>
<input type="checkbox" {...hobbiesProps} value="sports" /> Sports
<input type="checkbox" {...hobbiesProps} value="coding" /> Coding
<input type="checkbox" {...hobbiesProps} value="other" /> Other
{hobbiesOptions.map(({ label, value }) => (
<input
key={value}
type="checkbox"
{...hobbiesProps}
value={value}
checked={hobbiesProps.value.includes(value)}
/>
{label}
))}
</fieldset>
<fieldset>
<p>Do you accept the terms & conditions</p>
<input type="radio" {...termsProps} value="yes" /> Yes
<input type="radio" {...termsProps} value="no" /> No
{termsOptions.map(({ label, value }) => (
<input
key={value}
type="radio"
{...termsProps}
value={value}
checked={termsProps.value === value}
/>
{label}
))}
</fieldset>
</>
);
```

### Things to note

Use the `input` element, or a component which forwards the `ref` prop
The value attribute should be specified after spreading in your field props

```tsx
// This will prevent the checked state from being in sync
<input type="radio" {...radioProps} value="accept" ref={someOtherRef} />
```
// This will not work
<input type="radio" value="accept" {...radioProps} />

> Because of how checkboxes differ to other elements, `ref` access to checkbox and radio elements is required
// This will work
<input type="radio" {...radioProps} value="accept" />
```

**Do not** manually set the checked prop - this is handled by Fielder.
Set the initial value of your checkbox state to an array

```tsx
// This is unnecessary and will cause conflicts
<input type="radio" {...radioProps} value="accept" checked={someCondition} />
const [hobbiesProps] = useField({
name: 'hobbies',
initialValue: ['sports', 'coding'] // checkboxes with these values will be checked
});
```