ol-form-control.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { forwardRef, ComponentProps } from 'react'
  2. import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
  3. import FormControl from '@/features/ui/components/bootstrap-5/form/form-control'
  4. import BS3FormControl from '@/features/ui/components/bootstrap-3/form/form-control'
  5. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  6. type OLFormControlProps = ComponentProps<typeof FormControl> & {
  7. bs3Props?: Record<string, unknown>
  8. 'data-ol-dirty'?: unknown
  9. }
  10. type BS3FormControlProps = ComponentProps<typeof BS3FormControl>
  11. const OLFormControl = forwardRef<HTMLInputElement, OLFormControlProps>(
  12. (props, ref) => {
  13. const { bs3Props, ...rest } = props
  14. let bs3FormControlProps: BS3FormControlProps = {
  15. componentClass: rest.as,
  16. id: rest.id,
  17. name: rest.name,
  18. className: rest.className,
  19. style: rest.style,
  20. type: rest.type,
  21. value: rest.value,
  22. defaultValue: rest.defaultValue,
  23. required: rest.required,
  24. disabled: rest.disabled,
  25. placeholder: rest.placeholder,
  26. readOnly: rest.readOnly,
  27. autoComplete: rest.autoComplete,
  28. autoFocus: rest.autoFocus,
  29. minLength: rest.minLength,
  30. maxLength: rest.maxLength,
  31. onChange: rest.onChange as BS3FormControlProps['onChange'],
  32. onKeyDown: rest.onKeyDown as BS3FormControlProps['onKeyDown'],
  33. onFocus: rest.onFocus as BS3FormControlProps['onFocus'],
  34. onBlur: rest.onBlur as BS3FormControlProps['onBlur'],
  35. onInvalid: rest.onInvalid as BS3FormControlProps['onInvalid'],
  36. inputRef: (inputElement: HTMLInputElement) => {
  37. if (typeof ref === 'function') {
  38. ref(inputElement)
  39. } else if (ref) {
  40. ref.current = inputElement
  41. }
  42. },
  43. prepend: rest.prepend,
  44. append: rest.append,
  45. ...bs3Props,
  46. }
  47. bs3FormControlProps = {
  48. ...bs3FormControlProps,
  49. ...getAriaAndDataProps(rest),
  50. 'data-ol-dirty': rest['data-ol-dirty'],
  51. } as typeof bs3FormControlProps & Record<string, unknown>
  52. return (
  53. <BootstrapVersionSwitcher
  54. bs3={<BS3FormControl {...bs3FormControlProps} />}
  55. bs5={<FormControl ref={ref} {...rest} />}
  56. />
  57. )
  58. }
  59. )
  60. OLFormControl.displayName = 'OLFormControl'
  61. export default OLFormControl