This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FormArrayControl component and Adapters for radios and checkboxes (#1312
) * working FormArrayControl component and Adapters for radio and checkboxes * broken, but added a Field component to ease markup * rebuilt the howdiditstart page using checkbox component. * lint and passing tests. Not all new components have tests * added a test for FormArrayControl * cleaned up inputs, they also work properly now. * tweaked input and text-area to accept attributes from react-final-form * passing tests * added condition to prevent rendering of node if conditional field doesn't exist * some tweaks to ensure correct labelling of fields * #1312- Added the privacy consent checkbox fixes * #1312- Added whatWasAffected checkbox fixes * cleanups * Added the money lost checkbox fixes * Added the missing UI container * Added the information page checkbox fixes * Added the conditional form for single field * Merge conflicts * Added missing dependencies * Updated feedback forms * Added the final forms fixes * Remove unwanted copy - Not included in the mocks Co-authored-by: khalid elaggan <khalidelaggan@users.noreply.github.com>
- Loading branch information
1 parent
c664f9a
commit 35a9049
Showing
18 changed files
with
530 additions
and
477 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react' | ||
import { ThemeProvider } from 'emotion-theming' | ||
import canada from '../../../theme/canada' | ||
import { render, cleanup } from '@testing-library/react' | ||
import { Field } from '../' | ||
import { Form } from 'react-final-form' | ||
|
||
describe('<Field />', () => { | ||
afterEach(cleanup) | ||
|
||
it('renders', () => { | ||
const submitMock = jest.fn() | ||
|
||
render( | ||
<ThemeProvider theme={canada}> | ||
<Form | ||
initialValues="" | ||
onSubmit={submitMock} | ||
render={() => <Field name="name" label="foo" helperText="help" />} | ||
/> | ||
</ThemeProvider>, | ||
) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** @jsx jsx */ | ||
import { jsx } from '@emotion/core' | ||
import PropTypes from 'prop-types' | ||
import { FormErrorMessage, FormControl } from '@chakra-ui/core' | ||
import { FormHelperText } from '../FormHelperText' | ||
import { FormLabel } from '../FormLabel' | ||
import { Field as FieldAdapter } from 'react-final-form' | ||
import { UniqueID } from '../unique-id' | ||
import { Input } from '../input' | ||
|
||
export const Field = ({ name, label, helperText, errorMessage, component }) => { | ||
return ( | ||
<UniqueID> | ||
{id => { | ||
return ( | ||
<FormControl aria-labelledby={id}> | ||
<FormLabel id={id} htmlFor={name}> | ||
{label} | ||
</FormLabel> | ||
<FormHelperText>{helperText}</FormHelperText> | ||
<FormErrorMessage>{errorMessage}</FormErrorMessage> | ||
<FieldAdapter name={name} id={name} component={component} /> | ||
</FormControl> | ||
) | ||
}} | ||
</UniqueID> | ||
) | ||
} | ||
Field.defaultProps = { | ||
component: Input, | ||
} | ||
|
||
Field.propTypes = { | ||
children: PropTypes.any, | ||
name: PropTypes.string, | ||
label: PropTypes.any, | ||
helperText: PropTypes.any, | ||
errorMessage: PropTypes.any, | ||
} |
36 changes: 36 additions & 0 deletions
36
f2/src/components/FormArrayControl/__tests__/FormArrayControl.test.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react' | ||
import { ThemeProvider } from 'emotion-theming' | ||
import canada from '../../../theme/canada' | ||
import { render, cleanup } from '@testing-library/react' | ||
import { FormArrayControl } from '../' | ||
import { Form } from 'react-final-form' | ||
|
||
describe('<FormArrayControl />', () => { | ||
afterEach(cleanup) | ||
|
||
it('renders children', () => { | ||
const submitMock = jest.fn() | ||
|
||
const { getAllByText } = render( | ||
<ThemeProvider theme={canada}> | ||
<Form | ||
initialValues="" | ||
onSubmit={submitMock} | ||
render={() => ( | ||
<FormArrayControl name="foo" label="bar" helperText="help"> | ||
<p>all</p> | ||
</FormArrayControl> | ||
)} | ||
/> | ||
</ThemeProvider>, | ||
) | ||
|
||
const label = getAllByText(/bar/) | ||
const help = getAllByText(/help/) | ||
const test = getAllByText(/all/) | ||
|
||
expect(label).toHaveLength(1) | ||
expect(help).toHaveLength(1) | ||
expect(test).toHaveLength(1) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** @jsx jsx */ | ||
import PropTypes from 'prop-types' | ||
import { jsx } from '@emotion/core' | ||
import { FormErrorMessage, FormControl, Stack } from '@chakra-ui/core' | ||
import { useField } from 'react-final-form' | ||
import { FormLabel } from '../FormLabel' | ||
import { FormHelperText } from '../FormHelperText' | ||
import { UniqueID } from '../unique-id' | ||
|
||
export const FormArrayControl = ({ | ||
label, | ||
helperText, | ||
errorMessage, | ||
name, | ||
children, | ||
...rest | ||
}) => { | ||
const { | ||
meta: { error, touched }, | ||
} = useField(name, { subscription: { touched: true, error: true } }) | ||
return ( | ||
<UniqueID> | ||
{id => { | ||
return ( | ||
<FormControl | ||
as="fieldset" | ||
aria-labelledby={id} | ||
isInvalid={error && touched} | ||
{...rest} | ||
> | ||
<FormLabel id={id} as="legend" htmlFor={name}> | ||
{label} | ||
</FormLabel> | ||
<FormHelperText>{helperText}</FormHelperText> | ||
<FormErrorMessage>{errorMessage}</FormErrorMessage> | ||
{/** This component comes with a group attribute. We don't need to use Chakra's <CheckboxGroup> or <RadioGroup> as per the Chakra docs */} | ||
<Stack shouldWrapChildren spacing={4}> | ||
{children} | ||
</Stack> | ||
</FormControl> | ||
) | ||
}} | ||
</UniqueID> | ||
) | ||
} | ||
|
||
FormArrayControl.propTypes = { | ||
label: PropTypes.any, | ||
helperText: PropTypes.any, | ||
errorMessage: PropTypes.any, | ||
name: PropTypes.string, | ||
children: PropTypes.any, | ||
} |
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
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
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
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
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
Oops, something went wrong.