@piplup/rhf-adaptershtmlHooks
useHtmlInputAdapter
Returns normalized input props for building custom HTML input components connected to React Hook Form.
useHtmlInputAdapter returns value, event handlers, and field-state-aware props for custom input components that should behave like React Hook Form-controlled fields.
Import
import { useHtmlInputAdapter } from "@piplup/rhf-adapters/html";Usage
import * as React from 'react';import { type FieldPath, type FieldValues } from 'react-hook-form';import { useHtmlInputAdapter, type UseHtmlInputAdapterProps } from './adapter';export interface HtmlInputElementProps< TTransformedValue extends number | readonly string[] | string | undefined, TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,> extends Omit< React.ComponentProps<'input'>, | 'checked' | 'defaultChecked' | 'defaultValue' | 'indeterminate' | 'name' | 'pattern' | 'style' | 'value' >, Omit< UseHtmlInputAdapterProps<TTransformedValue, TFieldValues, TName>, | 'composeClassName' | 'composeHelperText' | 'helperText' | 'internalClasses' | 'onBlur' | 'onChange' | 'type' > {}function HtmlInputComponent< TTransformedValue extends number | readonly string[] | string | undefined, TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,>( props: HtmlInputElementProps<TTransformedValue, TFieldValues, TName>, ref?: React.Ref<HTMLInputElement>,): React.ReactElement { const { checked, classes, className, control, defaultValue, disabled, disableOnError, disableOnIsSubmitting, error, errorParser, indeterminate, max, maxLength, messages, min, minLength, name, onBlur, onChange, pattern, required, rules, shouldUnregister, style, title, transform, type, value, ...rest } = props; const { error: hasError, helperText: _helperText, ...adapter } = useHtmlInputAdapter( { checked, classes, className, composeClassName: true, composeHelperText: false, control, defaultValue, disabled, disableOnError, disableOnIsSubmitting, error, errorParser, helperText: undefined, indeterminate, max, maxLength, messages, min, minLength, name, onBlur, onChange, pattern, required, rules, shouldUnregister, style, title, transform, type, value, }, ref, ); return <input aria-invalid={hasError} {...rest} {...adapter} />;}export const HtmlInputElement = React.forwardRef( HtmlInputComponent,) as typeof HtmlInputComponent & { displayName?: string;};if (process.env.NODE_ENV !== 'production') { HtmlInputElement.displayName = 'HtmlInputElement';}Props
Prop
Type
Return value
Prop
Type
Notes
- Use this hook for bespoke input components that still need React Hook Form registration and value synchronization.
- The hook is built on top of
useControllerAdapter. transform,checked, andindeterminateare useful when DOM values do not match your stored form value exactly.