How to populate array with checkboxes?
See original GitHub issueHow to populate array with checkboxes?
Hi, I’m wondering if it’s possible to do this.
I have 2 check-boxes, and every time one is checked, an array should be populated. I’ve been playing around, even tried the latest FieldArray
, but I’m not sure I understand well how to use it, or even if it’s for that purpose.
Info
I am using Formik
component with render
method.
Something like:
import React from 'react';
import { render } from 'react-dom';
import { Formik } from 'formik';
const App = () => (
<div>
<h1>My Form</h1>
<Formik
initialValues={{
groups: [],
}}
onSubmit={values =>
console.log(values)
// Expected output would be something like:
// {groups: ['admin', 'normal']}
}
render={props => (
<form onSubmit={props.handleSubmit}>
<input
type="checkbox"
name="groups.0"
value="admin"
onChange={props.handleChange}
onBlur={props.handleBlur}
checked={props.values.groups === 'admin'}
/>
Admin
<br/>
<input
type="checkbox"
name="groups.1"
value="normal"
onChange={props.handleChange}
onBlur={props.handleBlur}
checked={props.values.groups === 'normal'}
/>
Normal
<br />
<button type="submit" >
Submit
</button>
</form>
)}
/>
</div>
);
render(<App />, document.getElementById('root'));
Here is an example: https://codesandbox.io/s/03pmmoo0kp
But what I really want is to have this as a result:
{
groups: ['admin', 'normal']
}
Is it possible doing something like that? What am I missing?
- Formik Version:
0.11-beta
- OS: Debian unstable
- Node Version:
node@v8.6.0
- Package Manager and version:
yarn@1.3.2
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (1 by maintainers)
Top Results From Across the Web
html - create and populate with DOM a checkbox list with array ...
Here's one way (pure JavaScript, no jQuery): var animals = ["lion", "tigers", "bears", "squirrels"]; var myDiv = document.
Read more >How to get all selected checkboxes in an array using jQuery
Given a number of checkboxes. The task is to get the value of all selected checkboxes in the array using JQuery.
Read more >Checkbox memory problem (was: returning an array) - Jazz.net
So after realize() the string array storage can perish? I think I can work with that. The only reason I was using stringhttp://]...
Read more >Get multiple selected (checked) CheckBox values in Array ...
When the Get Button is clicked, the CheckBoxes will be referenced using a loop. Inside the loop if the CheckBox is selected (checked)...
Read more >JavaScript Array of checkboxes - YouTube
JavaScript Array of checkboxes. Watch later. Share. Copy link. Info. Shopping. Tap to unmute. If playback doesn't begin shortly, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Example for a checkbox array using FieldArray: https://codesandbox.io/s/o5pw1vx916
@jaredpalmer - now v0.11 is released and
FieldArray
’s stable, is it possible to add examples of creating fieldsets of multiple radio and checkbox inputs please? Seems such a common feature of forms, but yet to see a definitive example in the docs. Thanks!