import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker'; import { isNumber } from '../../utils'; import { GrafanaTheme, StandardEditorProps } from '@grafana/data'; import { Button, ColorPicker, HorizontalGroup, IconButton, InlineField, InlineLabel, InlineSwitch, Input, stylesFactory, ThemeContext, } from '@grafana/ui'; import React from 'react'; import { css } from 'emotion'; import * as _ from 'lodash'; interface Props { defaultColor: string; arcBackground: string; thresholds: ThresholdConfig[]; } 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 ( {(theme) => { const styles = getStyles(theme.v1); return (
onFieldChange('defaultColor', val)} enableNamedColors={false} /> onFieldChange('arcBackground', val)} enableNamedColors={false} /> {config.thresholds.map((threshold, thresholdIdx) => { return ( onThresholdFieldChange(thresholdIdx, 'useMetric', evt.currentTarget.checked)} /> {threshold.useMetric ? ( onThresholdFieldChange(thresholdIdx, 'metricName', newVal)} item={fieldNamePickerSettings} /> ) : ( onThresholdFieldChange(thresholdIdx, 'value', isNumber(evt.currentTarget.value) ? +evt.currentTarget.value : undefined)} /> )} onThresholdFieldChange(thresholdIdx, 'color', newVal)} enableNamedColors={false} /> removeThreshold(thresholdIdx)} tooltip="Delete Threshold" > ); })}
); }}
); } interface ThresholdsEditorStyles { deleteButton: string; } const getStyles = stylesFactory((theme: GrafanaTheme): ThresholdsEditorStyles => { return { deleteButton: css` margin-right: 0px !important; `, }; });