본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

useFormControlForwardedProps

원문 보기 위키 갱신

FormControl 컴포넌트의 접근성 기능을 제공하는 훅이에요. TextInput, Radio, Select 같은 기본 제공 폼 컨트롤을 쓰지 않고 커스텀 폼 컨트롤을 만들 때 사용해요. 커스텀 입력 요소에 이 훅이 반환한 props를 펼쳐 넣으면 aria-describedby 같은 속성이 자동으로 적용돼요.

출처: 문서

본문

FormControl 컴포넌트의 접근성 기능을 제공해요. 이 훅은 기본 제공되는 폼 컨트롤 컴포넌트(TextInput, Radio, Select 등)를 사용하지 않는 접근 가능한 폼 컨트롤을 만들 때 유용해요.

예제

커스텀 입력을 만들 때 이 훅을 쓰면, Primer 입력이 아님에도 aria-describedby와 비슷한 속성이 자동으로 적용돼요.

import React from 'react'
import {FormControl, useFormControlForwardedProps} from '@primer/react'

const CustomInput = (externalProps: {type: string}) => {
  const props = useFormControlForwardedProps(externalProps)
  return <input {...props} />
}

export default function Default() {
  return (
    <FormControl disabled={false} required={false}>
      <FormControl.Label>Custom input</FormControl.Label>
      <CustomInput type="text" />
      <FormControl.Caption>
        This is not a Primer input, but it still has `aria-describedby` and similar attributes applied automatically
      </FormControl.Caption>
    </FormControl>
  )
}

API

useFormControlForwardedProps의 전체 API는 원문 페이지의 "API" 섹션에서 확인할 수 있어요.

관련 링크

  • FormControl

더 알아보기 (Learn more)

  • 커스텀 폼 컨트롤을 만들 때 FormControl과 함께 이 훅을 써서 라벨·캡션·에러 등 접근성 연결을 자동으로 받아요.
  • 외부 props를 그대로 받아 훅을 통과시킨 뒤 <input {...props} />로 펼치면 됩니다.
  • 기본 제공 폼 컨트롤(TextInput, Select 등)을 쓴다면 이 훅이 따로 필요 없어요.