UNPKG

@grafana/ui

Version:
56 lines (53 loc) 2.08 kB
import { jsx } from 'react/jsx-runtime'; import { memo, useCallback } from 'react'; import { fieldMatchers, FieldMatcherID } from '@grafana/data'; import { t } from '@grafana/i18n'; import { Input } from '../Input/Input.mjs'; import { MultiSelect } from '../Select/Select.mjs'; import { useFieldDisplayNames, useSelectOptions, frameHasName } from './utils.mjs'; "use strict"; const FieldNamesMatcherEditor = memo((props) => { var _a; const { data, options, onChange: onChangeFromProps } = props; const { readOnly, prefix } = options; const names = useFieldDisplayNames(data); const selectOptions = useSelectOptions(names, void 0); const onChange = useCallback( (selections) => { if (!Array.isArray(selections)) { return; } return onChangeFromProps({ ...options, names: selections.reduce((all, current) => { if (!frameHasName(current.value, names)) { return all; } all.push(current.value); return all; }, []) }); }, [names, onChangeFromProps, options] ); if (readOnly) { const displayNames = ((_a = options.names) != null ? _a : []).join(", "); return /* @__PURE__ */ jsx(Input, { value: displayNames, readOnly: true, disabled: true, prefix }); } return /* @__PURE__ */ jsx(MultiSelect, { value: options.names, options: selectOptions, onChange }); }); FieldNamesMatcherEditor.displayName = "FieldNameMatcherEditor"; const getFieldNamesMatcherItem = () => ({ id: FieldMatcherID.byNames, component: FieldNamesMatcherEditor, matcher: fieldMatchers.get(FieldMatcherID.byNames), name: t("grafana-ui.matchers-ui.name-fields-with-name", "Fields with name"), description: t("grafana-ui.matchers-ui.description-fields-with-name", "Set properties for a specific field"), optionsToLabel: (options) => { var _a; return ((_a = options.names) != null ? _a : []).join(", "); }, excludeFromPicker: true }); export { FieldNamesMatcherEditor, getFieldNamesMatcherItem }; //# sourceMappingURL=FieldNamesMatcherEditor.mjs.map