import React, { useEffect, useMemo, useRef, useState } from 'react'; import { SelectableValue } from '@grafana/data'; import { Input, Select } from '@grafana/ui'; interface Props { onChange: (value: string) => void; options: Array>; value?: string; addLabel?: string; className?: string; placeholder?: string; custom?: boolean; onCustomChange?: (custom: boolean) => void; width?: number; disabled?: boolean; 'aria-label'?: string; getOptionLabel?: ((item: SelectableValue) => React.ReactNode) | undefined; } export const SelectWithAdd = ({ value, onChange, options, className, placeholder, width, custom, onCustomChange, disabled = false, addLabel = '+ Add new', 'aria-label': ariaLabel, getOptionLabel, }: Props) => { const [isCustom, setIsCustom] = useState(custom); useEffect(() => { setIsCustom(custom); }, [custom]); const _options = useMemo( (): Array> => [...options, { value: '__add__', label: addLabel }], [options, addLabel] ); const inputRef = useRef(null); useEffect(() => { if (inputRef.current && isCustom) { inputRef.current.focus(); } }, [isCustom]); if (isCustom) { return ( onChange(e.currentTarget.value)} /> ); } else { return (