mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { SelectableValue } from '@grafana/data';
|
|
import { SelectableOptGroup } from './types';
|
|
|
|
/**
|
|
* Normalize the value format to SelectableValue[] | []. Only used for single select
|
|
*/
|
|
export const cleanValue = (
|
|
value: any,
|
|
options: Array<SelectableValue | SelectableOptGroup | SelectableOptGroup[]>
|
|
): SelectableValue[] | [] => {
|
|
if (Array.isArray(value)) {
|
|
return value.filter(Boolean);
|
|
}
|
|
if (typeof value === 'object' && value !== null) {
|
|
return [value];
|
|
}
|
|
if (typeof value === 'string' || typeof value === 'number') {
|
|
const selectedValue = findSelectedValue(value, options);
|
|
if (selectedValue) {
|
|
return [selectedValue];
|
|
}
|
|
}
|
|
return [];
|
|
};
|
|
|
|
/**
|
|
* Find the label for a string|number value inside array of options or optgroups
|
|
*/
|
|
export const findSelectedValue = (
|
|
value: string | number,
|
|
options: Array<SelectableValue | SelectableOptGroup | SelectableOptGroup[]>
|
|
): SelectableValue | null => {
|
|
for (const option of options) {
|
|
if ('options' in option) {
|
|
let found = findSelectedValue(value, option.options);
|
|
if (found) {
|
|
return found;
|
|
}
|
|
} else if ('value' in option && option.value === value) {
|
|
return option;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|