ol-button.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import classnames from 'classnames'
  7. import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
  8. export type BS3ButtonSize = 'xsmall' | 'sm' | 'medium' | 'lg'
  9. export type OLButtonProps = ButtonProps & {
  10. bs3Props?: {
  11. loading?: React.ReactNode
  12. bsSize?: BS3ButtonSize
  13. block?: boolean
  14. className?: string
  15. }
  16. }
  17. // Resolve type mismatch of the onClick event handler
  18. export type BS3ButtonProps = Omit<BS3ButtonPropsBase, 'onClick'> & {
  19. onClick?: React.MouseEventHandler<any>
  20. }
  21. export function bs3ButtonProps(props: ButtonProps) {
  22. const bs3ButtonProps: BS3ButtonProps = {
  23. bsStyle: null,
  24. bsSize: mapBsButtonSizes(props.size),
  25. className: classnames(`btn-${props.variant || 'primary'}`, props.className),
  26. disabled: props.isLoading || props.disabled,
  27. form: props.form,
  28. href: props.href,
  29. target: props.target,
  30. rel: props.rel,
  31. onClick: props.onClick,
  32. type: props.type,
  33. }
  34. return bs3ButtonProps
  35. }
  36. // maps Bootstrap 5 sizes to Bootstrap 3 sizes
  37. export const mapBsButtonSizes = (
  38. size: ButtonProps['size']
  39. ): 'sm' | 'lg' | undefined =>
  40. size === 'small' ? 'sm' : size === 'large' ? 'lg' : undefined
  41. export default function OLButton({ bs3Props = {}, ...rest }: OLButtonProps) {
  42. const { className: _, ...restBs3Props } = bs3Props
  43. // Get all `aria-*` and `data-*` attributes
  44. const extraProps = getAriaAndDataProps(rest)
  45. return (
  46. <BootstrapVersionSwitcher
  47. bs3={
  48. <BS3Button
  49. {...bs3ButtonProps({
  50. ...rest,
  51. // Override the `className` with bs3 specific className (if provided)
  52. className: bs3Props?.className ?? rest.className,
  53. })}
  54. {...restBs3Props}
  55. {...extraProps}
  56. >
  57. {bs3Props?.loading || rest.children}
  58. </BS3Button>
  59. }
  60. bs5={<Button {...rest} />}
  61. />
  62. )
  63. }