import { useCallback, useMemo } from 'react'; import { DataTransformerID, PluginState, TransformerRegistryItem, TransformerUIProps, SelectableValue, TransformerCategory, } from '@grafana/data'; import { InlineField, InlineFieldRow, ValuePicker, Button, HorizontalGroup, FieldValidationMessage, RadioButtonGroup, } from '@grafana/ui'; import { useFieldDisplayNames, useSelectOptions } from '@grafana/ui/src/components/MatchersUI/utils'; import { getTransformationContent } from '../docs/getTransformationContent'; import { partitionByValuesTransformer, PartitionByValuesTransformerOptions } from './partitionByValues'; export function PartitionByValuesEditor({ input, options, onChange, }: TransformerUIProps) { const names = useFieldDisplayNames(input); const allSelectOptions = useSelectOptions(names); const selectOptions = useMemo(() => { const fieldNames = new Set(options.fields); if (fieldNames.size < 1) { return allSelectOptions; } return allSelectOptions.filter((v) => !fieldNames.has(v.value!)); }, [allSelectOptions, options]); const addField = useCallback( (v: SelectableValue) => { if (!v.value) { return; } const fieldNames = new Set(options.fields); fieldNames.add(v.value); onChange({ ...options, fields: [...fieldNames], }); }, [onChange, options] ); enum namingModes { asLabels, frameName, } const namingModesOptions = [ { label: 'As label', value: namingModes.asLabels }, { label: 'As frame name', value: namingModes.frameName }, ]; const KeepFieldsOptions = [ { label: 'Yes', value: true }, { label: 'No', value: false }, ]; const removeField = useCallback( (v: string) => { if (!v) { return; } const fieldNames = new Set(options.fields); fieldNames.delete(v); onChange({ ...options, fields: [...fieldNames], }); }, [onChange, options] ); if (input.length > 1) { return Partition by values only works with a single frame.; } const fieldNames = [...new Set(options.fields)]; return (
{fieldNames.map((name) => ( ))} {selectOptions.length && ( )} onChange({ ...options, naming: { ...options.naming, asLabels: v === namingModes.asLabels } }) } /> onChange({ ...options, keepFields: v })} />
); } export const partitionByValuesTransformRegistryItem: TransformerRegistryItem = { id: DataTransformerID.partitionByValues, editor: PartitionByValuesEditor, transformation: partitionByValuesTransformer, name: partitionByValuesTransformer.name, description: partitionByValuesTransformer.description, state: PluginState.alpha, categories: new Set([TransformerCategory.Reformat]), help: getTransformationContent(DataTransformerID.partitionByValues).helperDocs, };