@ -3,8 +3,9 @@ import { FormEvent, useCallback } from 'react';
import { DataSourceInstanceSettings , MetricFindValue , readCSV } from '@grafana/data' ;
import { DataSourceInstanceSettings , MetricFindValue , readCSV } from '@grafana/data' ;
import { selectors } from '@grafana/e2e-selectors' ;
import { selectors } from '@grafana/e2e-selectors' ;
import { Trans , useTranslate } from '@grafana/i18n' ;
import { Trans , useTranslate } from '@grafana/i18n' ;
import { EditorField } from '@grafana/plugin-ui' ;
import { DataSourceRef } from '@grafana/schema' ;
import { DataSourceRef } from '@grafana/schema' ;
import { Alert , CodeEditor , Field , Switch } from '@grafana/ui' ;
import { Alert , Box , CodeEditor , Field , Switch } from '@grafana/ui' ;
import { DataSourcePicker } from 'app/features/datasources/components/picker/DataSourcePicker' ;
import { DataSourcePicker } from 'app/features/datasources/components/picker/DataSourcePicker' ;
import { VariableCheckboxField } from './VariableCheckboxField' ;
import { VariableCheckboxField } from './VariableCheckboxField' ;
@ -18,6 +19,8 @@ export interface GroupByVariableFormProps {
defaultOptions? : MetricFindValue [ ] ;
defaultOptions? : MetricFindValue [ ] ;
allowCustomValue : boolean ;
allowCustomValue : boolean ;
onAllowCustomValueChange : ( event : FormEvent < HTMLInputElement > ) = > void ;
onAllowCustomValueChange : ( event : FormEvent < HTMLInputElement > ) = > void ;
inline? : boolean ;
datasourceSupported : boolean ;
}
}
export function GroupByVariableForm ( {
export function GroupByVariableForm ( {
@ -28,6 +31,8 @@ export function GroupByVariableForm({
onDefaultOptionsChange ,
onDefaultOptionsChange ,
allowCustomValue ,
allowCustomValue ,
onAllowCustomValueChange ,
onAllowCustomValueChange ,
inline ,
datasourceSupported ,
} : GroupByVariableFormProps ) {
} : GroupByVariableFormProps ) {
const updateDefaultOptions = useCallback (
const updateDefaultOptions = useCallback (
( csvContent : string ) = > {
( csvContent : string ) = > {
@ -45,70 +50,85 @@ export function GroupByVariableForm({
return (
return (
< >
< >
< VariableLegend >
{ ! inline && (
< Trans i18nKey = "dashboard-scene.group-by-variable-form.group-by-options" > Group by options < / Trans >
< VariableLegend >
< / VariableLegend >
< Trans i18nKey = "dashboard-scene.group-by-variable-form.group-by-options" > Group by options < / Trans >
< Field
< / VariableLegend >
label = { t ( 'dashboard-scene.group-by-variable-form.label-data-source' , 'Data source' ) }
) }
htmlFor = "data-source-picker"
>
< Box marginBottom = { 2 } >
< DataSourcePicker current = { datasource } onChange = { onDataSourceChange } width = { 30 } variables = { true } noDefault / >
< EditorField
< / Field >
label = { t ( 'dashboard-scene.group-by-variable-form.label-data-source' , 'Data source' ) }
htmlFor = "data-source-picker"
tooltip = { infoText }
>
< DataSourcePicker current = { datasource } onChange = { onDataSourceChange } width = { 30 } variables = { true } noDefault / >
< / EditorField >
< / Box >
{ infoText ? (
{ ! datasourceSupported ? (
< Alert
< Alert
title = { infoText }
title = { t (
severity = "info"
'dashboard-scene.group-by-variable-form.alert-not-supported' ,
'This data source does not support group by variables'
) }
severity = "warning"
data - testid = { selectors . pages . Dashboard . Settings . Variables . Edit . GroupByVariable . infoText }
data - testid = { selectors . pages . Dashboard . Settings . Variables . Edit . GroupByVariable . infoText }
/ >
/ >
) : null }
) : null }
< Field
{ datasourceSupported && (
label = { t (
< >
'dashboard-scene.group-by-variable-form.label-use-static-group-by-dimensions' ,
< Field
'Use static group dimensions'
label = { t (
) }
'dashboard-scene.group-by-variable-form.label-use-static-group-by-dimensions' ,
description = { t (
'Use static group dimensions'
'dashboard-scene.group-by-variable-form.description-provide-dimensions-as-csv-dimension-name-dimension-id' ,
) }
'Provide dimensions as CSV: {{name}}, {{value}}' ,
description = { t (
{ name : 'dimensionName' , value : 'dimensionId' }
'dashboard-scene.group-by-variable-form.description-provide-dimensions-as-csv-dimension-name-dimension-id' ,
) }
'Provide dimensions as CSV: {{name}}, {{value}}' ,
>
{ name : 'dimensionName' , value : 'dimensionId' }
< Switch
) }
data - testid = { selectors . pages . Dashboard . Settings . Variables . Edit . GroupByVariable . modeToggle }
>
value = { defaultOptions !== undefined }
< Switch
onChange = { ( e ) = > {
data - testid = { selectors . pages . Dashboard . Settings . Variables . Edit . GroupByVariable . modeToggle }
if ( defaultOptions === undefined ) {
value = { defaultOptions !== undefined }
onDefaultOptionsChange ( [ ] ) ;
onChange = { ( e ) = > {
} else {
if ( defaultOptions === undefined ) {
onDefaultOptionsChange ( undefined ) ;
onDefaultOptionsChange ( [ ] ) ;
}
} else {
} }
onDefaultOptionsChange ( undefined ) ;
/ >
}
< / Field >
} }
/ >
< / Field >
{ defaultOptions !== undefined && (
{ defaultOptions !== undefined && (
< CodeEditor
< CodeEditor
height = { 300 }
height = { 300 }
language = "csv"
language = "csv"
value = { defaultOptions . map ( ( o ) = > ` ${ o . text } , ${ o . value } ` ) . join ( '\n' ) }
value = { defaultOptions . map ( ( o ) = > ` ${ o . text } , ${ o . value } ` ) . join ( '\n' ) }
onBlur = { updateDefaultOptions }
onBlur = { updateDefaultOptions }
onSave = { updateDefaultOptions }
onSave = { updateDefaultOptions }
showMiniMap = { false }
showMiniMap = { false }
showLineNumbers = { true }
showLineNumbers = { true }
/ >
/ >
) }
< / >
) }
) }
< VariableCheckboxField
{ datasourceSupported && ! inline && onAllowCustomValueChange && (
value = { allowCustomValue }
< VariableCheckboxField
name = "Allow custom values"
value = { allowCustomValue }
description = { t (
name = "Allow custom values"
'dashboard-scene.group-by-variable-form.description-enables-users-custom-values' ,
description = { t (
'Enables users to add custom values to the list'
'dashboard-scene.group-by-variable-form.description-enables-users-custom-values' ,
) }
'Enables users to add custom values to the list'
onChange = { onAllowCustomValueChange }
) }
testId = { selectors . pages . Dashboard . Settings . Variables . Edit . General . selectionOptionsAllowCustomValueSwitch }
onChange = { onAllowCustomValueChange }
/ >
testId = { selectors . pages . Dashboard . Settings . Variables . Edit . General . selectionOptionsAllowCustomValueSwitch }
/ >
) }
< / >
< / >
) ;
) ;
}
}