import React, { memo, useCallback } from 'react' import get from 'lodash/get.js' import snakeCase from 'lodash/snakeCase.js' import { ChartProperty, ChartPropertyWithControl, Flavor } from '../../types' import { AngleControlConfig, AnnotationsControlConfig, BlendModeControlConfig, BorderRadiusControlConfig, BoxAnchorControlConfig, BulletColorsControlConfig, CartesianOrientationControlConfig, ChoicesControlConfig, ColorInterpolatorsControlConfig, ColorPickerControlConfig, ContinuousColorsControlConfig, ControlContext, InheritedColorControlConfig, LineWidthControlConfig, MarginControlConfig, MotionConfigControlConfig, NumberArrayControlConfig, ObjectControlConfig, OpacityControlConfig, OrdinalColorsControlConfig, QuantizeColorsControlConfig, RadioControlConfig, RangeControlConfig, ScaleControlConfig, SwitchableRangeControlConfig, SwitchControlConfig, TextControlConfig, ValueFormatControlConfig, } from './types' import { ArrayControl, ObjectControl, SwitchControl, SwitchableRangeControl, TextControl, RadioControl, RangeControl, ChoicesControl, NumberArrayControl, PropertyDocumentation, } from './generics' import { AngleControl, AnnotationsControl, BorderRadiusControl, BoxAnchorControl, CartesianOrientationControl, LineWidthControl, MarginControl, MotionConfigControl, ScaleControl, ValueFormatControl, } from './specialized' import { BlendModeControl, BulletColorsControl, ColorInterpolatorsControl, ContinuousColorsControl, ColorPickerControl, OrdinalColorsControl, OpacityControl, InheritedColorControl, QuantizeColorsControl, } from './colors' // add some extra logic to render properties conditionally // depending on the current settings. export const shouldRenderProperty = (property: ChartProperty, currentSettings: any) => { if (typeof property.when !== 'function') return true return property.when(currentSettings) } interface ControlSwitcherProps { groupName: string property: ChartProperty flavors: Flavor[] currentFlavor: Flavor settings: any onChange: any context?: ControlContext } const ControlSwitcher = memo( ({ groupName, flavors = ['svg'], currentFlavor = 'svg', property, settings, onChange, context, }: ControlSwitcherProps) => { // generate a unique identifier for the property const id = `${snakeCase(groupName)}-${property.name}` const value = get(settings, property.name!) const controlConfig = 'control' in property ? property.control : undefined const handleChange = useCallback( (value: any) => { onChange({ ...settings, [property.name!]: value, }) }, [onChange, settings, property.name] ) if (!shouldRenderProperty(property, settings)) { return null } let shouldRenderControl = controlConfig !== undefined // the property is not available for the current flavor if (Array.isArray(property.flavors) && !property.flavors.includes(currentFlavor)) { shouldRenderControl = false } // the control is only available for certain flavors in the UI // while being available for usage, this is typically used for // `width` & `height` properties, which cannot be set for the demos // as we use the responsive version of the charts, but has to be defined // when using the HTTP API. if ( Array.isArray(property.enableControlForFlavors) && !property.enableControlForFlavors.includes(currentFlavor) ) { shouldRenderControl = false } if (!shouldRenderControl) { return ( ) } // every property which has a control should have a value if (value === undefined) { throw new Error( `no value defined for property: ${property.name} (${JSON.stringify( property, null, ' ' )}, ${JSON.stringify(context, null, ' ')})` ) } switch (controlConfig!.type) { case 'array': return ( ) case 'object': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'choices': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'radio': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'range': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'switch': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'switchableRange': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'text': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'inheritedColor': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'boxAnchor': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'margin': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'motionConfig': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'opacity': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'lineWidth': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'numberArray': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'quantizeColors': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'ordinalColors': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'colorPicker': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'angle': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'blendMode': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'valueFormat': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'annotations': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'continuous_colors': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'color_interpolators': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'bullet_colors': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'cartesianOrientation': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'scale': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) case 'borderRadius': return ( } flavors={flavors} currentFlavor={currentFlavor} value={value} onChange={handleChange} context={context} /> ) default: throw new Error( // @ts-expect-error this can happen at runtime. `invalid control type: ${controlConfig!.type} for property: ${property.name}` ) } } ) interface ControlsGroupProps { name: string flavors?: Flavor[] currentFlavor?: Flavor controls: ChartProperty[] settings: any onChange: any context?: ControlContext } export const ControlsGroup = ({ name, flavors = ['svg'], currentFlavor = 'svg', controls, settings, onChange, context, }: ControlsGroupProps) => ( <> {controls.map((control, index) => ( ))} )