import { css } from '@emotion/css'; import { ChangeEvent, useEffect, useState } from 'react'; import * as React from 'react'; import { usePrevious } from 'react-use'; import { GrafanaTheme2, DataSourceInstanceSettings, VariableSuggestion } from '@grafana/data'; import { DataSourcePicker } from '@grafana/runtime'; import { Button, DataLinkInput, Field, Icon, Input, Label, Tooltip, useStyles2, Select, Switch } from '@grafana/ui'; import { DerivedFieldConfig } from '../types'; type MatcherType = 'label' | 'regex'; const getStyles = (theme: GrafanaTheme2) => ({ row: css({ display: 'flex', alignItems: 'baseline', }), nameField: css({ flex: 2, marginRight: theme.spacing(0.5), }), regexField: css({ flex: 3, marginRight: theme.spacing(0.5), }), urlField: css({ flex: 1, marginRight: theme.spacing(0.5), }), urlDisplayLabelField: css({ flex: 1, }), internalLink: css({ marginRight: theme.spacing(1), }), openNewTab: css({ marginRight: theme.spacing(1), }), dataSource: css({}), nameMatcherField: css({ width: theme.spacing(20), marginRight: theme.spacing(0.5), }), }); type Props = { value: DerivedFieldConfig; onChange: (value: DerivedFieldConfig) => void; onDelete: () => void; suggestions: VariableSuggestion[]; className?: string; validateName: (name: string) => boolean; }; export const DerivedField = (props: Props) => { const { value, onChange, onDelete, suggestions, className, validateName } = props; const styles = useStyles2(getStyles); const [showInternalLink, setShowInternalLink] = useState(!!value.datasourceUid); const [openInNewTab, setOpenInNewTab] = useState(!!value.targetBlank); const previousUid = usePrevious(value.datasourceUid); const [fieldType, setFieldType] = useState(value.matcherType ?? 'regex'); // Force internal link visibility change if uid changed outside of this component. useEffect(() => { if (!previousUid && value.datasourceUid && !showInternalLink) { setShowInternalLink(true); } if (previousUid && !value.datasourceUid && showInternalLink) { setShowInternalLink(false); } }, [previousUid, value.datasourceUid, showInternalLink]); const handleChange = (field: keyof typeof value) => (event: React.ChangeEvent) => { onChange({ ...value, [field]: event.currentTarget.value, }); }; const invalidName = !validateName(value.name); return (
} >
onChange({ ...value, url: newValue, }) } suggestions={suggestions} /> } >
) => { const { checked } = e.currentTarget; if (!checked) { onChange({ ...value, datasourceUid: undefined, }); } setShowInternalLink(checked); }} /> {showInternalLink && ( onChange({ ...value, datasourceUid: ds.uid, }) } current={value.datasourceUid} noDefault /> )}
) => { const { checked } = e.currentTarget; onChange({ ...value, targetBlank: checked, }); setOpenInNewTab(checked); }} />
); }; const TooltipLabel = ({ content, label }: { content: string; label: string }) => ( );