FormActions.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Button } from 'ui'
  2. interface Props {
  3. form: React.HTMLProps<HTMLButtonElement>['form']
  4. hasChanges: boolean | undefined // Disables submit button if false
  5. handleReset: () => void // Handling a reset/cancel of the form
  6. helper?: React.ReactNode // Helper text to show alongside actions
  7. disabled?: boolean
  8. isSubmitting?: boolean
  9. submitText?: string
  10. }
  11. export const FormActions = ({
  12. form,
  13. hasChanges = undefined,
  14. handleReset,
  15. helper,
  16. disabled = false,
  17. isSubmitting,
  18. submitText = 'Save',
  19. }: Props) => {
  20. const isDisabled = isSubmitting || disabled || (!hasChanges && hasChanges !== undefined)
  21. return (
  22. <div
  23. className={[
  24. 'flex w-full items-center gap-2',
  25. // justify actions to right if no helper text
  26. helper ? 'justify-between' : 'justify-end',
  27. ].join(' ')}
  28. >
  29. {helper && <span className="text-sm text-foreground-lighter">{helper}</span>}
  30. <div className="flex items-center gap-2">
  31. <Button disabled={isDisabled} type="default" htmlType="reset" onClick={() => handleReset()}>
  32. Cancel
  33. </Button>
  34. <Button
  35. form={form}
  36. type="primary"
  37. htmlType="submit"
  38. disabled={isDisabled}
  39. loading={isSubmitting}
  40. >
  41. {submitText}
  42. </Button>
  43. </div>
  44. </div>
  45. )
  46. }