2020-11-25 03:21:48 -06:00
|
|
|
import React, { PropsWithChildren, ReactElement } from 'react';
|
|
|
|
import { InlineFormLabel, Select, useStyles } from '@grafana/ui';
|
|
|
|
import { GrafanaTheme, SelectableValue } from '@grafana/data';
|
2021-04-01 07:15:23 -05:00
|
|
|
import { css } from '@emotion/css';
|
2021-11-01 09:45:23 -05:00
|
|
|
import { useUniqueId } from 'app/plugins/datasource/influxdb/components/useUniqueId';
|
2020-11-25 03:21:48 -06:00
|
|
|
|
|
|
|
interface VariableSelectFieldProps<T> {
|
|
|
|
name: string;
|
|
|
|
value: SelectableValue<T>;
|
|
|
|
options: Array<SelectableValue<T>>;
|
|
|
|
onChange: (option: SelectableValue<T>) => void;
|
|
|
|
tooltip?: string;
|
2022-02-03 18:55:19 -06:00
|
|
|
testId?: string;
|
2020-11-25 03:21:48 -06:00
|
|
|
width?: number;
|
|
|
|
labelWidth?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function VariableSelectField({
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
options,
|
|
|
|
tooltip,
|
|
|
|
onChange,
|
2022-02-03 18:55:19 -06:00
|
|
|
testId,
|
2020-11-25 03:21:48 -06:00
|
|
|
width,
|
|
|
|
labelWidth,
|
|
|
|
}: PropsWithChildren<VariableSelectFieldProps<any>>): ReactElement {
|
|
|
|
const styles = useStyles(getStyles);
|
2021-11-01 09:45:23 -05:00
|
|
|
const uniqueId = useUniqueId();
|
|
|
|
const inputId = `variable-select-input-${name}-${uniqueId}`;
|
2020-11-25 03:21:48 -06:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-10-13 08:21:34 -05:00
|
|
|
<InlineFormLabel width={labelWidth ?? 6} tooltip={tooltip} htmlFor={inputId}>
|
2020-11-25 03:21:48 -06:00
|
|
|
{name}
|
|
|
|
</InlineFormLabel>
|
2022-02-03 18:55:19 -06:00
|
|
|
<div data-testid={testId}>
|
2020-11-25 03:21:48 -06:00
|
|
|
<Select
|
2021-10-13 08:21:34 -05:00
|
|
|
inputId={inputId}
|
2021-08-04 09:47:53 -05:00
|
|
|
menuShouldPortal
|
2020-11-25 03:21:48 -06:00
|
|
|
onChange={onChange}
|
|
|
|
value={value}
|
|
|
|
width={width ?? 25}
|
|
|
|
options={options}
|
|
|
|
className={styles.selectContainer}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStyles(theme: GrafanaTheme) {
|
|
|
|
return {
|
|
|
|
selectContainer: css`
|
|
|
|
margin-right: ${theme.spacing.xs};
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
}
|