123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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<SelectableValue<string>>;
- onChange: (query: ExpressionQuery) => void;
- }
- export const ClassicConditions: FC<Props> = ({ 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 (
- <div>
- <InlineFieldRow>
- <InlineField label="Conditions" labelWidth={14}>
- <div>
- {query.conditions?.map((condition, index) => {
- if (!condition) {
- return;
- }
- return (
- <Condition
- key={index}
- index={index}
- condition={condition}
- onChange={(condition: ClassicCondition) => onConditionChange(condition, index)}
- onRemoveCondition={onRemoveCondition}
- refIds={refIds}
- />
- );
- })}
- </div>
- </InlineField>
- </InlineFieldRow>
- <Button variant="secondary" type="button" onClick={onAddCondition}>
- <Icon name="plus-circle" />
- </Button>
- </div>
- );
- };
|