Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 1728226

Browse files
committed
Working on render functions
1 parent 7b56197 commit 1728226

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

components/molecules/Form/Form.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import cn from 'classnames'
77
/**
88
* Render Form component.
99
*
10-
* @param {object} props The component attributes as props.
11-
* @param {Element} props.children Form children elements.
12-
* @param {string} props.className Form wrapper class.
13-
* @param {object} props.formDefaults Formik default data.
14-
* @param {string|number} props.id Form id.
15-
* @param {object} props.validationSchema Yup validation schema object.
16-
* @param {Function} props.onSubmit Function to execute when form is submitted
17-
* @return {Element} The Form component.
10+
* @param {object} props The component attributes as props.
11+
* @param {Element|Function} props.children Form children elements.
12+
* @param {string} props.className Form wrapper class.
13+
* @param {object} props.formDefaults Formik default data.
14+
* @param {string|number} props.id Form id.
15+
* @param {object} props.validationSchema Yup validation schema object.
16+
* @param {Function} props.onSubmit Function to execute when form is submitted
17+
* @return {Element} The Form component.
1818
*/
1919
export default function Form({
2020
children,
@@ -24,22 +24,29 @@ export default function Form({
2424
validationSchema,
2525
onSubmit
2626
}) {
27+
let formattedChildren = children
28+
if ('function' !== typeof children) {
29+
formattedChildren = () => children
30+
}
31+
2732
return (
2833
<Formik
2934
initialValues={formDefaults}
3035
validationSchema={validationSchema}
3136
onSubmit={onSubmit}
3237
>
33-
<FormikForm id={id} className={cn(styles.form, className)}>
34-
{children}
35-
<button type="submit">Submit</button>
36-
</FormikForm>
38+
{(formikProps) => (
39+
<FormikForm id={id} className={cn(styles.form, className)}>
40+
{formattedChildren(formikProps)}
41+
<button type="submit">Submit</button>
42+
</FormikForm>
43+
)}
3744
</Formik>
3845
)
3946
}
4047

4148
Form.propTypes = {
42-
children: PropTypes.node,
49+
children: PropTypes.object,
4350
className: PropTypes.string,
4451
formDefaults: PropTypes.object,
4552
id: PropTypes.string,

components/molecules/GravityForm/Fields/File.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@ import cn from 'classnames'
1414
* @param {string|number} props.id GravityForm field id.
1515
* @param {boolean} props.isRequired GravityForm field is required.
1616
* @param {string} props.label GravityForm field label.
17-
* @param {Array} props.selectChoices GravityForm field selection options.
18-
* @param {boolean} props.visibility GravityForm field visibility.
19-
* @return {Element} The Select component.
17+
* @param {boolean} props.visibility GravityForm visibility option.
18+
* @param {Function} props.setFieldValue Formik function to set state.
19+
* @return {Element} The File component.
2020
*/
21-
export default function File({className, description, id, isRequired, label}) {
21+
export default function File({
22+
className,
23+
description,
24+
id,
25+
isRequired,
26+
label,
27+
visibility,
28+
setFieldValue
29+
}) {
2230
const fieldId = getGfFieldId(id)
23-
24-
// const [upload, setUpload] = useState([])
31+
const isHiddenClass = getGfHiddenClassName(visibility)
32+
const thisClassName = cn(className, isHiddenClass)
2533

2634
return (
27-
<div className={cn(styles.text, className)}>
35+
<div className={cn(styles.text, thisClassName)}>
2836
{label && (
2937
<label htmlFor={id} required={isRequired}>
3038
{label}
@@ -36,25 +44,10 @@ export default function File({className, description, id, isRequired, label}) {
3644
name={fieldId}
3745
required={isRequired}
3846
type="file"
39-
render={({field, form}) => (
40-
<input
41-
{...field}
42-
type="file"
43-
onChange={e => {
44-
form.setFieldValue(fieldId, e.currentTarget.files[0])
45-
}}
46-
/>
47-
)}
48-
/>
49-
{/* <input
50-
id={fieldId}
51-
name={fieldId}
52-
type="file"
53-
onChange={(event) => {
54-
setFieldValue(fieldId, event.currentTarget.files[0])
47+
onChange={(e) => {
48+
setFieldValue(fieldId, e.currentTarget.files[0])
5549
}}
56-
required={isRequired}
57-
/> */}
50+
/>
5851
{description && <p>{description}</p>}
5952
<InputError name={id} />
6053
</div>
@@ -68,5 +61,6 @@ File.propTypes = {
6861
isRequired: PropTypes.bool,
6962
label: PropTypes.string,
7063
selectChoices: PropTypes.arrayOf(PropTypes.object),
71-
visibility: PropTypes.string
64+
visibility: PropTypes.string,
65+
setFieldValue: PropTypes.func
7266
}

components/molecules/GravityForm/GravityForm.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ export default function GravityForm({
3131
formDefaults={fieldDefaults}
3232
id={formId && `gform-${formId}`}
3333
validationSchema={formValidationSchema}
34-
onSubmit={(values, actions) => console.log({values, actions})}
34+
// onSubmit={(values, actions) => console.log({values, actions})}
3535
>
36-
{title && <h2 className={styles.title}>{title}</h2>}
37-
{fieldData && <Fields fields={fieldData} />}
36+
{(formikProps) => (
37+
<>
38+
{title && <h2 className={styles.title}>{title}</h2>}
39+
{fieldData && <Fields fields={fieldData} formikProps={formikProps} />}
40+
</>
41+
)}
3842
</Form>
3943
)
4044
}

0 commit comments

Comments
 (0)