import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; import { StandardEditorProps } from '@grafana/data'; import { Button, ColorPicker, HorizontalGroup, IconButton, InlineField, InlineSwitch, Input, ThemeContext } from '@grafana/ui'; import React from 'react'; import * as _ from 'lodash'; interface Props { defaultColor: string, arcBackground: string, thresholds: Array } type ThresholdConfig = { color: string; useMetric: boolean; value?: number; metricName?: string; } const DEFAULT_THRESHOLD: ThresholdConfig = { color: '#378372d', useMetric: false, value: 0 } const fieldNamePickerSettings = { settings: { width: 24 }, } as any; export function ThresholdsEditor({ onChange, value, context }: StandardEditorProps) { const config = value; const addThreshold = () => { config.thresholds.push(_.cloneDeep(DEFAULT_THRESHOLD)); onChange(config); } const removeThreshold = (idx: number) => { config.thresholds.splice(idx, 1); onChange(config); } const onFieldChange = (field: keyof Props, value: any) => { config[field] = value; onChange(config); } const onThresholdFieldChange = (thresholdIdx: number, field: keyof ThresholdConfig, value: any) => { // @ts-ignore config.thresholds[thresholdIdx][field] = value; onChange(config); }; return ( { () => { // const styles = getStyles(theme.v1); return (
onFieldChange('defaultColor', val)} enableNamedColors={true} />
} /> onFieldChange('arcBackground', val) } enableNamedColors={true} /> } /> { config.thresholds.map((threshold, thresholdIdx) => { return ( onThresholdFieldChange(thresholdIdx, 'color', newVal)} enableNamedColors={true} /> } /> onThresholdFieldChange(thresholdIdx, 'useMetric', (evt.target as any).checked)} /> { threshold.useMetric ? onThresholdFieldChange(thresholdIdx, 'metricName', newVal)} item={fieldNamePickerSettings} /> : onThresholdFieldChange(thresholdIdx, 'value', (evt.target as any).value)} /> } removeThreshold(thresholdIdx)}> ) }) } ) } }
) }