2023-03-14 07:38:21 -07:00
|
|
|
import React, { useCallback, useMemo } from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2020-06-04 13:44:48 +02:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { Select } from '@grafana/ui';
|
2022-09-19 10:49:35 +01:00
|
|
|
import { useSelector } from 'app/types';
|
2020-06-04 13:44:48 +02:00
|
|
|
|
2022-04-22 14:33:13 +01:00
|
|
|
import { getLastKey, getVariablesByKey } from '../../../variables/state/selectors';
|
2020-06-04 13:44:48 +02:00
|
|
|
|
|
|
|
|
export interface Props {
|
2021-10-12 13:26:01 +01:00
|
|
|
id?: string;
|
2021-07-20 09:57:03 +01:00
|
|
|
repeat?: string | null;
|
|
|
|
|
onChange: (name: string | null) => void;
|
2020-06-04 13:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-14 07:38:21 -07:00
|
|
|
export const RepeatRowSelect = ({ repeat, onChange, id }: Props) => {
|
2022-09-19 10:49:35 +01:00
|
|
|
const variables = useSelector((state) => {
|
2022-02-18 06:06:04 +01:00
|
|
|
return getVariablesByKey(getLastKey(state), state);
|
|
|
|
|
});
|
2020-06-04 13:44:48 +02:00
|
|
|
|
|
|
|
|
const variableOptions = useMemo(() => {
|
2023-03-14 09:51:44 +00:00
|
|
|
const options: Array<SelectableValue<string | null>> = variables.map((item) => {
|
2020-06-04 13:44:48 +02:00
|
|
|
return { label: item.name, value: item.name };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
|
|
|
|
options.unshift({
|
|
|
|
|
label: 'No template variables found',
|
|
|
|
|
value: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.unshift({
|
|
|
|
|
label: 'Disable repeating',
|
|
|
|
|
value: null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return options;
|
2021-03-25 12:42:14 +01:00
|
|
|
}, [variables]);
|
2020-06-04 13:44:48 +02:00
|
|
|
|
2021-07-20 09:57:03 +01:00
|
|
|
const onSelectChange = useCallback((option: SelectableValue<string | null>) => onChange(option.value!), [onChange]);
|
2020-06-04 13:44:48 +02:00
|
|
|
|
2022-05-04 15:12:59 +01:00
|
|
|
return <Select inputId={id} value={repeat} onChange={onSelectChange} options={variableOptions} />;
|
2020-06-04 13:44:48 +02:00
|
|
|
};
|