복합 구성과 그룹 지원이 있는 스타일 없고 접근 가능한 라디오 버튼 컴포넌트예요.
출처: 문서
본문
레이아웃과 스타일링을 완전히 제어하며 완전히 커스텀한 라디오 버튼 컨트롤을 만들어 보세요.
기능 (Features)
- 세 개의 하위 컴포넌트를 갖춘 복합 컴포넌트 API:
Root, Box, Indicator
- 여러 라디오 버튼에 걸쳐 공유 값을 관리하는
RadioButtonGroup
match prop을 사용한 조건부 인디케이터 렌더링
사용법 (Usage)
import { RadioButton } from 'primereact/radiobutton';
import { RadioButtonGroup } from 'primereact/radiobuttongroup';
<RadioButtonGroup>
<RadioButton.Root value="a">
<RadioButton.Box>
<RadioButton.Indicator />
</RadioButton.Box>
</RadioButton.Root>
<RadioButton.Root value="b">
<RadioButton.Box>
<RadioButton.Indicator />
</RadioButton.Box>
</RadioButton.Root>
</RadioButtonGroup>
동작 (Behavior)
다형성 렌더링 (Polymorphic Rendering)
어느 하위 컴포넌트에서든 as를 사용해 렌더링되는 HTML 요소를 바꿀 수 있어요.
<RadioButton.Root as="label">
<RadioButton.Box as="span">
<RadioButton.Indicator as="div" />
</RadioButton.Box>
</RadioButton.Root>
기본 요소: Root=div, Box=div, Indicator=span.
렌더 함수 children (Render Function Children)
RadioButton.Box와 RadioButton.Indicator는 children으로 렌더 함수를 받아 컴포넌트 인스턴스에 접근할 수 있게 해줘요.
<RadioButton.Box>{(instance) => <span>{instance.radiobutton?.state.checked ? '●' : '○'}</span>}</RadioButton.Box>
패스 스루 (Pass Through)
<DocPTViewer name="radiobutton-pt" components={['RadioButtonRoot']} />
API
| Attribute |
Value |
data-scope |
"radiobutton" |
data-part |
"root" |
data-checked |
Present when checked |
data-unchecked |
Present when unchecked |
data-disabled |
Present when disabled |
| Label |
Type |
Description |
| root |
RadioButtonRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| input |
RadioButtonRootPassThroughType<InputHTMLAttributes> |
Used to pass attributes to the input's DOM element. |
| box |
RadioButtonRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the box's DOM element. |
| indicator |
RadioButtonRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the indicator's DOM element. |
| Attribute |
Value |
data-scope |
"radiobutton" |
data-part |
"box" |
data-checked |
Present when checked |
data-unchecked |
Present when unchecked |
data-disabled |
Present when disabled |
| Label |
Type |
Description |
| root |
RadioButtonBoxPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"radiobutton" |
data-part |
"indicator" |
data-checked |
Present when checked |
data-unchecked |
Present when unchecked |
data-disabled |
Present when disabled |
| Label |
Type |
Description |
| root |
RadioButtonIndicatorPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-scope |
"radiobuttongroup" |
data-part |
"root" |
| Label |
Type |
Description |
| root |
RadioButtonGroupPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
RadioButton 컴포넌트는 내부적으로 스크린 리더에만 보이는 숨겨진 네이티브 라디오 버튼 요소를 사용해요. 컴포넌트를 설명하는 값은 inputId prop과 결합된 label 태그, 또는 ariaLabelledby, ariaLabel props로 제공할 수 있어요.
<label htmlFor="rb1">One</label>
<RadioButton.Root inputId="rb1">
<RadioButton.Box>
<RadioButton.Indicator />
</RadioButton.Box>
</RadioButton.Root>
<span id="rb2">Two</span>
<RadioButton.Root ariaLabelledby="rb2">
<RadioButton.Box>
<RadioButton.Indicator />
</RadioButton.Box>
</RadioButton.Root>
<RadioButton.Root ariaLabel="Three">
<RadioButton.Box>
<RadioButton.Indicator />
</RadioButton.Box>
</RadioButton.Root>
키보드 지원 (Keyboard Support)
| Key |
Function |
tab |
Moves focus to the selected radio button in a group, or first one. |
space |
Selects the focused radio button. |
arrow |
In a named radio group, moves focus between options and updates choice. |
더 알아보기 (Learn more)