| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { forwardRef } from 'react'
- import { Form } from 'react-bootstrap-5'
- import { FormControl as BS3FormControl } from 'react-bootstrap'
- import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
- type OLFormControlProps = React.ComponentProps<(typeof Form)['Control']> & {
- bs3Props?: Record<string, unknown>
- }
- const OLFormControl = forwardRef<HTMLInputElement, OLFormControlProps>(
- (props, ref) => {
- const { bs3Props, ...rest } = props
- let bs3FormControlProps: React.ComponentProps<typeof BS3FormControl> = {
- id: rest.id,
- className: rest.className,
- style: rest.style,
- type: rest.type,
- value: rest.value,
- required: rest.required,
- disabled: rest.disabled,
- placeholder: rest.placeholder,
- readOnly: rest.readOnly,
- autoComplete: rest.autoComplete,
- minLength: rest.minLength,
- maxLength: rest.maxLength,
- onChange: rest.onChange as (e: React.ChangeEvent<unknown>) => void,
- onKeyDown: rest.onKeyDown as (e: React.KeyboardEvent<unknown>) => void,
- onFocus: rest.onFocus as (e: React.FocusEvent<unknown>) => void,
- onInvalid: rest.onInvalid as (e: React.InvalidEvent<unknown>) => void,
- inputRef: (inputElement: HTMLInputElement) => {
- if (typeof ref === 'function') {
- ref(inputElement)
- } else if (ref) {
- ref.current = inputElement
- }
- },
- ...bs3Props,
- }
- // get all `aria-*` and `data-*` attributes
- const extraProps = Object.entries(rest).reduce(
- (acc, [key, value]) => {
- if (key.startsWith('aria-') || key.startsWith('data-')) {
- acc[key] = value
- }
- return acc
- },
- {} as Record<string, string>
- )
- bs3FormControlProps = {
- ...bs3FormControlProps,
- ...extraProps,
- 'data-ol-dirty': rest['data-ol-dirty'],
- } as typeof bs3FormControlProps & Record<string, unknown>
- return (
- <BootstrapVersionSwitcher
- bs3={<BS3FormControl {...bs3FormControlProps} />}
- bs5={<Form.Control ref={ref} {...rest} />}
- />
- )
- }
- )
- OLFormControl.displayName = 'OLFormControl'
- export default OLFormControl
|