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-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-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-05-15 10:31:00 -04:00
|
|
|
export default function OLButton(props: OLButtonProps) {
|
2024-04-16 11:06:42 -04:00
|
|
|
const { bs3Props, ...rest } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={
|
2024-06-06 11:37:47 -04:00
|
|
|
<BS3Button {...bs3ButtonProps(rest)} {...bs3Props}>
|
2024-04-16 11:06:42 -04:00
|
|
|
{bs3Props?.loading || rest.children}
|
|
|
|
</BS3Button>
|
|
|
|
}
|
|
|
|
bs5={<Button {...rest} />}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|