File size: 2,836 Bytes
1e92f2d |
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 |
import React, { memo, useMemo, useState, useCallback } from 'react'
import styled from 'styled-components'
import { ChartPropertyWithControl, Flavor } from '../../../types'
import { ObjectControlConfig, ControlContext } from '../types'
import { PropertyHeader, Help, Cell, Toggle } from '../ui'
import { ControlsGroup } from '../ControlsGroup'
interface ObjectControlProps {
id: string
property: ChartPropertyWithControl<ObjectControlConfig>
flavors: Flavor[]
currentFlavor: Flavor
value: Record<string, unknown>
onChange: (value: Record<string, unknown>) => void
isOpenedByDefault?: boolean
context?: ControlContext
}
export const ObjectControl = memo(
({ property, flavors, currentFlavor, value, onChange, context }: ObjectControlProps) => {
const { control } = property
const [isOpened, setIsOpened] = useState(
control.isOpenedByDefault !== undefined ? control.isOpenedByDefault : false
)
const toggle = useCallback(() => setIsOpened(flag => !flag), [setIsOpened])
const subProps = useMemo(
() =>
control.props.map(prop => ({
...prop,
name: prop.key,
group: property.group,
})),
[control.props]
)
const newContext = {
path: [...(context ? context.path : []), property.key || property.name] as string[],
}
return (
<>
<Header $isOpened={isOpened} onClick={toggle}>
<PropertyHeader {...property} context={context} />
<Help>{property.help}</Help>
<Toggle isOpened={isOpened} />
</Header>
{isOpened && (
<ControlsGroup
name={property.key}
flavors={flavors}
currentFlavor={currentFlavor}
controls={subProps}
settings={value}
onChange={onChange}
context={newContext}
/>
)}
</>
)
}
)
const Title = styled.div`
white-space: nowrap;
font-weight: 600;
color: ${({ theme }) => theme.colors.accentLight};
`
const Header = styled(Cell)<{
$isOpened: boolean
}>`
cursor: pointer;
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.accent};
}
}
${Title} {
${({ $isOpened, theme }) => ($isOpened ? `color: ${theme.colors.accent};` : '')}
}
`
|