selection.ts 820 B

123456789101112131415161718192021222324252627282930313233
  1. import { SelectableValue } from '@grafana/data';
  2. export interface SelectionInfo<T = any> {
  3. options: Array<SelectableValue<T>>;
  4. current?: SelectableValue<T>;
  5. }
  6. /**
  7. * The select component is really annoying -- if the current value is not in the list of options
  8. * it won't show up. This is a wrapper to make that happen.
  9. */
  10. export function getSelectionInfo<T>(v?: T, options?: Array<SelectableValue<T>>): SelectionInfo<T> {
  11. if (v && !options) {
  12. const current = { label: `${v}`, value: v };
  13. return { options: [current], current };
  14. }
  15. if (!options) {
  16. options = [];
  17. }
  18. let current = options.find((item) => item.value === v);
  19. if (v && !current) {
  20. current = {
  21. label: `${v} (not found)`,
  22. value: v,
  23. };
  24. options.push(current);
  25. }
  26. return {
  27. options,
  28. current,
  29. };
  30. }