import React, { FC } from 'react'; import { SelectableValue } from '@grafana/data'; import { Button, Icon, InlineField, InlineFieldRow } from '@grafana/ui'; import { ClassicCondition, ExpressionQuery } from '../types'; import { defaultCondition } from '../utils/expressionTypes'; import { Condition } from './Condition'; interface Props { query: ExpressionQuery; refIds: Array>; onChange: (query: ExpressionQuery) => void; } export const ClassicConditions: FC = ({ onChange, query, refIds }) => { const onConditionChange = (condition: ClassicCondition, index: number) => { if (query.conditions) { onChange({ ...query, conditions: [...query.conditions.slice(0, index), condition, ...query.conditions.slice(index + 1)], }); } }; const onAddCondition = () => { if (query.conditions) { onChange({ ...query, conditions: query.conditions.length > 0 ? [...query.conditions, defaultCondition] : [defaultCondition], }); } }; const onRemoveCondition = (index: number) => { if (query.conditions) { const condition = query.conditions[index]; const conditions = query.conditions .filter((c) => c !== condition) .map((c, index) => { if (index === 0) { return { ...c, operator: { type: 'when', }, }; } return c; }); onChange({ ...query, conditions, }); } }; return (
{query.conditions?.map((condition, index) => { if (!condition) { return; } return ( onConditionChange(condition, index)} onRemoveCondition={onRemoveCondition} refIds={refIds} /> ); })}
); };