Skip to Content
Piplup
@piplup/rhf-adaptershtmlComponents

HtmlFormLabelElement

HtmlFormLabelElement reads a react-hook-form field’s error and required state, so labels can style themselves without extra wiring.

HtmlFormLabelElement wraps the native <label> element and exposes field-state-aware styling without requiring manual state plumbing.

Import

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

Usage

"e.g. test@gmail.co"

'use client';import {  HtmlFormHelperTextElement,  HtmlFormLabelElement,  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>        <HtmlFormLabelElement          style={({ error }) => ({            ...(error && { color: 'red' }),          })}          className="block"          control={control}          htmlFor="email"          id="email-label"          name="email"        >          Email        </HtmlFormLabelElement>        <HtmlInputElement          messages={{            required: 'Email is required',          }}          aria-describedby="email-help"          aria-labelledby="email-label"          className="border px-2 block rounded"          control={control}          name="email"          placeholder="Enter your email address"          required        />        <HtmlFormHelperTextElement control={control} id="email-help" name="email">          &quot;e.g. test@gmail.co&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

HtmlFormLabelElement accepts standard label attributes together with adapter props such as name, control, and custom state-driven styling.

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

useHtmlFormLabelAdapter

useHtmlFormLabelAdapter exposes label props and field-state-aware styling inputs for custom label components that should respond to validation and disabled state.

Import

import { useHtmlFormLabelAdapter } from '@piplup/rhf-adapters/html';
import * as React from 'react';import { useHtmlFormLabelAdapter } from './adapter';import type { UseHtmlFormLabelProps } from './adapter';import type { FieldPath, FieldValues } from 'react-hook-form';export interface HtmlFormLabelElementProps<  TFieldValues extends FieldValues = FieldValues,  TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,>  extends    Omit<React.ComponentProps<'label'>, 'style'>,    Omit<      UseHtmlFormLabelProps<TFieldValues, TName>,      'composeClassName' | 'composeHelperText' | 'helperText' | 'internalClasses'    > {}function HtmlFormLabelComponent<  TFieldValues extends FieldValues = FieldValues,  TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,>(  props: HtmlFormLabelElementProps<TFieldValues, TName>,  ref?: React.Ref<HTMLLabelElement>,): React.ReactElement {  const {    className,    disabled,    ref: adapterRef,    style,  } = useHtmlFormLabelAdapter(    {      ...props,      composeClassName: true,      composeHelperText: false,    },    ref,  );  return (    <label      aria-disabled={disabled}      {...props}      className={className}      htmlFor={props.htmlFor}      ref={adapterRef}      style={style}    />  );}export const HtmlFormLabelElement = React.forwardRef(  HtmlFormLabelComponent,) as typeof HtmlFormLabelComponent & { displayName?: string };if (process.env.NODE_ENV !== 'production') {  HtmlFormLabelElement.displayName = 'HtmlFormLabelElement';}

Props

Prop

Type

Return value

Prop

Type

Notes

  • Use this hook when you need custom label markup but still want field-state-aware styling.
  • The hook is built on top of useFieldStateAdapter.
  • Pair the rendered label with the target control via htmlFor or aria-labelledby.

Notes

  • Use htmlFor to associate the label with an input id.
  • The adapter exposes error, disabled, and helper-text-aware styling composition through field state.

See also

On this page