import { css } from '@emotion/css'; import React, { useCallback, useState } from 'react'; // @ts-ignore import Highlighter from 'react-highlight-words'; import { SelectableValue, toOption, GrafanaTheme2 } from '@grafana/data'; import { EditorField, EditorFieldGroup } from '@grafana/experimental'; import { Select, FormatOptionLabelMeta, useStyles2 } from '@grafana/ui'; import { PromVisualQuery } from '../types'; // We are matching words split with space const splitSeparator = ' '; export interface Props { query: PromVisualQuery; onChange: (query: PromVisualQuery) => void; onGetMetrics: () => Promise; } export function MetricSelect({ query, onChange, onGetMetrics }: Props) { const styles = useStyles2(getStyles); const [state, setState] = useState<{ metrics?: Array>; isLoading?: boolean; }>({}); const customFilterOption = useCallback((option: SelectableValue, searchQuery: string) => { const label = option.label ?? option.value; if (!label) { return false; } // custom value is not a string label but a react node if (!label.toLowerCase) { return true; } const searchWords = searchQuery.split(splitSeparator); return searchWords.reduce((acc, cur) => acc && label.toLowerCase().includes(cur.toLowerCase()), true); }, []); const formatOptionLabel = useCallback( (option: SelectableValue, meta: FormatOptionLabelMeta) => { // For newly created custom value we don't want to add highlight if (option['__isNew__']) { return option.label; } return ( ); }, [styles.highlight] ); return (