/
ColorInterpolatorsControl.tsx
62 lines (58 loc) · 1.8 KB
/
ColorInterpolatorsControl.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
import React, { useCallback } from 'react'
// @ts-ignore
import { components } from 'react-select'
import { ColorInterpolatorId } from '@nivo/colors'
import { ChartProperty, Flavor } from '../../../types'
import { ControlContext, ColorInterpolatorsControlConfig } from '../types'
import { Control, PropertyHeader, Help, Select } from '../ui'
import {
ColorSchemeSelectOption,
ColorSchemeSelectValue,
useColorInterpolators,
} from './colorSchemeSelect'
interface OrdinalColorsControlProps {
id: string
property: ChartProperty
flavors: Flavor[]
currentFlavor: Flavor
config: ColorInterpolatorsControlConfig
value: ColorInterpolatorId
onChange: (value: ColorInterpolatorId) => void
context?: ControlContext
}
export const ColorInterpolatorsControl = ({
id,
property,
flavors,
currentFlavor,
value: _value,
onChange,
context,
}: OrdinalColorsControlProps) => {
const options = useColorInterpolators()
const handleChange = useCallback(value => onChange(value.value), [onChange])
const value = options.find(({ value: v }) => v === _value)
return (
<Control
id={id}
description={property.description}
flavors={flavors}
currentFlavor={currentFlavor}
supportedFlavors={property.flavors}
>
<PropertyHeader {...property} context={context} />
<Select
options={options}
onChange={handleChange}
value={value}
isSearchable
clearable={false}
components={{
SingleValue: ColorSchemeSelectValue,
Option: ColorSchemeSelectOption,
}}
/>
<Help>{property.help}</Help>
</Control>
)
}