import React, { FunctionComponent } from 'react'; import { SelectableValue } from '@grafana/data'; import { Segment, Icon } from '@grafana/ui'; import { labelsToGroupedOptions } from '../functions'; import { systemLabels } from '../constants'; export interface Props { values: string[]; onChange: (values: string[]) => void; variableOptionGroup: SelectableValue; groupBys: string[]; } const removeText = '-- remove group by --'; const removeOption: SelectableValue = { label: removeText, value: removeText }; export const GroupBys: FunctionComponent = ({ groupBys = [], values = [], onChange, variableOptionGroup }) => { const options = [removeOption, variableOptionGroup, ...labelsToGroupedOptions([...groupBys, ...systemLabels])]; return (
{values && values.map((value, index) => ( onChange( value === removeText ? values.filter((_, i) => i !== index) : values.map((v, i) => (i === index ? value : v)) ) } /> ))} {values.length !== groupBys.length && ( } allowCustomValue onChange={({ value = '' }) => onChange([...values, value])} options={[ variableOptionGroup, ...labelsToGroupedOptions([...groupBys.filter((groupBy) => !values.includes(groupBy)), ...systemLabels]), ]} /> )}
); };