2022-11-09 01:02:24 -06:00
|
|
|
import { isArray } from 'lodash';
|
|
|
|
import React from 'react';
|
|
|
|
|
2022-12-13 01:20:09 -06:00
|
|
|
import { MultiSelect, Select } from '@grafana/ui';
|
2022-11-09 01:02:24 -06:00
|
|
|
|
|
|
|
import { SceneComponentProps } from '../../core/types';
|
|
|
|
import { MultiValueVariable } from '../variants/MultiValueVariable';
|
|
|
|
|
|
|
|
export function VariableValueSelect({ model }: SceneComponentProps<MultiValueVariable>) {
|
2022-12-13 01:20:09 -06:00
|
|
|
const { value, key, loading } = model.useState();
|
2022-11-09 01:02:24 -06:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
id={key}
|
|
|
|
placeholder="Select value"
|
|
|
|
width="auto"
|
|
|
|
value={value}
|
|
|
|
allowCustomValue
|
2022-12-13 01:20:09 -06:00
|
|
|
tabSelectsValue={false}
|
2022-11-09 01:02:24 -06:00
|
|
|
isLoading={loading}
|
2022-12-13 01:20:09 -06:00
|
|
|
options={model.getOptionsForSelect()}
|
2022-11-16 04:36:30 -06:00
|
|
|
onChange={(newValue) => {
|
|
|
|
model.changeValueTo(newValue.value!, newValue.label!);
|
|
|
|
}}
|
2022-11-09 01:02:24 -06:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2022-12-13 01:20:09 -06:00
|
|
|
|
|
|
|
export function VariableValueSelectMulti({ model }: SceneComponentProps<MultiValueVariable>) {
|
|
|
|
const { value, key, loading } = model.useState();
|
|
|
|
const arrayValue = isArray(value) ? value : [value];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MultiSelect
|
|
|
|
id={key}
|
|
|
|
placeholder="Select value"
|
|
|
|
width="auto"
|
|
|
|
value={arrayValue}
|
|
|
|
tabSelectsValue={false}
|
|
|
|
allowCustomValue
|
|
|
|
isLoading={loading}
|
|
|
|
options={model.getOptionsForSelect()}
|
|
|
|
closeMenuOnSelect={false}
|
|
|
|
isClearable={true}
|
|
|
|
onOpenMenu={() => {}}
|
|
|
|
onChange={(newValue) => {
|
|
|
|
model.changeValueTo(
|
|
|
|
newValue.map((v) => v.value!),
|
|
|
|
newValue.map((v) => v.label!)
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderSelectForVariable(model: MultiValueVariable) {
|
|
|
|
if (model.state.isMulti) {
|
|
|
|
return <VariableValueSelectMulti model={model} />;
|
|
|
|
} else {
|
|
|
|
return <VariableValueSelect model={model} />;
|
|
|
|
}
|
|
|
|
}
|