ActionRow.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { css } from '@emotion/css';
  2. import React, { FC, ChangeEvent, FormEvent } from 'react';
  3. import { GrafanaTheme2, SelectableValue } from '@grafana/data';
  4. import { config } from '@grafana/runtime';
  5. import { HorizontalGroup, RadioButtonGroup, Checkbox, InlineSwitch, useStyles2 } from '@grafana/ui';
  6. import { SortPicker } from 'app/core/components/Select/SortPicker';
  7. import { TagFilter } from 'app/core/components/TagFilter/TagFilter';
  8. import { SearchSrv } from 'app/core/services/search_srv';
  9. import { DashboardQuery, SearchLayout } from '../types';
  10. export const layoutOptions = [
  11. { value: SearchLayout.Folders, icon: 'folder', ariaLabel: 'View by folders' },
  12. { value: SearchLayout.List, icon: 'list-ul', ariaLabel: 'View as list' },
  13. ];
  14. const searchSrv = new SearchSrv();
  15. interface Props {
  16. onLayoutChange: (layout: SearchLayout) => void;
  17. setShowPreviews: (newValue: boolean) => void;
  18. onSortChange: (value: SelectableValue) => void;
  19. onStarredFilterChange?: (event: FormEvent<HTMLInputElement>) => void;
  20. onTagFilterChange: (tags: string[]) => void;
  21. query: DashboardQuery;
  22. showStarredFilter?: boolean;
  23. hideLayout?: boolean;
  24. showPreviews?: boolean;
  25. }
  26. export const ActionRow: FC<Props> = ({
  27. onLayoutChange,
  28. setShowPreviews,
  29. onSortChange,
  30. onStarredFilterChange = () => {},
  31. onTagFilterChange,
  32. query,
  33. showStarredFilter,
  34. hideLayout,
  35. showPreviews,
  36. }) => {
  37. const styles = useStyles2(getStyles);
  38. const previewsEnabled = config.featureToggles.dashboardPreviews;
  39. return (
  40. <div className={styles.actionRow}>
  41. <div className={styles.rowContainer}>
  42. <HorizontalGroup spacing="md" width="auto">
  43. {!hideLayout ? (
  44. <RadioButtonGroup options={layoutOptions} onChange={onLayoutChange} value={query.layout} />
  45. ) : null}
  46. <SortPicker onChange={onSortChange} value={query.sort?.value} />
  47. {previewsEnabled && (
  48. <InlineSwitch
  49. id="search-show-previews"
  50. label="Show previews"
  51. showLabel
  52. value={showPreviews}
  53. onChange={(ev: ChangeEvent<HTMLInputElement>) => setShowPreviews(ev.target.checked)}
  54. transparent
  55. />
  56. )}
  57. </HorizontalGroup>
  58. </div>
  59. <HorizontalGroup spacing="md" width="auto">
  60. {showStarredFilter && (
  61. <div className={styles.checkboxWrapper}>
  62. <Checkbox label="Filter by starred" onChange={onStarredFilterChange} value={query.starred} />
  63. </div>
  64. )}
  65. <TagFilter isClearable tags={query.tag} tagOptions={searchSrv.getDashboardTags} onChange={onTagFilterChange} />
  66. </HorizontalGroup>
  67. </div>
  68. );
  69. };
  70. ActionRow.displayName = 'ActionRow';
  71. export const getStyles = (theme: GrafanaTheme2) => {
  72. return {
  73. actionRow: css`
  74. display: none;
  75. ${theme.breakpoints.up('md')} {
  76. display: flex;
  77. justify-content: space-between;
  78. align-items: center;
  79. padding-bottom: ${theme.spacing(2)};
  80. width: 100%;
  81. }
  82. `,
  83. rowContainer: css`
  84. margin-right: ${theme.spacing(1)};
  85. `,
  86. checkboxWrapper: css`
  87. label {
  88. line-height: 1.2;
  89. }
  90. `,
  91. };
  92. };