ol-button.tsx 1.7 KB

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