Created
July 14, 2019 02:17
-
-
Save luandevpro/665a77761392461b574d2bbe022986b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Formik, Form, Field, FieldArray } from 'formik'; | |
import styled from 'styled-components'; | |
const languageState = ['vi', 'en', 'jp']; | |
export default () => { | |
const handleSubmit = values => { | |
console.log(values.languages); | |
}; | |
return ( | |
<Formik | |
onSubmit={handleSubmit} | |
initialValues={{ | |
languages: [], | |
}} | |
> | |
{() => ( | |
<Form> | |
<FieldArray name="languages"> | |
{arrayHelpers => { | |
return languageState.map((language, index) => { | |
return ( | |
<StyledWrap | |
key={index} | |
htmlFor={`languages.${index}.language`} | |
style={{ marginBottom: '10px' }} | |
> | |
<label htmlFor={`languages.${index}.language`}> | |
<input | |
id={`languages.${index}.language`} | |
type="radio" | |
name={`languages.${index}.language`} | |
value={language} | |
checked={arrayHelpers.form.values.languages.includes(language)} | |
onChange={e => { | |
if (e.target.checked && arrayHelpers.form.values.languages.length === 0) | |
arrayHelpers.push(language); | |
else { | |
arrayHelpers.replace(0, language); | |
} | |
}} | |
className="check-custom" | |
/> | |
<span className="check-toggle" /> | |
<label style={{ marginLeft: '10px' }} htmlFor={`languages.${index}.language`}> | |
{language} | |
</label> | |
</label> | |
</StyledWrap> | |
); | |
}); | |
}} | |
</FieldArray> | |
<br /> | |
<button type="submit">submit</button> | |
</Form> | |
)} | |
</Formik> | |
); | |
}; | |
const StyledWrap = styled.div``; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment