ol-button.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { forwardRef } from 'react'
  2. import BootstrapVersionSwitcher from '../bootstrap-5/bootstrap-version-switcher'
  3. import { Button as BS3Button } from 'react-bootstrap'
  4. import type { ButtonProps } from '@/features/ui/components/types/button-props'
  5. import type { ButtonProps as BS3ButtonPropsBase } from 'react-bootstrap'
  6. import Button from '../bootstrap-5/button'
  7. import classnames from 'classnames'
  8. import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
  9. import { callFnsInSequence } from '@/utils/functions'
  10. export type BS3ButtonSize = 'xsmall' | 'sm' | 'medium' | 'lg'
  11. export type OLButtonProps = ButtonProps & {
  12. bs3Props?: {
  13. loading?: React.ReactNode
  14. bsSize?: BS3ButtonSize
  15. block?: boolean
  16. className?: string
  17. onMouseOver?: React.MouseEventHandler<HTMLButtonElement>
  18. onMouseOut?: React.MouseEventHandler<HTMLButtonElement>
  19. onFocus?: React.FocusEventHandler<HTMLButtonElement>
  20. onBlur?: React.FocusEventHandler<HTMLButtonElement>
  21. }
  22. }
  23. // Resolve type mismatch of the onClick event handler
  24. export type BS3ButtonProps = Omit<BS3ButtonPropsBase, 'onClick'> & {
  25. onClick?: React.MouseEventHandler<any>
  26. }
  27. export function bs3ButtonProps(props: ButtonProps) {
  28. const bs3ButtonProps: BS3ButtonProps = {
  29. bsStyle: null,
  30. bsSize: props.size,
  31. className: classnames(`btn-${props.variant || 'primary'}`, props.className),
  32. disabled: props.isLoading || props.disabled,
  33. form: props.form,
  34. href: props.href,
  35. target: props.target,
  36. rel: props.rel,
  37. onClick: props.onClick,
  38. type: props.type,
  39. draggable: props.draggable,
  40. download: props.download,
  41. style: props.style,
  42. active: props.active,
  43. }
  44. return bs3ButtonProps
  45. }
  46. const OLButton = forwardRef<HTMLButtonElement, OLButtonProps>(
  47. ({ bs3Props = {}, ...rest }, ref) => {
  48. const { className: _, ...restBs3Props } = bs3Props
  49. // BS3 OverlayTrigger automatically provides 'onMouseOver', 'onMouseOut', 'onFocus', 'onBlur' event handlers
  50. const bs3FinalProps = {
  51. ...restBs3Props,
  52. onMouseOver: callFnsInSequence(bs3Props?.onMouseOver, rest.onMouseOver),
  53. onMouseOut: callFnsInSequence(bs3Props?.onMouseOut, rest.onMouseOut),
  54. onFocus: callFnsInSequence(bs3Props?.onFocus, rest.onFocus),
  55. onBlur: callFnsInSequence(bs3Props?.onBlur, rest.onBlur),
  56. }
  57. // Get all `aria-*` and `data-*` attributes
  58. const extraProps = getAriaAndDataProps(rest)
  59. return (
  60. <BootstrapVersionSwitcher
  61. bs3={
  62. <BS3Button
  63. {...bs3ButtonProps({
  64. ...rest,
  65. // Override the `className` with bs3 specific className (if provided)
  66. className: bs3Props?.className ?? rest.className,
  67. })}
  68. {...extraProps}
  69. {...bs3FinalProps}
  70. ref={ref as React.LegacyRef<any> | undefined}
  71. >
  72. {bs3Props?.loading || rest.children}
  73. </BS3Button>
  74. }
  75. bs5={<Button {...rest} ref={ref} />}
  76. />
  77. )
  78. }
  79. )
  80. OLButton.displayName = 'OLButton'
  81. export default OLButton