File size: 3,387 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import React, { useCallback, useState, ChangeEvent } from 'react'
import styled from 'styled-components'
import { ControlContext, SwitchableRangeControlConfig } from '../types'
import { ChartPropertyWithControl, Flavor } from '../../../types'
import { Control, TextInput, Switch } from '../ui'
interface SwitchableRangeControlProps {
id: string
property: ChartPropertyWithControl<SwitchableRangeControlConfig>
flavors: Flavor[]
currentFlavor: Flavor
value: number | string
onChange: (value: number | string) => void
context?: ControlContext
}
export const SwitchableRangeControl = ({
id,
property,
flavors,
currentFlavor,
value,
onChange,
context,
}: SwitchableRangeControlProps) => {
const { disabledValue, defaultValue, min, max, step } = property.control
const [isSliderEnabled, setIsSliderEnabled] = useState(value !== disabledValue)
const [sliderValue, setSliderValue] = useState(value === disabledValue ? defaultValue : value)
const handleSliderUpdate = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setSliderValue(Number(e.target.value))
onChange(Number(e.target.value))
},
[setSliderValue, onChange]
)
const handleSwitchUpdate = useCallback(
(checked: boolean) => {
if (!checked) {
setIsSliderEnabled(true)
onChange(Number(sliderValue))
} else {
setIsSliderEnabled(false)
onChange(disabledValue)
}
},
[onChange, disabledValue, sliderValue, setIsSliderEnabled]
)
return (
<Control
id={id}
property={property}
flavors={flavors}
currentFlavor={currentFlavor}
context={context}
>
<SwitchRow>
<Switch
id={`${id}.switch`}
value={!isSliderEnabled}
onChange={handleSwitchUpdate}
/>
<DisabledValue $isCurrent={!isSliderEnabled}>{disabledValue}</DisabledValue>
</SwitchRow>
<RangeRow $isEnabled={isSliderEnabled}>
<TextInput
value={sliderValue}
unit={property.control.unit}
isNumber={true}
disabled={true}
/>
<input
id={`${id}.slider`}
type="range"
value={sliderValue}
onChange={handleSliderUpdate}
min={min}
max={max}
step={step}
disabled={!isSliderEnabled}
/>
</RangeRow>
</Control>
)
}
const SwitchRow = styled.div`
display: flex;
align-items: center;
margin-bottom: 5px;
& > *:first-child {
margin-right: 9px;
}
`
const DisabledValue = styled.span<{ $isCurrent?: boolean }>`
opacity: ${({ $isCurrent }) => ($isCurrent ? 1 : 0.35)};
font-weight: 500;
`
const RangeRow = styled.div<{ $isEnabled?: boolean }>`
display: grid;
grid-template-columns: 60px auto;
grid-column-gap: 9px;
align-items: center;
max-width: 240px;
margin-bottom: 5px;
opacity: ${({ $isEnabled }) => ($isEnabled ? 1 : 0.75)};
`
|