Skip to Content
Piplup
@piplup/rhf-adaptershtmlComponents

HtmlFormHelperTextElement

HtmlFormHelperTextElement shows a react-hook-form field’s validation error, or your hint text when the field is valid.

HtmlFormHelperTextElement renders helper content for a field and can switch naturally into error messaging based on React Hook Form state.

Import

import { HtmlFormHelperTextElement } from '@piplup/rhf-adapters/html';

Usage

"e.g. test@gmail.com"

'use client';import { HtmlFormHelperTextElement, HtmlInputElement } from '@piplup/rhf-adapters/html';import { useForm } from 'react-hook-form';export default function Page() {  const { control, handleSubmit } = useForm({    defaultValues: {      email: '',    },  });  return (    <form      onSubmit={handleSubmit((data) => {        alert(JSON.stringify(data));      })}      noValidate    >      <div>        <HtmlInputElement          messages={{            required: 'Email is required',          }}          aria-describedby="email-help"          className="border px-2 block rounded"          control={control}          name="email"          placeholder="Enter your email address"          required        />        <HtmlFormHelperTextElement          style={({ error }) => ({            ...(error && { color: 'red' }),          })}          control={control}          id="email-help"          name="email"        >          &quot;e.g. test@gmail.com&quot;        </HtmlFormHelperTextElement>      </div>      <input        className="bg-black hover:bg-black/90 px-4 py-1 text-white rounded mt-3"        type="submit"        value="Submit"      />    </form>  );}

Props

Prop

Type

HtmlFormHelperTextElement accepts paragraph props together with adapter props such as name, control, and renderOnError.

The ref is forwarded to the underlying <p> element.

useHtmlFormHelperTextAdapter

useHtmlFormHelperTextAdapter derives helper text content and state from a field so custom helper text components can react to validation without manual field-state wiring.

Import

import { useHtmlFormHelperTextAdapter } from '@piplup/rhf-adapters/html';
import * as React from 'react';import { useHtmlFormHelperTextAdapter } from './adapter';import type { UseHtmlFormHelperTextProps } from './adapter';import type { FieldPath, FieldValues } from 'react-hook-form';export interface HtmlFormHelperTextElementProps<  TFieldValues extends FieldValues = FieldValues,  TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,>  extends    Omit<React.ComponentProps<'p'>, 'style'>,    Omit<      UseHtmlFormHelperTextProps<TFieldValues, TName>,      'composeClassName' | 'composeHelperText' | 'helperText' | 'internalClasses'    > {  /**   * Render component only, if there is an error   */  renderOnError?: boolean;}function HtmlFormHelperTextComponent<  TFieldValues extends FieldValues = FieldValues,  TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,>(  props: HtmlFormHelperTextElementProps<TFieldValues, TName>,  ref?: React.Ref<HTMLParagraphElement>,): React.ReactElement {  const {    children,    classes,    className,    control,    disabled,    disableOnError,    disableOnIsSubmitting,    error,    errorParser,    name,    renderOnError,    style,    ...rest  } = props;  const { error: hasError, ...adapter } = useHtmlFormHelperTextAdapter(    {      children,      classes,      className,      composeClassName: true,      composeHelperText: true,      control,      disabled,      disableOnError,      disableOnIsSubmitting,      error,      errorParser,      name,      style,    },    ref,  );  if (renderOnError && !hasError) {    // eslint-disable-next-line react/jsx-no-useless-fragment    return <React.Fragment></React.Fragment>;  }  return <p {...rest} {...adapter} />;}export const HtmlFormHelperTextElement = React.forwardRef(  HtmlFormHelperTextComponent,) as typeof HtmlFormHelperTextComponent & { displayName?: string };if (process.env.NODE_ENV !== 'production') {  HtmlFormHelperTextElement.displayName = 'HtmlFormHelperTextElement';}

Props

Prop

Type

Return value

Prop

Type

Notes

  • The hook is built on top of useFieldStateAdapter.
  • Pair the rendered helper text with the field's aria-describedby for accessibility.

Notes

  • Use renderOnError when helper text should only appear for validation failures.
  • Pair the helper text id with an input's aria-describedby for better accessibility.

See also

On this page