QueryHeaderSwitch.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { css } from '@emotion/css';
  2. import { uniqueId } from 'lodash';
  3. import React, { HTMLProps, useRef } from 'react';
  4. import { GrafanaTheme2 } from '@grafana/data';
  5. import { Stack } from '@grafana/experimental';
  6. import { Switch, useStyles2 } from '@grafana/ui';
  7. export interface Props extends Omit<HTMLProps<HTMLInputElement>, 'value' | 'ref'> {
  8. value?: boolean;
  9. label: string;
  10. }
  11. export function QueryHeaderSwitch({ label, ...inputProps }: Props) {
  12. const dashedLabel = label.replace(' ', '-');
  13. const switchIdRef = useRef(uniqueId(`switch-${dashedLabel}`));
  14. const styles = useStyles2(getStyles);
  15. return (
  16. <Stack gap={1}>
  17. <label htmlFor={switchIdRef.current} className={styles.switchLabel}>
  18. {label}
  19. </label>
  20. <Switch {...inputProps} id={switchIdRef.current} />
  21. </Stack>
  22. );
  23. }
  24. const getStyles = (theme: GrafanaTheme2) => {
  25. return {
  26. switchLabel: css({
  27. color: theme.colors.text.secondary,
  28. cursor: 'pointer',
  29. fontSize: theme.typography.bodySmall.fontSize,
  30. '&:hover': {
  31. color: theme.colors.text.primary,
  32. },
  33. }),
  34. };
  35. };