/
ColorsControl.tsx
98 lines (90 loc) · 2.46 KB
/
ColorsControl.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
import React, { useCallback } from 'react'
import range from 'lodash/range'
import {
colorSchemeIds,
colorSchemes,
colorInterpolatorIds,
colorInterpolators,
} from '@nivo/colors'
// @ts-ignore
import { components } from 'react-select'
import { ChartProperty, Flavor } from '../../../types'
import { ColorsControlConfig, ControlContext } from '../types'
import { Control, PropertyHeader, Help, Select } from '../ui'
import { ColorsControlItem } from './ColorsControlItem'
const colors = colorSchemeIds.map(id => ({
id,
colors: colorSchemes[id],
}))
const sequentialColors = colorInterpolatorIds.map(id => ({
id: `seq:${id}`,
colors: range(0, 1, 0.05).map(t => colorInterpolators[id](t)),
}))
const SingleValue = (props: any) => (
<components.SingleValue {...props}>
<ColorsControlItem id={props.data.label} colors={props.data.colors} />
</components.SingleValue>
)
const Option = (props: any) => (
<components.Option {...props}>
<ColorsControlItem id={props.value} colors={props.data.colors} />
</components.Option>
)
interface ColorsControlProps {
id: string
property: ChartProperty
flavors: Flavor[]
currentFlavor: Flavor
onChange: any
value: string
config: ColorsControlConfig
context?: ControlContext
}
export const ColorsControl = ({
id,
property,
flavors,
currentFlavor,
value,
config,
onChange,
}: ColorsControlProps) => {
const handleChange = useCallback(
value => {
onChange(value.value)
},
[onChange]
)
let options: any[] = colors
if (config.includeSequential === true) {
options = options.concat(sequentialColors)
}
options = options.map(({ id, colors }) => ({
label: id,
value: id,
colors,
}))
const selectedOption = options.find(o => o.value === value)
return (
<Control
id={id}
description={property.description}
flavors={flavors}
currentFlavor={currentFlavor}
supportedFlavors={property.flavors}
>
<PropertyHeader {...property} />
<Select
options={options}
onChange={handleChange}
value={selectedOption}
isSearchable
components={{
SingleValue,
Option,
}}
/>
<Help>{property.help}</Help>
</Control>
)
}