mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 04:31:36 +08:00

* type fixes * couple more * just a couple more * small fixes to prometheus typings * improve some more datasource types
34 lines
814 B
TypeScript
34 lines
814 B
TypeScript
import { SelectableValue } from '@grafana/data';
|
|
|
|
export interface SelectionInfo<T> {
|
|
options: Array<SelectableValue<T>>;
|
|
current?: SelectableValue<T>;
|
|
}
|
|
|
|
/**
|
|
* The select component is really annoying -- if the current value is not in the list of options
|
|
* it won't show up. This is a wrapper to make that happen.
|
|
*/
|
|
export function getSelectionInfo<T>(v?: T, options?: Array<SelectableValue<T>>): SelectionInfo<T> {
|
|
if (v && !options) {
|
|
const current = { label: `${v}`, value: v };
|
|
return { options: [current], current };
|
|
}
|
|
if (!options) {
|
|
options = [];
|
|
}
|
|
let current = options.find((item) => item.value === v);
|
|
|
|
if (v && !current) {
|
|
current = {
|
|
label: `${v} (not found)`,
|
|
value: v,
|
|
};
|
|
options.push(current);
|
|
}
|
|
return {
|
|
options,
|
|
current,
|
|
};
|
|
}
|