Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 5f38675

Browse files
Port coding options to formik
1 parent fdb64c3 commit 5f38675

File tree

1 file changed

+45
-79
lines changed
  • packages/builder/src/components/ComponentOptions/components/Content/Page/components/ItemOptions/components

1 file changed

+45
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React, { useState } from 'react'
2-
import PropTypes from 'prop-types'
32

4-
import { Control, actions } from 'react-redux-form'
5-
import { Row, Col, Input,
3+
import { FastField, FieldArray, useFormikContext, getIn } from 'formik'
4+
import { Row, Col,
65
InputGroup, InputGroupAddon, InputGroupText, InputGroupButtonDropdown,
76
DropdownMenu, DropdownToggle, DropdownItem } from 'reactstrap'
87

98
import Icon from '../../../../../../../Icon'
9+
import { Input } from '../../../../../../../Form'
1010

11-
const CodingPair = (
12-
{ icon, iconFallbackWeight='r', model, index, itemModel, totalPairs },
13-
{ formDispatch }
14-
) => {
11+
const CodingPair = ({
12+
name, index, arrayHelpers,
13+
icon, iconFallbackWeight='r', totalPairs
14+
}) => {
1515
const [dropdownOpen, setDropdownOpen] = useState(false)
1616

1717
return (
@@ -25,22 +25,16 @@ const CodingPair = (
2525
/>
2626
</InputGroupText>
2727
</InputGroupAddon>
28-
<Control
29-
model={ `${ model }[${ index }].label` }
28+
<FastField
29+
name={ `${ name }[${ index }].label` }
3030
placeholder="label"
3131
component={ Input }
32-
debounce={ 300 }
3332
/>
34-
<Control
35-
model={ `${ model }[${ index }].coding` }
33+
<FastField
34+
name={ `${ name }[${ index }].coding` }
3635
placeholder="coding"
3736
component={ Input }
38-
controlProps={{
39-
style: {
40-
fontFamily: 'Fira Mono',
41-
}
42-
}}
43-
debounce={ 300 }
37+
className="text-monospace"
4438
/>
4539
<InputGroupButtonDropdown
4640
addonType="append"
@@ -57,26 +51,12 @@ const CodingPair = (
5751
Add and delete
5852
</DropdownItem>
5953
<DropdownItem
60-
onClick={ () => formDispatch(
61-
actions.change(
62-
`local.items${ itemModel }${ model }`,
63-
items => [
64-
...items.slice(0, index + 1),
65-
{},
66-
...items.slice(index + 1, items.length)
67-
]
68-
)
69-
) }
54+
onClick={ () => arrayHelpers.insert(index + 1, {}) }
7055
>
7156
Add below
7257
</DropdownItem>
7358
<DropdownItem
74-
onClick={ () => formDispatch(
75-
actions.remove(
76-
`local.items${ itemModel }${ model }`,
77-
index
78-
)
79-
) }
59+
onClick={ () => arrayHelpers.remove(index) }
8060
>
8161
Delete
8262
</DropdownItem>
@@ -85,23 +65,13 @@ const CodingPair = (
8565
Move
8666
</DropdownItem>
8767
<DropdownItem
88-
onClick={ () => formDispatch(
89-
actions.move(
90-
`local.items${ itemModel }${ model }`,
91-
index, index - 1
92-
)
93-
) }
68+
onClick={ () => arrayHelpers.swap(index, index - 1) }
9469
disabled={ index === 0 }
9570
>
9671
Up
9772
</DropdownItem>
9873
<DropdownItem
99-
onClick={ () => formDispatch(
100-
actions.move(
101-
`local.items${ itemModel }${ model }`,
102-
index, index + 1
103-
)
104-
) }
74+
onClick={ () => arrayHelpers.swap(index, index + 1) }
10575
disabled={ index >= totalPairs - 1 }
10676
>
10777
Down
@@ -112,37 +82,33 @@ const CodingPair = (
11282
)
11383
}
11484

115-
CodingPair.contextTypes = {
116-
formDispatch: PropTypes.func,
117-
}
85+
export const CodingGroup = ({ name, icon, iconFallbackWeight }) => {
86+
const { values } = useFormikContext()
87+
const data = getIn(values, name) ?? []
11888

119-
export const CodingGroup = ({
120-
data=[], model, itemModel,
121-
icon, iconFallbackWeight
122-
}) =>
123-
// TODO: Simplify form field coordination. Splitting the path
124-
// into model, itemmodel and index seems overly complicated
125-
// (the full path still needs to be reconstructed at some point,
126-
// because the action creator above needs access to it -- sadly,
127-
// the fieldset doesn't help here.)
128-
// ----
129-
// Also, think about moving the manipulation logic out of the
130-
// individual pairs, and up to a higher level such as this one.
131-
<div className="pt-1">
132-
{
133-
[...data, {}].map((x, i) =>
134-
<Row form key={ `coding-${ model }-${ i }` }>
135-
<Col className="pt-1">
136-
<CodingPair
137-
icon={ icon }
138-
iconFallbackWeight={ iconFallbackWeight }
139-
model={ `${ model }` }
140-
itemModel={ itemModel }
141-
index={ i }
142-
totalPairs={ data.length }
143-
/>
144-
</Col>
145-
</Row>
146-
)
147-
}
148-
</div>
89+
return (
90+
// TODO: Think about moving the manipulation logic out of the
91+
// individual pairs, and up to a higher level such as this one.
92+
<div className="pt-1">
93+
<FieldArray
94+
name={ name }
95+
render={ arrayHelpers =>
96+
[...data, {}].map((x, i) => (
97+
<Row form key={ `coding-${ name }-${ i }` }>
98+
<Col className="pt-1">
99+
<CodingPair
100+
name={ `${ name }` }
101+
index={ i }
102+
arrayHelpers={ arrayHelpers }
103+
icon={ icon }
104+
iconFallbackWeight={ iconFallbackWeight }
105+
totalPairs={ data.length }
106+
/>
107+
</Col>
108+
</Row>
109+
) )
110+
}
111+
/>
112+
</div>
113+
)
114+
}

0 commit comments

Comments
 (0)