button-wrapper.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import BootstrapVersionSwitcher from '../bootstrap-version-switcher'
  2. import { Button as BS3Button } from 'react-bootstrap'
  3. import type { ButtonProps } from '@/features/ui/components/types/button-props'
  4. import type { ButtonProps as BS3ButtonPropsBase } from 'react-bootstrap'
  5. import Button from '../button'
  6. export type ButtonWrapperProps = ButtonProps & {
  7. bs3Props?: {
  8. bsStyle?: string | null
  9. loading?: React.ReactNode
  10. }
  11. }
  12. // Resolve type mismatch of the onClick event handler
  13. export type BS3ButtonProps = Omit<BS3ButtonPropsBase, 'onClick'> & {
  14. onClick?: React.MouseEventHandler<any>
  15. }
  16. // maps Bootstrap 5 sizes to Bootstrap 3 sizes
  17. export const mapBsButtonSizes = (
  18. size: ButtonProps['size']
  19. ): 'sm' | 'lg' | undefined =>
  20. size === 'small' ? 'sm' : size === 'large' ? 'lg' : undefined
  21. export default function ButtonWrapper(props: ButtonWrapperProps) {
  22. const { bs3Props, ...rest } = props
  23. const bs3ButtonProps: BS3ButtonProps = {
  24. bsStyle: rest.variant,
  25. bsSize: mapBsButtonSizes(rest.size),
  26. className: rest.className,
  27. disabled: rest.isLoading || rest.disabled,
  28. form: rest.form,
  29. onClick: rest.onClick,
  30. type: rest.type,
  31. ...bs3Props,
  32. }
  33. return (
  34. <BootstrapVersionSwitcher
  35. bs3={
  36. <BS3Button {...bs3ButtonProps}>
  37. {bs3Props?.loading || rest.children}
  38. </BS3Button>
  39. }
  40. bs5={<Button {...rest} />}
  41. />
  42. )
  43. }