import { css } from '@emotion/css'; import React, { FC, useCallback, useMemo } from 'react'; import { GrafanaTheme2, SelectableValue, StandardEditorProps } from '@grafana/data'; import { InlineField, InlineFieldRow, Select, useStyles2 } from '@grafana/ui'; import { useFieldDisplayNames, useSelectOptions, } from '../../../../../packages/grafana-ui/src/components/MatchersUI/utils'; import { validateScaleOptions, validateScaleConfig } from '../scale'; import { ScaleDimensionConfig, ScaleDimensionOptions } from '../types'; import { NumberInput } from './NumberInput'; const fixedValueOption: SelectableValue = { label: 'Fixed value', value: '_____fixed_____', }; export const ScaleDimensionEditor: FC> = ( props ) => { const { value, context, onChange, item } = props; const { settings } = item; const styles = useStyles2(getStyles); const fieldName = value?.field; const isFixed = Boolean(!fieldName); const names = useFieldDisplayNames(context.data); const selectOptions = useSelectOptions(names, fieldName, fixedValueOption); const minMaxStep = useMemo(() => { return validateScaleOptions(settings); }, [settings]); // Validate and update const validateAndDoChange = useCallback( (v: ScaleDimensionConfig) => { // always called with a copy so no need to spread onChange(validateScaleConfig(v, minMaxStep)); }, [onChange, minMaxStep] ); const onSelectChange = useCallback( (selection: SelectableValue) => { const field = selection.value; if (field && field !== fixedValueOption.value) { validateAndDoChange({ ...value, field, }); } else { validateAndDoChange({ ...value, field: undefined, }); } }, [validateAndDoChange, value] ); const onMinChange = useCallback( (min?: number) => { if (min !== undefined) { validateAndDoChange({ ...value, min, }); } }, [validateAndDoChange, value] ); const onMaxChange = useCallback( (max?: number) => { if (max !== undefined) { validateAndDoChange({ ...value, max, }); } }, [validateAndDoChange, value] ); const onValueChange = useCallback( (fixed?: number) => { if (fixed !== undefined) { validateAndDoChange({ ...value, fixed, }); } }, [validateAndDoChange, value] ); const val = value ?? {}; const selectedOption = isFixed ? fixedValueOption : selectOptions.find((v) => v.value === fieldName); return ( <>