1
1
import React , { useState } from 'react'
2
- import PropTypes from 'prop-types'
3
2
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 ,
6
5
InputGroup , InputGroupAddon , InputGroupText , InputGroupButtonDropdown ,
7
6
DropdownMenu , DropdownToggle , DropdownItem } from 'reactstrap'
8
7
9
8
import Icon from '../../../../../../../Icon'
9
+ import { Input } from '../../../../../../../Form'
10
10
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
+ } ) => {
15
15
const [ dropdownOpen , setDropdownOpen ] = useState ( false )
16
16
17
17
return (
@@ -25,22 +25,16 @@ const CodingPair = (
25
25
/>
26
26
</ InputGroupText >
27
27
</ InputGroupAddon >
28
- < Control
29
- model = { `${ model } [${ index } ].label` }
28
+ < FastField
29
+ name = { `${ name } [${ index } ].label` }
30
30
placeholder = "label"
31
31
component = { Input }
32
- debounce = { 300 }
33
32
/>
34
- < Control
35
- model = { `${ model } [${ index } ].coding` }
33
+ < FastField
34
+ name = { `${ name } [${ index } ].coding` }
36
35
placeholder = "coding"
37
36
component = { Input }
38
- controlProps = { {
39
- style : {
40
- fontFamily : 'Fira Mono' ,
41
- }
42
- } }
43
- debounce = { 300 }
37
+ className = "text-monospace"
44
38
/>
45
39
< InputGroupButtonDropdown
46
40
addonType = "append"
@@ -57,26 +51,12 @@ const CodingPair = (
57
51
Add and delete
58
52
</ DropdownItem >
59
53
< 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 , { } ) }
70
55
>
71
56
Add below
72
57
</ DropdownItem >
73
58
< DropdownItem
74
- onClick = { ( ) => formDispatch (
75
- actions . remove (
76
- `local.items${ itemModel } ${ model } ` ,
77
- index
78
- )
79
- ) }
59
+ onClick = { ( ) => arrayHelpers . remove ( index ) }
80
60
>
81
61
Delete
82
62
</ DropdownItem >
@@ -85,23 +65,13 @@ const CodingPair = (
85
65
Move
86
66
</ DropdownItem >
87
67
< 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 ) }
94
69
disabled = { index === 0 }
95
70
>
96
71
Up
97
72
</ DropdownItem >
98
73
< 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 ) }
105
75
disabled = { index >= totalPairs - 1 }
106
76
>
107
77
Down
@@ -112,37 +82,33 @@ const CodingPair = (
112
82
)
113
83
}
114
84
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 ) ?? [ ]
118
88
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