useInputBase
A base hook for all input-like components (e.g., Input, AutoSuggest). It centralizes controlled/uncontrolled value handling, focus state, common behaviors like selectOnFocus or clearOnEscape, and builds ARIA/data attributes. Styling is not enforced.
Import
import { useInputBase } from "@libdev-ui/base/hooks";
API
useInputBase(props)
Props:
value?: string— Controlled valuedefaultValue?: string— Uncontrolled initial valuedisabled?: boolean— Disables the fieldreadOnly?: boolean— Read-only fieldrequired?: boolean— Marks as requiredautoFocus?: boolean— Focus on mountselectOnFocus?: boolean— Select all text on focusclearOnEscape?: boolean— Clear the field onEscapeid?: string,name?: string,placeholder?: string,type?: HTMLInputTypeAttributeinputRef?: React.Ref<HTMLInputElement>- Events:
onChange(event, value)onFocus(event)onBlur(event)onKeyDown(event)onKeyUp(event)onClear()— Fired after clear (via clear button orEscape)
Return:
value: string— Current valuefocused: boolean— Whether the field is focuseddisabled: booleanreadOnly: booleanrequired: booleaninputRef: RefObject<HTMLInputElement>- Methods:
focus(),blur(),select(),setValue(next) getRootProps(): DataAttributesgetInputProps(extra?): InputHTMLAttributes<HTMLInputElement>
Example (custom component)
import { useInputBase } from "@libdev-ui/base/hooks";
function CustomInput(props) {
const { value, focused, getRootProps, getInputProps } = useInputBase(props);
return (
<div {...getRootProps()} className="custom-input-root">
<input {...getInputProps()} className="custom-input" />
{focused && <span className="caret" />}
</div>
);
}
Accessibility:
useInputBase automatically applies aria-required, aria-readonly, and clears with Escape when clearOnEscape is set.