ActionBar.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { noop } from 'lodash'
  2. import { PropsWithChildren, useCallback, useState } from 'react'
  3. import { Button, KeyboardShortcut } from 'ui'
  4. import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
  5. import { useShortcut } from '@/state/shortcuts/useShortcut'
  6. interface ActionBarProps {
  7. loading?: boolean
  8. disableApply?: boolean
  9. hideApply?: boolean
  10. applyButtonLabel?: string
  11. backButtonLabel?: string
  12. applyFunction?: (resolve: any) => void
  13. closePanel: () => void
  14. formId?: string
  15. visible?: boolean
  16. }
  17. export const ActionBar = ({
  18. loading = false,
  19. disableApply = false,
  20. hideApply = false,
  21. children = undefined,
  22. applyButtonLabel = 'Apply',
  23. backButtonLabel = 'Back',
  24. applyFunction = undefined,
  25. closePanel = noop,
  26. formId,
  27. visible = true,
  28. }: PropsWithChildren<ActionBarProps>) => {
  29. const [isRunning, setIsRunning] = useState(false)
  30. const onSelectApply = useCallback(async () => {
  31. const applyCallback = () => new Promise((resolve) => applyFunction?.(resolve))
  32. setIsRunning(true)
  33. await applyCallback()
  34. setIsRunning(false)
  35. }, [applyFunction])
  36. const handleSave = useCallback(() => {
  37. if (isRunning || loading || disableApply || hideApply) return
  38. if (formId) {
  39. const form = document.getElementById(formId) as HTMLFormElement | null
  40. if (form) {
  41. form.requestSubmit()
  42. }
  43. } else if (applyFunction) {
  44. onSelectApply()
  45. }
  46. }, [isRunning, loading, disableApply, hideApply, formId, applyFunction, onSelectApply])
  47. useShortcut(SHORTCUT_IDS.ACTION_BAR_SAVE, handleSave, { enabled: visible })
  48. return (
  49. <div className="flex w-full items-center gap-3 border-t border-default px-3 py-4">
  50. {children}
  51. <div className="flex items-center gap-3 ml-auto">
  52. <Button
  53. type="default"
  54. htmlType="button"
  55. onClick={closePanel}
  56. disabled={isRunning || loading}
  57. >
  58. {backButtonLabel}
  59. </Button>
  60. {applyFunction !== undefined ? (
  61. // Old solution, necessary when loading is handled by this component itself
  62. <Button
  63. onClick={onSelectApply}
  64. disabled={disableApply || isRunning || loading}
  65. loading={isRunning || loading}
  66. iconRight={
  67. isRunning || loading ? undefined : (
  68. <KeyboardShortcut keys={['Meta', 'Enter']} variant="inline" />
  69. )
  70. }
  71. >
  72. {applyButtonLabel}
  73. </Button>
  74. ) : !hideApply ? (
  75. // New solution, when using the Form component, loading is handled by the Form itself
  76. // Does not require applyFunction() callback
  77. <Button
  78. disabled={loading || disableApply}
  79. loading={loading}
  80. data-testid="action-bar-save-row"
  81. htmlType="submit"
  82. form={formId}
  83. iconRight={
  84. loading ? undefined : <KeyboardShortcut keys={['Meta', 'Enter']} variant="inline" />
  85. }
  86. >
  87. {applyButtonLabel}
  88. </Button>
  89. ) : (
  90. <div />
  91. )}
  92. </div>
  93. </div>
  94. )
  95. }