import { ChangeEvent, FocusEvent, MouseEvent, useCallback } from 'react';
import { Input, InputNumber } from 'antd';
import {
ariaDescribedByIds,
BaseInputTemplateProps,
examplesId,
getInputProps,
FormContextType,
GenericObjectType,
RJSFSchema,
StrictRJSFSchema,
} from '@rjsf/utils';
import { SchemaExamples } from '@rjsf/core';
const INPUT_STYLE = {
width: '100%',
};
/** The `BaseInputTemplate` is the template to use to render the basic `` component for the `core` theme.
* It is used as the template for rendering many of the based widgets that differ by `type` and callbacks only.
* It can be customized/overridden for other themes or individual implementations as needed.
*
* @param props - The `WidgetProps` for this template
*/
export default function BaseInputTemplate<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any,
>(props: BaseInputTemplateProps) {
const {
disabled,
registry,
id,
htmlName,
onBlur,
onChange,
onChangeOverride,
onFocus,
options,
placeholder,
readonly,
schema,
value,
type,
} = props;
const { formContext } = registry;
// InputNumber doesn't use a native directly - it wraps it and controls the stepping behavior
// through its own props. The step prop in Ant Design expects a number, not the string "any"
const inputProps = getInputProps(schema, type, options, false);
const { readonlyAsDisabled = true } = formContext as GenericObjectType;
const { ClearButton } = registry.templates.ButtonTemplates;
const handleNumberChange = (nextValue: number | null) => onChange(nextValue);
const handleTextChange = onChangeOverride
? onChangeOverride
: ({ target }: ChangeEvent) => onChange(target.value === '' ? options.emptyValue : target.value);
const handleBlur = ({ target }: FocusEvent) => onBlur(id, target && target.value);
const handleFocus = ({ target }: FocusEvent) => onFocus(id, target && target.value);
const handleClear = useCallback(
(e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
onChange(options.emptyValue ?? '');
},
[onChange, options.emptyValue],
);
const input =
inputProps.type === 'number' || inputProps.type === 'integer' ? (
) : (
);
return (
<>
{input}
{options.allowClearTextInputs && !readonly && !disabled && value && (
)}
>
);
}