1
- import React , { memo , Fragment , useMemo , useState , useCallback } from 'react'
1
+ import React , { memo , Fragment , useMemo , useState , useCallback , MouseEvent } from 'react'
2
2
import styled from 'styled-components'
3
3
import { PropertyHeader } from './PropertyHeader'
4
4
import ControlsGroup from './ControlsGroup'
5
5
import { Cell , Toggle } from './styled'
6
6
import { Help } from './Help'
7
+ import { Flavor , ChartProperty } from '../../types'
8
+ import { ArrayControlConfig } from './types'
7
9
8
10
interface ArrayControlProps {
9
- /*
10
- property: PropTypes.object.isRequired,
11
- value: PropTypes.array.isRequired,
12
- flavors: PropTypes.arrayOf(PropTypes.oneOf(['svg', 'html', 'canvas', 'api'])).isRequired,
13
- currentFlavor: PropTypes.oneOf(['svg', 'html', 'canvas', 'api']).isRequired,
14
- options: PropTypes.shape({
15
- props: PropTypes.array.isRequired,
16
- shouldCreate: PropTypes.bool,
17
- addLabel: PropTypes.string,
18
- shouldRemove: PropTypes.bool,
19
- removeLabel: PropTypes.string,
20
- defaults: PropTypes.object,
21
- getItemTitle: PropTypes.func,
22
- }).isRequired,
23
- onChange: PropTypes.func.isRequired,
24
- */
11
+ id : string
12
+ property : ChartProperty
13
+ value : unknown [ ]
14
+ flavors : Flavor [ ]
15
+ currentFlavor : Flavor
16
+ config : ArrayControlConfig
17
+ onChange : ( value : unknown ) => void
18
+ context ?: any
25
19
}
26
20
27
- const ArrayControl = memo (
21
+ export const ArrayControl = memo (
28
22
( {
29
23
property,
30
24
flavors,
31
25
currentFlavor,
32
26
value,
33
27
onChange,
34
- options : {
28
+ config : {
35
29
props,
36
30
shouldCreate = false ,
37
31
addLabel = 'add' ,
@@ -40,23 +34,24 @@ const ArrayControl = memo(
40
34
defaults = { } ,
41
35
getItemTitle,
42
36
} ,
43
- } ) => {
37
+ } : ArrayControlProps ) => {
44
38
const [ activeItems , setActiveItems ] = useState ( [ 0 ] )
45
39
const append = useCallback ( ( ) => {
46
40
onChange ( [ ...value , { ...defaults } ] )
47
41
setActiveItems ( [ value . length ] )
48
42
} , [ value , onChange , defaults , setActiveItems ] )
43
+
49
44
const remove = useCallback (
50
- index => event => {
45
+ ( index : number ) => ( event : MouseEvent ) => {
51
46
event . stopPropagation ( )
52
- const items = value . filter ( ( v , i ) => i !== index )
47
+ const items = value . filter ( ( _item : any , i ) => i !== index )
53
48
setActiveItems ( [ ] )
54
49
onChange ( items )
55
50
} ,
56
51
[ value , onChange , setActiveItems ]
57
52
)
58
53
const change = useCallback (
59
- index => itemValue => {
54
+ ( index : number ) => ( itemValue : unknown ) => {
60
55
onChange (
61
56
value . map ( ( v , i ) => {
62
57
if ( i === index ) return itemValue
@@ -67,7 +62,7 @@ const ArrayControl = memo(
67
62
[ value , onChange ]
68
63
)
69
64
const toggle = useCallback (
70
- index => ( ) => {
65
+ ( index : number ) => ( ) => {
71
66
setActiveItems ( items => {
72
67
if ( items . includes ( index ) ) {
73
68
return items . filter ( i => i !== index )
@@ -118,7 +113,6 @@ const ArrayControl = memo(
118
113
controls = { subProps }
119
114
settings = { item }
120
115
onChange = { change ( index ) }
121
- isNested = { true }
122
116
/>
123
117
) }
124
118
</ Fragment >
@@ -128,8 +122,6 @@ const ArrayControl = memo(
128
122
}
129
123
)
130
124
131
- export default ArrayControl
132
-
133
125
const Header = styled ( Cell ) `
134
126
border-bottom: 1px solid ${ ( { theme } ) => theme . colors . borderLight } ;
135
127
@@ -144,7 +136,9 @@ const Title = styled.div`
144
136
color: ${ ( { theme } ) => theme . colors . textLight } ;
145
137
`
146
138
147
- const SubHeader = styled ( Cell ) `
139
+ const SubHeader = styled ( Cell ) < {
140
+ isOpened : boolean
141
+ } > `
148
142
cursor: pointer;
149
143
font-weight: 600;
150
144
user-select: none;
0 commit comments