import React, { HTMLProps, useRef } from 'react'; import { css, cx } from 'emotion'; import useClickAway from 'react-use/lib/useClickAway'; import { SelectableValue } from '@grafana/data'; import { Select } from '../Forms/Legacy/Select/Select'; export interface Props extends Omit, 'value' | 'onChange'> { value?: SelectableValue; options: Array>; onChange: (item: SelectableValue) => void; onClickOutside: () => void; width: number; noOptionsMessage?: string; allowCustomValue?: boolean; } export function SegmentSelect({ value, options = [], onChange, onClickOutside, width, noOptionsMessage = '', allowCustomValue = false, ...rest }: React.PropsWithChildren>) { const ref = useRef(null); useClickAway(ref, () => { if (ref && ref.current) { // https://github.com/JedWatson/react-select/issues/188#issuecomment-279240292 // Unfortunately there's no other way of retrieving the (not yet) created new option const input = ref.current.querySelector('input[id^="react-select-"]') as HTMLInputElement; if (input && input.value) { onChange({ value: input.value as any, label: input.value }); } else { onClickOutside(); } } }); return (