col-wrapper.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Col } from 'react-bootstrap-5'
  2. import { Col as BS3Col } from 'react-bootstrap'
  3. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  4. type ColWrapperProps = React.ComponentProps<typeof Col> & {
  5. bs3Props?: Record<string, unknown>
  6. }
  7. function ColWrapper(props: ColWrapperProps) {
  8. const { bs3Props, ...rest } = props
  9. const sizes = new Set(['xs', 'sm', 'md', 'lg', 'xl', 'xxl'])
  10. const getBs3Sizes = (obj: typeof rest) => {
  11. return Object.entries(obj).reduce(
  12. (prev, [key, value]) => {
  13. if (sizes.has(key)) {
  14. if (typeof value === 'object') {
  15. prev[key] = value.span
  16. prev[`${key}Offset`] = value.offset
  17. } else {
  18. prev[key] = value
  19. }
  20. }
  21. return prev
  22. },
  23. {} as Record<string, (typeof rest)['xs']>
  24. )
  25. }
  26. const bs3ColProps: React.ComponentProps<typeof BS3Col> = {
  27. children: rest.children,
  28. className: rest.className,
  29. ...getBs3Sizes(rest),
  30. ...bs3Props,
  31. }
  32. return (
  33. <BootstrapVersionSwitcher
  34. bs3={<BS3Col {...bs3ColProps} />}
  35. bs5={<Col {...rest} />}
  36. />
  37. )
  38. }
  39. export default ColWrapper