1

I have this data:

...
initialValues: {
  alarm: 'normal',
  days: { mon: true, tue: true, wed: true, thu: false, friday: true }
}
...

to render, I do the following.

<Field name="days" component={({ input }) => {
  return (
    <div className='days-container'>
      <Checkbox label='Monday' checked={input.value.mon} onChange={input.onChange} />
      <Checkbox label='Tuesday' checked={input.value.tue} onChange={input.onChange} />
      ...
    </div>
  )
}}/>

but days value becomes Boolean:

days: true

as you would to maintain the initial structure?

2
  • you can edit the post you know? Commented Sep 21, 2016 at 21:53
  • Modified, I did not see the option to edit Commented Sep 21, 2016 at 22:43

1 Answer 1

1

I did it this way

<Field name="days" label='Days' component={({ input, label }) => {
  return (
    <div className='days-container'>
      <span className='label'>{label}</span>
      {[ 'mon', 'tue', 'wed', 'thu', 'friday'].map((name, index) =>
          <Checkbox label={name} checked={input.value[name]} key={index} onChange={checked => {
            const setter = {};
            setter[name]=checked;
            input.onChange({...input.value, ...setter})}} />
      )}
    </div>
  )
}}/>

there a better way?

Sign up to request clarification or add additional context in comments.

1 Comment

This is really good. In my case I ended up creating a custom reducer plugin for redux form and handling each tickbox separately by listening on it's onclick event... This is much easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.