RowOptionsForm.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React, { FC, useCallback, useState } from 'react';
  2. import { Button, Field, Form, Modal, Input } from '@grafana/ui';
  3. import { RepeatRowSelect } from '../RepeatRowSelect/RepeatRowSelect';
  4. export type OnRowOptionsUpdate = (title: string, repeat?: string | null) => void;
  5. export interface Props {
  6. title: string;
  7. repeat?: string | null;
  8. onUpdate: OnRowOptionsUpdate;
  9. onCancel: () => void;
  10. }
  11. export const RowOptionsForm: FC<Props> = ({ repeat, title, onUpdate, onCancel }) => {
  12. const [newRepeat, setNewRepeat] = useState<string | null | undefined>(repeat);
  13. const onChangeRepeat = useCallback((name?: string | null) => setNewRepeat(name), [setNewRepeat]);
  14. return (
  15. <Form
  16. defaultValues={{ title }}
  17. onSubmit={(formData: { title: string }) => {
  18. onUpdate(formData.title, newRepeat);
  19. }}
  20. >
  21. {({ register }) => (
  22. <>
  23. <Field label="Title">
  24. <Input {...register('title')} type="text" />
  25. </Field>
  26. <Field label="Repeat for">
  27. <RepeatRowSelect repeat={newRepeat} onChange={onChangeRepeat} />
  28. </Field>
  29. <Modal.ButtonRow>
  30. <Button type="button" variant="secondary" onClick={onCancel} fill="outline">
  31. Cancel
  32. </Button>
  33. <Button type="submit">Update</Button>
  34. </Modal.ButtonRow>
  35. </>
  36. )}
  37. </Form>
  38. );
  39. };