ol-form-control.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { forwardRef, ComponentProps, useCallback } from 'react'
  2. import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
  3. import FormControl, {
  4. type OLBS5FormControlProps,
  5. } from '@/features/ui/components/bootstrap-5/form/form-control'
  6. import BS3FormControl from '@/features/ui/components/bootstrap-3/form/form-control'
  7. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  8. import OLSpinner from '@/features/ui/components/ol/ol-spinner'
  9. import Icon from '@/shared/components/icon'
  10. import type { BsPrefixRefForwardingComponent } from 'react-bootstrap-5/helpers'
  11. type OLFormControlProps = OLBS5FormControlProps & {
  12. bs3Props?: Record<string, unknown>
  13. 'data-ol-dirty'?: unknown
  14. 'main-field'?: any // For the CM6's benefit in the editor search panel
  15. loading?: boolean
  16. }
  17. type BS3FormControlProps = ComponentProps<typeof BS3FormControl> & {
  18. 'main-field'?: any
  19. }
  20. const OLFormControl: BsPrefixRefForwardingComponent<
  21. 'input',
  22. OLFormControlProps
  23. > = forwardRef<HTMLInputElement, OLFormControlProps>((props, ref) => {
  24. const { bs3Props, append, ...rest } = props
  25. // Use a callback so that the ref passed to the BS3 FormControl is stable
  26. const bs3InputRef = useCallback(
  27. (inputElement: HTMLInputElement) => {
  28. if (typeof ref === 'function') {
  29. ref(inputElement)
  30. } else if (ref) {
  31. ref.current = inputElement
  32. }
  33. },
  34. [ref]
  35. )
  36. let bs3FormControlProps: BS3FormControlProps = {
  37. inputRef: bs3InputRef,
  38. componentClass: rest.as,
  39. bsSize: rest.size,
  40. id: rest.id,
  41. name: rest.name,
  42. className: rest.className,
  43. style: rest.style,
  44. type: rest.type,
  45. value: rest.value,
  46. defaultValue: rest.defaultValue,
  47. required: rest.required,
  48. disabled: rest.disabled,
  49. placeholder: rest.placeholder,
  50. readOnly: rest.readOnly,
  51. autoComplete: rest.autoComplete,
  52. autoFocus: rest.autoFocus,
  53. minLength: rest.minLength,
  54. maxLength: rest.maxLength,
  55. onChange: rest.onChange as BS3FormControlProps['onChange'],
  56. onKeyDown: rest.onKeyDown as BS3FormControlProps['onKeyDown'],
  57. onFocus: rest.onFocus as BS3FormControlProps['onFocus'],
  58. onBlur: rest.onBlur as BS3FormControlProps['onBlur'],
  59. onInvalid: rest.onInvalid as BS3FormControlProps['onInvalid'],
  60. prepend: rest.prepend,
  61. size: rest.htmlSize,
  62. 'main-field': rest['main-field'],
  63. children: rest.children,
  64. ...bs3Props,
  65. }
  66. bs3FormControlProps = {
  67. ...bs3FormControlProps,
  68. ...getAriaAndDataProps(rest),
  69. 'data-ol-dirty': rest['data-ol-dirty'],
  70. } as typeof bs3FormControlProps & Record<string, unknown>
  71. return (
  72. <BootstrapVersionSwitcher
  73. bs3={
  74. <BS3FormControl
  75. {...bs3FormControlProps}
  76. append={rest.loading ? <Icon type="spinner" spin /> : append}
  77. />
  78. }
  79. bs5={
  80. <FormControl
  81. ref={ref}
  82. {...rest}
  83. append={rest.loading ? <OLSpinner size="sm" /> : append}
  84. />
  85. }
  86. />
  87. )
  88. })
  89. OLFormControl.displayName = 'OLFormControl'
  90. export default OLFormControl