ol-form-control.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. onInvalid: rest.onInvalid as BS3FormControlProps['onInvalid'],
  35. inputRef: (inputElement: HTMLInputElement) => {
  36. if (typeof ref === 'function') {
  37. ref(inputElement)
  38. } else if (ref) {
  39. ref.current = inputElement
  40. }
  41. },
  42. prepend: rest.prepend,
  43. append: rest.append,
  44. ...bs3Props,
  45. }
  46. bs3FormControlProps = {
  47. ...bs3FormControlProps,
  48. ...getAriaAndDataProps(rest),
  49. 'data-ol-dirty': rest['data-ol-dirty'],
  50. } as typeof bs3FormControlProps & Record<string, unknown>
  51. return (
  52. <BootstrapVersionSwitcher
  53. bs3={<BS3FormControl {...bs3FormControlProps} />}
  54. bs5={<FormControl ref={ref} {...rest} />}
  55. />
  56. )
  57. }
  58. )
  59. OLFormControl.displayName = 'OLFormControl'
  60. export default OLFormControl