Skip to content

Commit

Permalink
refactor(ApiUserForm): Use Formik.Field to wire inputs automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Sep 30, 2020
1 parent d2d6dd9 commit eee3104
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions components/ApiUserForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clone from 'clone'
import { Formik } from 'formik'
import { Field, Formik } from 'formik'
import { Component } from 'react'
import { Button, Card, Col, Container, Form, Row } from 'react-bootstrap'
import { withAuth } from 'use-auth0-hooks'
Expand Down Expand Up @@ -108,10 +108,7 @@ class ApiUserForm extends Component {
initialValues={apiUser}
>
{({
handleBlur,
handleSubmit,
handleChange,
values,
touched,
errors
}) => (
Expand All @@ -131,14 +128,12 @@ class ApiUserForm extends Component {
return (
<Form.Group key={fieldIndex}>
<Form.Label>{field.title}</Form.Label>
<Form.Control
<Field
as={Form.Control}
disabled={!createUser}
isInvalid={touched[fieldName] && !!errors[fieldName]}
name={fieldName}
onBlur={handleBlur}
onChange={handleChange}
type='text'
value={values[fieldName]}
// onBlur, onChange, and value are passed automatically.
/>
<Form.Control.Feedback type='invalid'>
{errors[fieldName]}
Expand All @@ -157,8 +152,8 @@ class ApiUserForm extends Component {

<div className='mt-3'>
<Form.Group>
<Form.Check
checked={values.hasConsentedToTerms}
<Field
as={Form.Check}
disabled={!createUser}
feedback={errors.hasConsentedToTerms}
isInvalid={touched.hasConsentedToTerms && !!errors.hasConsentedToTerms}
Expand All @@ -170,9 +165,7 @@ class ApiUserForm extends Component {
</>
}
name='hasConsentedToTerms'
onBlur={handleBlur}
onChange={handleChange}
type='checkbox'
// onBlur, onChange, and value are passed automatically.
/>
</Form.Group>
</div>
Expand Down

0 comments on commit eee3104

Please sign in to comment.