UNPKG

@grafana/ui

Version:
87 lines (84 loc) 2.63 kB
import { jsx } from 'react/jsx-runtime'; import { difference } from 'lodash'; import { memo, useEffect } from 'react'; import { fieldReducers } from '@grafana/data'; import { t } from '@grafana/i18n'; import { Combobox } from '../Combobox/Combobox.mjs'; import { MultiCombobox } from '../Combobox/MultiCombobox.mjs'; import { selectableValueToComboboxOption } from '../Combobox/utils.mjs'; import { pickComboboxLayout } from './pickComboboxLayout.mjs'; "use strict"; const StatsPicker = memo( ({ placeholder = t("grafana-ui.stats-picker.placeholder", "Choose"), onChange, stats, allowMultiple = false, defaultStat, width, minWidth, maxWidth, filterOptions, ...rest }) => { const layout = pickComboboxLayout(width, minWidth, maxWidth); useEffect(() => { const current = fieldReducers.list(stats); if (current.length !== stats.length) { const found = current.map((v) => v.id); const notFound = difference(stats, found); console.warn("Unknown stats", notFound, stats); onChange(current.map((stat) => stat.id)); } if (!allowMultiple && stats.length > 1) { console.warn("Removing extra stat", stats); onChange([stats[0]]); } if (defaultStat && stats.length < 1) { onChange([defaultStat]); } }, [stats, allowMultiple, defaultStat, onChange]); const select = fieldReducers.selectOptions(stats, filterOptions); const options = select.options.map((v) => selectableValueToComboboxOption(v)).filter((v) => !!v); const value = select.current.map((v) => selectableValueToComboboxOption(v)).filter((v) => !!v); if (allowMultiple) { return /* @__PURE__ */ jsx( MultiCombobox, { ...rest, ...layout, value, options, placeholder, isClearable: !defaultStat, onChange: (items) => onChange(items.map((v) => v.value)) } ); } const commonOptions = { ...rest, ...layout, value: value[0], options, placeholder }; return defaultStat ? /* @__PURE__ */ jsx( Combobox, { ...commonOptions, isClearable: false, onChange: (item) => onChange(item && item.value ? [item.value] : []) } ) : /* @__PURE__ */ jsx( Combobox, { ...commonOptions, isClearable: true, onChange: (item) => onChange(item && item.value ? [item.value] : []) } ); } ); StatsPicker.displayName = "StatsPicker"; export { StatsPicker }; //# sourceMappingURL=StatsPicker.mjs.map