/
ArrayControl.tsx
197 lines (180 loc) · 5.9 KB
/
ArrayControl.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, { memo, Fragment, useMemo, useState, useCallback, MouseEvent } from 'react'
import styled from 'styled-components'
import { PropertyHeader } from './PropertyHeader'
import ControlsGroup from './ControlsGroup'
import { Cell, Toggle } from './styled'
import { Help } from './Help'
import { Flavor, ChartProperty } from '../../types'
import { ArrayControlConfig } from './types'
interface ArrayControlProps {
id: string
property: ChartProperty
value: unknown[]
flavors: Flavor[]
currentFlavor: Flavor
config: ArrayControlConfig
onChange: (value: unknown) => void
context?: any
}
export const ArrayControl = memo(
({
property,
flavors,
currentFlavor,
value,
onChange,
config: {
props,
shouldCreate = false,
addLabel = 'add',
shouldRemove = false,
removeLabel = 'remove',
defaults = {},
getItemTitle,
},
}: ArrayControlProps) => {
const [activeItems, setActiveItems] = useState([0])
const append = useCallback(() => {
onChange([...value, { ...defaults }])
setActiveItems([value.length])
}, [value, onChange, defaults, setActiveItems])
const remove = useCallback(
(index: number) => (event: MouseEvent) => {
event.stopPropagation()
const items = value.filter((_item: any, i) => i !== index)
setActiveItems([])
onChange(items)
},
[value, onChange, setActiveItems]
)
const change = useCallback(
(index: number) => (itemValue: unknown) => {
onChange(
value.map((v, i) => {
if (i === index) return itemValue
return v
})
)
},
[value, onChange]
)
const toggle = useCallback(
(index: number) => () => {
setActiveItems(items => {
if (items.includes(index)) {
return items.filter(i => i !== index)
}
return [...activeItems, index]
})
},
[setActiveItems]
)
const subProps = useMemo(
() =>
props.map(prop => ({
...prop,
name: prop.key,
group: property.group,
})),
[props]
)
return (
<>
<Header>
<PropertyHeader {...property} />
<Help>{property.help}</Help>
{shouldCreate && <AddButton onClick={append}>{addLabel}</AddButton>}
</Header>
{value.map((item, index) => (
<Fragment key={index}>
<SubHeader isOpened={activeItems.includes(index)} onClick={toggle(index)}>
<Title>
{getItemTitle !== undefined
? getItemTitle(index, item)
: `${property.key}[${index}]`}
{shouldRemove && (
<RemoveButton onClick={remove(index)}>
{removeLabel}
</RemoveButton>
)}
</Title>
<Toggle isOpened={activeItems.includes(index)} />
</SubHeader>
{activeItems.includes(index) && (
<ControlsGroup
name={property.key}
flavors={flavors}
currentFlavor={currentFlavor}
controls={subProps}
settings={item}
onChange={change(index)}
/>
)}
</Fragment>
))}
</>
)
}
)
const Header = styled(Cell)`
border-bottom: 1px solid ${({ theme }) => theme.colors.borderLight};
&:last-child {
border-bottom-width: 0;
}
`
const Title = styled.div`
white-space: nowrap;
font-weight: 600;
color: ${({ theme }) => theme.colors.textLight};
`
const SubHeader = styled(Cell)<{
isOpened: boolean
}>`
cursor: pointer;
font-weight: 600;
user-select: none;
border-bottom: 1px solid ${({ theme }) => theme.colors.borderLight};
&:last-child {
border-bottom-width: 0;
}
&:hover {
background: ${({ theme }) => theme.colors.cardAltBackground};
${Title} {
color: ${({ theme }) => theme.colors.text};
}
}
${Title} {
${({ isOpened, theme }) => (isOpened ? `color: ${theme.colors.text};` : '')}
}
`
const AddButton = styled.div`
position: absolute;
top: 9px;
right: 20px;
font-weight: 600;
cursor: pointer;
font-size: 12px;
color: ${({ theme }) => theme.colors.accent};
border: 1px solid ${({ theme }) => theme.colors.accent};
padding: 3px 9px;
border-radius: 2px;
user-select: none;
&:hover {
color: ${({ theme }) => theme.colors.cardBackground};
background: ${({ theme }) => theme.colors.accent};
}
`
const RemoveButton = styled.span`
display: inline-block;
font-size: 12px;
margin-left: 12px;
background: ${({ theme }) => theme.colors.cardBackground};
color: ${({ theme }) => theme.colors.accent};
border: 1px solid ${({ theme }) => theme.colors.accent};
padding: 1px 9px;
border-radius: 1px;
&:hover {
color: ${({ theme }) => theme.colors.cardBackground};
background: ${({ theme }) => theme.colors.accent};
}
`