ol-form-select.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { forwardRef } from 'react'
  2. import { Form, FormSelectProps } from 'react-bootstrap-5'
  3. import {
  4. FormControl as BS3FormControl,
  5. FormControlProps as BS3FormControlProps,
  6. } from 'react-bootstrap'
  7. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  8. import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
  9. type OLFormSelectProps = FormSelectProps & {
  10. bs3Props?: Record<string, unknown>
  11. }
  12. const OLFormSelect = forwardRef<HTMLSelectElement, OLFormSelectProps>(
  13. (props, ref) => {
  14. const { bs3Props, ...bs5Props } = props
  15. const bs3FormSelectProps: BS3FormControlProps = {
  16. children: bs5Props.children,
  17. bsSize: bs5Props.size,
  18. name: bs5Props.name,
  19. value: bs5Props.value,
  20. defaultValue: bs5Props.defaultValue,
  21. disabled: bs5Props.disabled,
  22. onChange: bs5Props.onChange as BS3FormControlProps['onChange'],
  23. required: bs5Props.required,
  24. placeholder: bs5Props.placeholder,
  25. className: bs5Props.className,
  26. inputRef: (inputElement: HTMLInputElement) => {
  27. if (typeof ref === 'function') {
  28. ref(inputElement as unknown as HTMLSelectElement)
  29. } else if (ref) {
  30. ref.current = inputElement as unknown as HTMLSelectElement
  31. }
  32. },
  33. ...bs3Props,
  34. }
  35. // Get all `aria-*` and `data-*` attributes
  36. const extraProps = getAriaAndDataProps(bs5Props)
  37. return (
  38. <BootstrapVersionSwitcher
  39. bs3={
  40. <BS3FormControl
  41. componentClass="select"
  42. {...bs3FormSelectProps}
  43. {...extraProps}
  44. />
  45. }
  46. bs5={<Form.Select ref={ref} {...bs5Props} />}
  47. />
  48. )
  49. }
  50. )
  51. OLFormSelect.displayName = 'OLFormSelect'
  52. export default OLFormSelect