PlaylistForm.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React, { FC } from 'react';
  2. import { selectors } from '@grafana/e2e-selectors';
  3. import { config } from '@grafana/runtime';
  4. import { Button, Field, Form, HorizontalGroup, Input, LinkButton } from '@grafana/ui';
  5. import { DashboardPickerByID } from 'app/core/components/editors/DashboardPickerByID';
  6. import { TagFilter } from '../../core/components/TagFilter/TagFilter';
  7. import { SearchSrv } from '../../core/services/search_srv';
  8. import { PlaylistTable } from './PlaylistTable';
  9. import { Playlist } from './types';
  10. import { usePlaylistItems } from './usePlaylistItems';
  11. interface PlaylistFormProps {
  12. onSubmit: (playlist: Playlist) => void;
  13. playlist: Playlist;
  14. }
  15. const searchSrv = new SearchSrv();
  16. export const PlaylistForm: FC<PlaylistFormProps> = ({ onSubmit, playlist }) => {
  17. const { name, interval, items: propItems } = playlist;
  18. const { items, addById, addByTag, deleteItem, moveDown, moveUp } = usePlaylistItems(propItems);
  19. return (
  20. <>
  21. <Form onSubmit={(list: Playlist) => onSubmit({ ...list, items })} validateOn={'onBlur'}>
  22. {({ register, errors }) => {
  23. const isDisabled = items.length === 0 || Object.keys(errors).length > 0;
  24. return (
  25. <>
  26. <Field label="Name" invalid={!!errors.name} error={errors?.name?.message}>
  27. <Input
  28. type="text"
  29. {...register('name', { required: 'Name is required' })}
  30. placeholder="Name"
  31. defaultValue={name}
  32. aria-label={selectors.pages.PlaylistForm.name}
  33. />
  34. </Field>
  35. <Field label="Interval" invalid={!!errors.interval} error={errors?.interval?.message}>
  36. <Input
  37. type="text"
  38. {...register('interval', { required: 'Interval is required' })}
  39. placeholder="5m"
  40. defaultValue={interval ?? '5m'}
  41. aria-label={selectors.pages.PlaylistForm.interval}
  42. />
  43. </Field>
  44. <PlaylistTable items={items} onMoveUp={moveUp} onMoveDown={moveDown} onDelete={deleteItem} />
  45. <div className="gf-form-group">
  46. <h3 className="page-headering">Add dashboards</h3>
  47. <Field label="Add by title">
  48. <DashboardPickerByID onChange={addById} id="dashboard-picker" isClearable />
  49. </Field>
  50. <Field label="Add by tag">
  51. <TagFilter
  52. isClearable
  53. tags={[]}
  54. hideValues
  55. tagOptions={searchSrv.getDashboardTags}
  56. onChange={addByTag}
  57. placeholder={''}
  58. />
  59. </Field>
  60. </div>
  61. <HorizontalGroup>
  62. <Button type="submit" variant="primary" disabled={isDisabled}>
  63. Save
  64. </Button>
  65. <LinkButton variant="secondary" href={`${config.appSubUrl}/playlists`}>
  66. Cancel
  67. </LinkButton>
  68. </HorizontalGroup>
  69. </>
  70. );
  71. }}
  72. </Form>
  73. </>
  74. );
  75. };