2024-05-15 10:31:00 -04:00
|
|
|
import BootstrapVersionSwitcher from '../bootstrap-5/bootstrap-version-switcher'
|
2024-04-16 11:06:42 -04:00
|
|
|
import { Button as BS3Button } from 'react-bootstrap'
|
|
|
|
import type { ButtonProps } from '@/features/ui/components/types/button-props'
|
|
|
|
import type { ButtonProps as BS3ButtonPropsBase } from 'react-bootstrap'
|
2024-05-15 10:31:00 -04:00
|
|
|
import Button from '../bootstrap-5/button'
|
2024-06-06 11:37:47 -04:00
|
|
|
import classnames from 'classnames'
|
2024-09-04 07:00:21 -04:00
|
|
|
import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
|
2024-04-16 11:06:42 -04:00
|
|
|
|
2024-05-30 11:09:57 -04:00
|
|
|
export type BS3ButtonSize = 'xsmall' | 'sm' | 'medium' | 'lg'
|
|
|
|
|
2024-05-15 10:31:00 -04:00
|
|
|
export type OLButtonProps = ButtonProps & {
|
2024-04-16 11:06:42 -04:00
|
|
|
bs3Props?: {
|
|
|
|
loading?: React.ReactNode
|
2024-05-30 11:09:57 -04:00
|
|
|
bsSize?: BS3ButtonSize
|
2024-06-13 08:44:38 -04:00
|
|
|
block?: boolean
|
2024-09-30 04:11:26 -04:00
|
|
|
className?: string
|
2024-04-16 11:06:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve type mismatch of the onClick event handler
|
|
|
|
export type BS3ButtonProps = Omit<BS3ButtonPropsBase, 'onClick'> & {
|
|
|
|
onClick?: React.MouseEventHandler<any>
|
|
|
|
}
|
|
|
|
|
2024-06-06 11:37:47 -04:00
|
|
|
export function bs3ButtonProps(props: ButtonProps) {
|
|
|
|
const bs3ButtonProps: BS3ButtonProps = {
|
|
|
|
bsStyle: null,
|
|
|
|
bsSize: mapBsButtonSizes(props.size),
|
|
|
|
className: classnames(`btn-${props.variant || 'primary'}`, props.className),
|
|
|
|
disabled: props.isLoading || props.disabled,
|
|
|
|
form: props.form,
|
|
|
|
href: props.href,
|
2024-06-20 08:18:22 -04:00
|
|
|
target: props.target,
|
|
|
|
rel: props.rel,
|
2024-06-06 11:37:47 -04:00
|
|
|
onClick: props.onClick,
|
|
|
|
type: props.type,
|
|
|
|
}
|
|
|
|
return bs3ButtonProps
|
|
|
|
}
|
|
|
|
|
2024-04-16 11:06:42 -04:00
|
|
|
// maps Bootstrap 5 sizes to Bootstrap 3 sizes
|
|
|
|
export const mapBsButtonSizes = (
|
|
|
|
size: ButtonProps['size']
|
|
|
|
): 'sm' | 'lg' | undefined =>
|
|
|
|
size === 'small' ? 'sm' : size === 'large' ? 'lg' : undefined
|
|
|
|
|
2024-09-30 04:11:26 -04:00
|
|
|
export default function OLButton({ bs3Props = {}, ...rest }: OLButtonProps) {
|
|
|
|
const { className: _, ...restBs3Props } = bs3Props
|
2024-04-16 11:06:42 -04:00
|
|
|
|
2024-09-04 07:00:21 -04:00
|
|
|
// Get all `aria-*` and `data-*` attributes
|
|
|
|
const extraProps = getAriaAndDataProps(rest)
|
|
|
|
|
2024-04-16 11:06:42 -04:00
|
|
|
return (
|
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={
|
2024-09-30 04:11:26 -04:00
|
|
|
<BS3Button
|
|
|
|
{...bs3ButtonProps({
|
|
|
|
...rest,
|
|
|
|
// Override the `className` with bs3 specific className (if provided)
|
|
|
|
className: bs3Props?.className ?? rest.className,
|
|
|
|
})}
|
|
|
|
{...restBs3Props}
|
|
|
|
{...extraProps}
|
|
|
|
>
|
2024-04-16 11:06:42 -04:00
|
|
|
{bs3Props?.loading || rest.children}
|
|
|
|
</BS3Button>
|
|
|
|
}
|
|
|
|
bs5={<Button {...rest} />}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|