import { css } from '@emotion/css'; import { useCallback, useState } from 'react'; import { GrafanaTheme2, VariableOrigin, DataLinkBuiltInVars } from '@grafana/data'; import { ConfigDescriptionLink, ConfigSubSection } from '@grafana/plugin-ui'; import { Button, useTheme2 } from '@grafana/ui'; import { DerivedFieldConfig } from '../types'; import { DebugSection } from './DebugSection'; import { DerivedField } from './DerivedField'; const getStyles = (theme: GrafanaTheme2) => ({ addButton: css({ marginRight: '10px', }), derivedField: css({ marginBottom: theme.spacing(1), }), container: css({ marginBottom: theme.spacing(4), }), debugSection: css({ marginTop: theme.spacing(4), }), }); type Props = { fields?: DerivedFieldConfig[]; onChange: (value: DerivedFieldConfig[]) => void; }; export const DerivedFields = ({ fields = [], onChange }: Props) => { const theme = useTheme2(); const styles = getStyles(theme); const [showDebug, setShowDebug] = useState(false); const validateName = useCallback( (name: string) => { return fields.filter((field) => field.name && field.name === name).length <= 1; }, [fields] ); return ( } >
{fields.map((field, index) => { return ( { const newDerivedFields = [...fields]; newDerivedFields.splice(index, 1, newField); onChange(newDerivedFields); }} onDelete={() => { const newDerivedFields = [...fields]; newDerivedFields.splice(index, 1); onChange(newDerivedFields); }} validateName={validateName} suggestions={[ { value: DataLinkBuiltInVars.valueRaw, label: 'Raw value', documentation: 'Exact string captured by the regular expression', origin: VariableOrigin.Value, }, ]} /> ); })}
{fields.length > 0 && ( )}
{showDebug && (
)}
); };