ol-button.tsx 1.5 KB

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