Created
October 9, 2023 08:08
-
-
Save mckn/b33b2fc8562fcaa16df0d1ba72acbbaa to your computer and use it in GitHub Desktop.
Example: query editor with some options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { ReactElement } from 'react'; | |
import { css } from '@emotion/css'; | |
import { useStyles2, Segment } from '@grafana/ui'; | |
import { GrafanaTheme2, SelectableValue } from '@grafana/data'; | |
import type { EditorProps } from './types'; | |
import { useAsync } from 'react-use'; | |
export function QueryEditor(props: EditorProps): ReactElement { | |
const { query } = props; | |
const { dimensions } = query; | |
const styles = useStyles2(getStyles); | |
// You can use regions here and pass it to the QueryModel | |
// in the constructor instead of passing it via the query. | |
const { value: regions } = useAsync(async () => { | |
// fetch options from API | |
const response = await fetchSomethingFromApi(dimensions); | |
// map to selectable values | |
return response.data.map((d) => { | |
return { | |
label: d.value, | |
value: d.value, | |
}; | |
}); | |
}, [dimensions]); // add any dependencies you have on the API call. | |
return ( | |
<div className={styles.editor}> | |
<Segment | |
options={regions} | |
onChange={(region) => | |
props.onChange({ | |
...query, | |
region: region, | |
}) | |
} | |
/> | |
</div> | |
); | |
} | |
function getStyles(theme: GrafanaTheme2) { | |
return { | |
editor: css` | |
margin: ${theme.spacing(0, 0.5, 0.5, 0)}; | |
`, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment