File size: 12,721 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 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
import React, { memo, useCallback, useMemo, useState, ChangeEvent } from 'react'
import styled from 'styled-components'
import { formatSpecifier as parseFormat, FormatSpecifier } from 'd3-format'
import { components } from 'react-select'
import { FaChevronUp, FaChevronDown } from 'react-icons/fa'
import { ChartPropertyWithControl, Flavor } from '../../../types'
import { ControlContext, ValueFormatControlConfig } from '../types'
import { Control, TextInput, Switch, Select } from '../ui'
interface Option<Value = string> {
value: Value
label: string
description: string
}
type TypeOption = Option<FormatSpecifier['type']>
const typeOptions: TypeOption[] = [
{
value: '',
label: 'none',
description: 'none',
},
{
value: 'e',
label: 'e',
description: 'exponent notation.',
},
{
value: 'f',
label: 'f',
description: 'fixed point notation.',
},
{
value: 'g',
label: 'g',
description: 'either decimal or exponent notation, rounded to significant digits.',
},
{
value: 'r',
label: 'r',
description: 'decimal notation, rounded to significant digits.',
},
{
value: 's',
label: 's',
description: 'decimal notation with an SI prefix, rounded to significant digits.',
},
{
value: '%',
label: '%',
description: 'multiply by 100, and then decimal notation with a percent sign.',
},
{
value: 'p',
label: 'p',
description:
'multiply by 100, round to significant digits, and then decimal notation with a percent sign.',
},
{
value: 'b',
label: 'b',
description: 'binary notation, rounded to integer.',
},
{
value: 'o',
label: 'o',
description: 'octal notation, rounded to integer.',
},
{
value: 'd',
label: 'd',
description: 'decimal notation, rounded to integer.',
},
{
value: 'x',
label: 'x',
description: 'hexadecimal notation, using lower-case letters, rounded to integer.',
},
{
value: 'X',
label: 'X',
description: 'hexadecimal notation, using upper-case letters, rounded to integer.',
},
{
value: 'c',
label: 'c',
description: 'converts the integer to the corresponding unicode character before printing.',
},
]
type AlignOption = Option<FormatSpecifier['align']>
const alignOptions: AlignOption[] = [
{
value: '>',
label: '>',
description: 'Force the field to be right-aligned within the available space.',
},
{
value: '<',
label: '<',
description: 'Force the field to be left-aligned within the available space.',
},
{
value: '^',
label: '^',
description: 'Force the field to be centered within the available space.',
},
{
value: '=',
label: '=',
description: 'like >, but with any sign and symbol to the left of any padding.',
},
]
type SignOption = Option<FormatSpecifier['sign']>
const signOptions: SignOption[] = [
{
value: '-',
label: '-',
description: 'nothing for zero or positive and a minus sign for negative.',
},
{
value: '+',
label: '+',
description: 'a plus sign for zero or positive and a minus sign for negative.',
},
{
value: '(',
label: '(',
description: 'nothing for zero or positive and parentheses for negative.',
},
{
value: ' ',
label: '(space)',
description: 'a space for zero or positive and a minus sign for negative.',
},
]
type SymbolOption = Omit<Option<FormatSpecifier['symbol']>, 'description'>
const symbolOptions: SymbolOption[] = [
{
value: '',
label: 'none',
},
{
value: '#',
label: '#',
},
{
value: '$',
label: '$',
},
]
const Option = (props: any) => (
<components.Option {...props}>
{props.value === undefined && 'none'}
{props.value !== undefined && (
<>
<strong>{props.label}</strong> {props.data.description}
</>
)}
</components.Option>
)
interface ValueFormatControlProps {
id: string
property: ChartPropertyWithControl<ValueFormatControlConfig>
flavors: Flavor[]
currentFlavor: Flavor
value: { format: string; enabled: boolean }
onChange: (value: { format: string; enabled: boolean }) => void
context?: ControlContext
}
export const ValueFormatControl = memo(
({
id,
property,
flavors,
currentFlavor,
value,
onChange,
context,
}: ValueFormatControlProps) => {
const [isEditing, setIsEditing] = useState(false)
const formatSpecifier = useMemo(() => parseFormat(value.format), [value.format])
const handleSwitch = useCallback(
(enabled: boolean) => {
onChange({
format: formatSpecifier.toString(),
enabled,
})
},
[formatSpecifier, onChange]
)
const updateFormat = useCallback(
<P extends keyof FormatSpecifier>(property: P, propertyValue: FormatSpecifier[P]) => {
// Not the most performant way to create a new format specifier, but it's simple enough
const updatedFormatSpecifier = parseFormat(formatSpecifier.toString())
updatedFormatSpecifier[property] = propertyValue
onChange({
format: updatedFormatSpecifier.toString(),
enabled: value.enabled,
})
},
[formatSpecifier, onChange, value.enabled]
)
const handleTypeChange = (option: TypeOption | null) => {
updateFormat('type', option!.value)
}
const handleFillChange = (e: ChangeEvent<HTMLInputElement>) => {
updateFormat('fill', e.target.value.slice(1))
}
const handleAlignChange = (option: AlignOption | null) => {
updateFormat('align', option!.value)
}
const handleSignChange = (option: SignOption | null) => {
updateFormat('sign', option!.value)
}
const handleSymbolChange = (option: SymbolOption | null) => {
updateFormat('symbol', option!.value)
}
const handleZeroChange = (flag: boolean) => {
updateFormat('zero', flag)
}
const handleWidthChange = (e: ChangeEvent<HTMLInputElement>) => {
updateFormat('width', e.target.value ? Number(e.target.value) : undefined)
}
const handleCommaChange = (flag: boolean) => {
updateFormat('comma', flag)
}
const handlePrecisionChange = (e: ChangeEvent<HTMLInputElement>) => {
updateFormat('precision', e.target.value ? Number(e.target.value) : undefined)
}
const handleTrimChange = (flag: boolean) => {
updateFormat('trim', flag)
}
return (
<Control
id={id}
property={property}
flavors={flavors}
currentFlavor={currentFlavor}
context={context}
>
<MainControls>
<Switch value={value.enabled} id={`${id}-enable`} onChange={handleSwitch} />
<label
htmlFor={`${id}-enable.switch`}
style={{ gridColumnStart: 2, gridColumnEnd: 4 }}
>
enable formatting
</label>
<label>format</label>
<TextInput value={value.format} readOnly />
<ToggleButton onClick={() => setIsEditing(flag => !flag)}>
<span>{isEditing ? 'close' : 'open'} editor</span>
{isEditing ? <FaChevronUp /> : <FaChevronDown />}
</ToggleButton>
</MainControls>
{isEditing && (
<SubControls>
<label>type</label>
<Select<TypeOption>
options={typeOptions}
value={typeOptions.find(
option => option.value === formatSpecifier.type
)}
isClearable={false}
onChange={handleTypeChange}
components={{ Option }}
/>
<label>sign</label>
<Select<SignOption>
options={signOptions}
value={signOptions.find(
option => option.value === formatSpecifier.sign
)}
isClearable={false}
onChange={handleSignChange}
components={{ Option }}
/>
<label>symbol</label>
<Select<SymbolOption>
options={symbolOptions}
value={symbolOptions.find(
option => option.value === formatSpecifier.symbol
)}
isClearable={false}
onChange={handleSymbolChange}
/>
<label>precision</label>
<TextInput
value={formatSpecifier.precision}
isNumber
onChange={handlePrecisionChange}
/>
<label>width</label>
<TextInput
value={formatSpecifier.width}
isNumber
onChange={handleWidthChange}
/>
<label htmlFor={`${id}.fill`}>fill</label>
<TextInput
id={`${id}.fill`}
value={formatSpecifier.fill}
onChange={handleFillChange}
/>
<label>align</label>
<Select<AlignOption>
options={alignOptions}
value={alignOptions.find(
option => option.value === formatSpecifier.align
)}
isClearable={false}
onChange={handleAlignChange}
components={{ Option }}
/>
<Switch
value={formatSpecifier.zero}
id={`${id}zero`}
onChange={handleZeroChange}
/>
<label htmlFor={`${id}zero.switch`}>zero-padding</label>
<Switch
value={formatSpecifier.comma}
id={`${id}comma`}
onChange={handleCommaChange}
/>
<label htmlFor={`${id}comma.switch`}>comma</label>
<Switch
value={formatSpecifier.trim}
id={`${id}trim`}
onChange={handleTrimChange}
/>
<label htmlFor={`${id}trim.switch`}>trim trailing zeros</label>
</SubControls>
)}
</Control>
)
}
)
const MainControls = styled.div`
display: grid;
grid-template-columns: 60px auto 100px;
align-items: center;
row-gap: 9px;
column-gap: 9px;
margin: 12px 0;
`
const SubControls = styled.div`
display: grid;
grid-template-columns: 60px auto 60px auto;
align-items: center;
row-gap: 9px;
column-gap: 9px;
margin: 12px 0;
`
const ToggleButton = styled.span`
cursor: pointer;
display: grid;
grid-template-columns: auto 24px;
grid-column-gap: 9px;
align-items: center;
white-space: nowrap;
color: ${props => props.theme.colors.accent};
path {
fill: ${props => props.theme.colors.border};
}
`
|