import type { ChangeEvent, FocusEvent, MouseEvent } from 'react'; import { useCallback } from 'react'; import { SchemaExamples } from '@rjsf/core'; import type { BaseInputTemplateProps, FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils'; import { ariaDescribedByIds, examplesId, getInputProps } from '@rjsf/utils'; import Form from 'react-bootstrap/Form'; export default function BaseInputTemplate< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any, >({ id, htmlName, placeholder, required, readonly, disabled, type, value, onChange, onChangeOverride, onBlur, onFocus, autofocus, options, schema, rawErrors = [], children, extraProps, registry, }: BaseInputTemplateProps) { const { ClearButton } = registry.templates.ButtonTemplates; const inputProps = { ...extraProps, ...getInputProps(schema, type, options), }; const handleChange = ({ target: { value: newValue } }: ChangeEvent) => onChange(newValue === '' ? options.emptyValue : newValue); const handleBlur = ({ target }: FocusEvent) => onBlur(id, target?.value); const handleFocus = ({ target }: FocusEvent) => onFocus(id, target?.value); const handleClear = useCallback( (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); onChange(options.emptyValue); }, [onChange, options.emptyValue], ); // const classNames = [rawErrors.length > 0 ? "is-invalid" : "", type === 'file' ? 'custom-file-label': ""] return ( <> 0 ? 'is-invalid' : ''} list={schema.examples ? examplesId(id) : undefined} {...inputProps} value={value || value === 0 ? value : ''} onChange={onChangeOverride || handleChange} onBlur={handleBlur} onFocus={handleFocus} aria-describedby={ariaDescribedByIds(id, !!schema.examples)} /> {options.allowClearTextInputs && !readonly && !disabled && value && ( )} {children} ); }