@grafana/ui
Version:
Grafana Components Library
91 lines (86 loc) • 2.8 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var jsxRuntime = require('react/jsx-runtime');
var lodash = require('lodash');
var React = require('react');
var data = require('@grafana/data');
var i18n = require('@grafana/i18n');
var Combobox = require('../Combobox/Combobox.cjs');
var MultiCombobox = require('../Combobox/MultiCombobox.cjs');
var utils = require('../Combobox/utils.cjs');
var pickComboboxLayout = require('./pickComboboxLayout.cjs');
;
const StatsPicker = React.memo(
({
placeholder = i18n.t("grafana-ui.stats-picker.placeholder", "Choose"),
onChange,
stats,
allowMultiple = false,
defaultStat,
width,
minWidth,
maxWidth,
filterOptions,
...rest
}) => {
const layout = pickComboboxLayout.pickComboboxLayout(width, minWidth, maxWidth);
React.useEffect(() => {
const current = data.fieldReducers.list(stats);
if (current.length !== stats.length) {
const found = current.map((v) => v.id);
const notFound = lodash.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 = data.fieldReducers.selectOptions(stats, filterOptions);
const options = select.options.map((v) => utils.selectableValueToComboboxOption(v)).filter((v) => !!v);
const value = select.current.map((v) => utils.selectableValueToComboboxOption(v)).filter((v) => !!v);
if (allowMultiple) {
return /* @__PURE__ */ jsxRuntime.jsx(
MultiCombobox.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__ */ jsxRuntime.jsx(
Combobox.Combobox,
{
...commonOptions,
isClearable: false,
onChange: (item) => onChange(item && item.value ? [item.value] : [])
}
) : /* @__PURE__ */ jsxRuntime.jsx(
Combobox.Combobox,
{
...commonOptions,
isClearable: true,
onChange: (item) => onChange(item && item.value ? [item.value] : [])
}
);
}
);
StatsPicker.displayName = "StatsPicker";
exports.StatsPicker = StatsPicker;
//# sourceMappingURL=StatsPicker.cjs.map