import { ChangeEvent, FocusEvent, ReactNode, useCallback, useEffect, useRef } from "react"; import { CommonInputProps } from "~~/components/scaffold-eth"; type InputBaseProps = CommonInputProps & { error?: boolean; prefix?: ReactNode; suffix?: ReactNode; reFocus?: boolean; }; export const InputBase = string } | undefined = string>({ name, value, onChange, placeholder, error, disabled, prefix, suffix, reFocus, }: InputBaseProps) => { const inputReft = useRef(null); let modifier = ""; if (error) { modifier = "border-error"; } else if (disabled) { modifier = "border-disabled bg-base-300"; } const handleChange = useCallback( (e: ChangeEvent) => { onChange(e.target.value as unknown as T); }, [onChange], ); // Runs only when reFocus prop is passed, useful for setting the cursor // at the end of the input. Example AddressInput const onFocus = (e: FocusEvent) => { if (reFocus !== undefined) { e.currentTarget.setSelectionRange(e.currentTarget.value.length, e.currentTarget.value.length); } }; useEffect(() => { if (reFocus !== undefined && reFocus === true) inputReft.current?.focus(); }, [reFocus]); return (
{prefix} {suffix}
); };