From 8ec281728d5525e28ff1f5081bb0fafdcc1982fe Mon Sep 17 00:00:00 2001 From: Philippe Caron Date: Thu, 6 Feb 2020 17:57:30 -0500 Subject: [PATCH 01/22] working FormArrayControl component and Adapters for radio and checkboxes --- f2/src/components/FormArrayControl/index.js | 29 ++++++ f2/src/components/FormHelperText/index.js | 8 +- f2/src/components/FormLabel/index.js | 2 +- f2/src/components/checkbox/index.js | 26 +++++- f2/src/components/radio/index.js | 97 ++++++++++++--------- f2/src/forms/HowDidItStartForm.js | 69 ++++++++------- 6 files changed, 152 insertions(+), 79 deletions(-) create mode 100644 f2/src/components/FormArrayControl/index.js diff --git a/f2/src/components/FormArrayControl/index.js b/f2/src/components/FormArrayControl/index.js new file mode 100644 index 000000000..756fba13c --- /dev/null +++ b/f2/src/components/FormArrayControl/index.js @@ -0,0 +1,29 @@ +/** @jsx jsx */ +import { jsx } from '@emotion/core' +import { FormErrorMessage, FormControl } from '@chakra-ui/core' +import { useField } from 'react-final-form' +import { FormLabel } from '../FormLabel' +import { FormHelperText } from '../FormHelperText' + +export const FormArrayControl = ({ + label, + helperText, + errorMessage, + name, + children, + ...rest +}) => { + const { + meta: { error, touched }, + } = useField(name, { subscription: { touched: true, error: true } }) + return ( + + + {label} + + {helperText} + {errorMessage} + {children} + + ) +} diff --git a/f2/src/components/FormHelperText/index.js b/f2/src/components/FormHelperText/index.js index 0c354109a..7bb1a8320 100644 --- a/f2/src/components/FormHelperText/index.js +++ b/f2/src/components/FormHelperText/index.js @@ -6,11 +6,11 @@ export const FormHelperText = styled(ChakraFormHelperText)( variant({ variants: { above: { - mt: 0, - mb: 2, + mt: -2, + mb: 4, }, below: { - mt: 2, + mt: 4, mb: 0, }, unstyled: { @@ -22,7 +22,7 @@ export const FormHelperText = styled(ChakraFormHelperText)( FormHelperText.defaultProps = { variant: 'above', - as: 'div', + as: 'p', fontSize: 'md', color: 'black', fontFamily: 'body', diff --git a/f2/src/components/FormLabel/index.js b/f2/src/components/FormLabel/index.js index 8e067e37c..7a498ff04 100644 --- a/f2/src/components/FormLabel/index.js +++ b/f2/src/components/FormLabel/index.js @@ -7,7 +7,7 @@ FormLabel.defaultProps = { fontSize: 'xl', fontWeight: 'bold', fontFamily: 'body', - pb: 1, + mb: 2, lineHeight: 1, maxW: '600px', } diff --git a/f2/src/components/checkbox/index.js b/f2/src/components/checkbox/index.js index 133f9d561..b28119167 100644 --- a/f2/src/components/checkbox/index.js +++ b/f2/src/components/checkbox/index.js @@ -4,8 +4,31 @@ import { jsx } from '@emotion/core' import { Text } from '../text' import { UniqueID } from '../unique-id' import { Box, VisuallyHidden, ControlBox, Icon, Flex } from '@chakra-ui/core' +import { useField } from 'react-final-form' -export const Checkbox = ({ label, isChecked, ...props }) => { +export const CheckboxAdapter = ({ + name, + value, + defaultIsChecked, + children, +}) => { + const { + input: { checked, ...input }, + meta: { error, touched }, + } = useField(name, { + type: 'checkbox', // important for RFF to manage the checked prop + value, // important for RFF to manage list of strings + defaultIsChecked, + }) + + return ( + + {children} + + ) +} + +export const Checkbox = ({ input, label, isChecked, ...props }) => { return ( {id => { @@ -13,6 +36,7 @@ export const Checkbox = ({ label, isChecked, ...props }) => { { +export const RadioAdapter = ({ name, value, defaultIsChecked, children }) => { + const { + input: { checked, ...input }, + meta: { error, touched }, + } = useField(name, { + type: 'radio', + value, + defaultIsChecked, + }) + + return ( + + {children} + + ) +} + +export const Radio = ({ input, label, isChecked, ...props }) => { return ( {id => { return ( - - - + + - - - + + + - - {props.children} - - - + + {props.children} + + ) }} diff --git a/f2/src/forms/HowDidItStartForm.js b/f2/src/forms/HowDidItStartForm.js index f99f907d2..ea82e51f8 100644 --- a/f2/src/forms/HowDidItStartForm.js +++ b/f2/src/forms/HowDidItStartForm.js @@ -5,13 +5,20 @@ import { Trans } from '@lingui/macro' import { Form, Field, useField } from 'react-final-form' import { NextAndCancelButtons } from '../components/next-and-cancel-buttons' import { Checkbox } from '../components/checkbox' -import { Radio } from '../components/radio' -import { FormControl, Stack, Box } from '@chakra-ui/core' +import { Radio, RadioAdapter } from '../components/radio' +import { + FormControl, + Stack, + Box, + RadioGroup, + CheckboxGroup, +} from '@chakra-ui/core' import { FormHelperText } from '../components/FormHelperText' import { TextArea } from '../components/text-area' import { useStateValue } from '../utils/state' import { FormLabel } from '../components/FormLabel' import { ConditionalForm } from '../components/container' +import { FormArrayControl } from '../components/FormArrayControl' const Control = ({ name, ...rest }) => { const { @@ -165,14 +172,12 @@ export const HowDidItStartForm = props => { shouldWrapChildren spacing={12} > - - - - - - - - + } + helperText={} + > + {questionsList.map(question => { return ( @@ -210,50 +215,50 @@ export const HowDidItStartForm = props => { ) })} - - + + - - - - - + } + > + {whenDidItStart.map(key => { return ( - - + {i18n._(key)} - - + + ) })} - - + + - - - - - + } + > + {howManyTimes.map(key => { return ( - {i18n._(key)} - + ) })} - - + + } button={} From 43b350c6dd894f65902fe71ac72e3a1bbda5f78a Mon Sep 17 00:00:00 2001 From: Philippe Caron Date: Mon, 10 Feb 2020 13:45:59 -0500 Subject: [PATCH 02/22] broken, but added a Field component to ease markup --- .../components/Field/__tests__/Field.test.js | 0 f2/src/components/Field/index.js | 31 +++++++ f2/src/components/FormArrayControl/index.js | 9 ++ f2/src/components/checkbox/index.js | 21 +++-- f2/src/components/radio/index.js | 90 ++++++++++--------- f2/src/forms/HowDidItStartForm.js | 12 +++ 6 files changed, 117 insertions(+), 46 deletions(-) create mode 100644 f2/src/components/Field/__tests__/Field.test.js create mode 100644 f2/src/components/Field/index.js diff --git a/f2/src/components/Field/__tests__/Field.test.js b/f2/src/components/Field/__tests__/Field.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/f2/src/components/Field/index.js b/f2/src/components/Field/index.js new file mode 100644 index 000000000..40325bd5a --- /dev/null +++ b/f2/src/components/Field/index.js @@ -0,0 +1,31 @@ +/** @jsx jsx */ +import { jsx } from '@emotion/core' +import PropTypes from 'prop-types' +import { render } from 'react-dom' +import { FormErrorMessage, FormControl } from '@chakra-ui/core' +import { FormHelperText } from '../FormHelperText' +import { FormLabel } from '../FormLabel' +import { Field as FieldAdapter } from 'react-final-form' + +export const Field = (name, label, helperText, errorMessage, children) => { + render( + + {props => ( + + {label} + {helperText} + {errorMessage} + {children} + + )} + , + ) +} + +Field.propTypes = { + children: PropTypes.any, + name: PropTypes.string, + label: PropTypes.object, + helperText: PropTypes.object, + errorMessage: PropTypes.object, +} diff --git a/f2/src/components/FormArrayControl/index.js b/f2/src/components/FormArrayControl/index.js index 756fba13c..21ea7ec8b 100644 --- a/f2/src/components/FormArrayControl/index.js +++ b/f2/src/components/FormArrayControl/index.js @@ -1,4 +1,5 @@ /** @jsx jsx */ +import PropTypes from 'prop-types' import { jsx } from '@emotion/core' import { FormErrorMessage, FormControl } from '@chakra-ui/core' import { useField } from 'react-final-form' @@ -27,3 +28,11 @@ export const FormArrayControl = ({ ) } + +FormArrayControl.propTypes = { + label: PropTypes.object, + helperText: PropTypes.object, + errorMessage: PropTypes.object, + name: PropTypes.string, + children: PropTypes.any, +} diff --git a/f2/src/components/checkbox/index.js b/f2/src/components/checkbox/index.js index b28119167..5751a78a1 100644 --- a/f2/src/components/checkbox/index.js +++ b/f2/src/components/checkbox/index.js @@ -1,10 +1,12 @@ /** @jsx jsx */ +import React from 'react' import PropTypes from 'prop-types' import { jsx } from '@emotion/core' import { Text } from '../text' import { UniqueID } from '../unique-id' import { Box, VisuallyHidden, ControlBox, Icon, Flex } from '@chakra-ui/core' import { useField } from 'react-final-form' +import { ConditionalForm } from '../container' export const CheckboxAdapter = ({ name, @@ -28,13 +30,19 @@ export const CheckboxAdapter = ({ ) } -export const Checkbox = ({ input, label, isChecked, ...props }) => { +export const Checkbox = ({ + input, + label, + isChecked, + conditionalField, + ...props +}) => { return ( {id => { return ( - - + + { {props.children} - + + {isChecked && {conditionalField}} + ) }} ) } -Checkbox.defaultProps = {} - Checkbox.propTypes = { + conditionalField: PropTypes.object, children: PropTypes.any, } diff --git a/f2/src/components/radio/index.js b/f2/src/components/radio/index.js index 04503b5b2..9cb805eb8 100644 --- a/f2/src/components/radio/index.js +++ b/f2/src/components/radio/index.js @@ -1,10 +1,12 @@ /** @jsx jsx */ +import React from 'react' import PropTypes from 'prop-types' import { jsx } from '@emotion/core' import { Text } from '../text' import { UniqueID } from '../unique-id' import { Box, VisuallyHidden, ControlBox, Flex } from '@chakra-ui/core' import { useField } from 'react-final-form' +import { ConditionalForm } from '../container' export const RadioAdapter = ({ name, value, defaultIsChecked, children }) => { const { @@ -23,58 +25,66 @@ export const RadioAdapter = ({ name, value, defaultIsChecked, children }) => { ) } -export const Radio = ({ input, label, isChecked, ...props }) => { +export const Radio = ({ + input, + label, + isChecked, + conditionalField, + ...props +}) => { return ( {id => { return ( - - + + + - - - + + + - - {props.children} - - + + {props.children} + + + {isChecked && {conditionalField}} + ) }} ) } -Radio.defaultProps = {} - Radio.propTypes = { children: PropTypes.any, + conditionalField: PropTypes.object, } diff --git a/f2/src/forms/HowDidItStartForm.js b/f2/src/forms/HowDidItStartForm.js index ea82e51f8..7ee0b58e6 100644 --- a/f2/src/forms/HowDidItStartForm.js +++ b/f2/src/forms/HowDidItStartForm.js @@ -19,6 +19,7 @@ import { useStateValue } from '../utils/state' import { FormLabel } from '../components/FormLabel' import { ConditionalForm } from '../components/container' import { FormArrayControl } from '../components/FormArrayControl' +import { Field as ConditionalField } from '../components/Field' const Control = ({ name, ...rest }) => { const { @@ -189,6 +190,17 @@ export const HowDidItStartForm = props => { )} > {i18n._(question.channel)} + } + helperText={} + > +