사용자가 여러 옵션 중 하나만 선택할 수 있게 하는 라디오 그룹 컴포넌트입니다.
@seed-design/lynx-react@0.1.0, @seed-design/lynx-css@0.1.0
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup
defaultValue = "apple"
label = "좋아하는 과일"
description = "좋아하는 과일을 선택해 주세요."
indicator = "선택"
>
< RadioGroupItem value = "apple" label = "Apple" tone = "neutral" size = "large" />
< RadioGroupItem value = "banana" label = "Banana" tone = "neutral" size = "large" />
< RadioGroupItem value = "orange" label = "Orange" tone = "neutral" size = "large" />
</ RadioGroup >
</ VStack >
);
}
npx @seed-design/cli@latest add ui:radio-group pnpm dlx @seed-design/cli@latest add ui:radio-group yarn dlx @seed-design/cli@latest add ui:radio-group bun x @seed-design/cli@latest add ui:radio-group
의존성 설치
npm install @seed-design/lynx-react yarn add @seed-design/lynx-react pnpm add @seed-design/lynx-react bun add @seed-design/lynx-react 아래 코드를 복사 후 붙여넣고 사용하세요 /**
* @file ui:radio-group
* @requires @seed-design/lynx-react@^1.0.0
* @requires @seed-design/lynx-css@^1.0.0
**/
import * as React from "@lynx-js/react" ;
import {
RadioGroup as SeedRadioGroup,
RadioGroupField as SeedRadioGroupField,
} from "@seed-design/lynx-react" ;
export interface RadioGroupProps extends SeedRadioGroupField . RootProps {
label ?: React . ReactNode ;
/**
* @default " medium "
*/
labelWeight ?: SeedRadioGroupField . LabelProps [ "weight" ];
indicator ?: React . ReactNode ;
showRequiredIndicator ?: boolean ;
description ?: React . ReactNode ;
errorMessage ?: React . ReactNode ;
}
/**
* @see https://seed-design.io/lynx/components/radio-group
*/
export const RadioGroup = React. forwardRef < unknown , RadioGroupProps >(
(
{
label,
labelWeight,
indicator,
showRequiredIndicator,
description,
errorMessage,
children,
"accessibility-label" : accessibilityLabel,
... props
},
ref,
) => {
const renderHeader = label != null || indicator != null ;
const renderErrorMessage = props.invalid && errorMessage != null ;
const renderDescription = description != null && ! renderErrorMessage;
const renderFooter = renderDescription || renderErrorMessage;
const defaultAccessibilityLabel = typeof label === "string" ? label : undefined ;
return (
< SeedRadioGroupField.Root
ref = {ref}
accessibility-label = {accessibilityLabel ?? defaultAccessibilityLabel}
{ ... props}
>
{renderHeader ? (
< SeedRadioGroupField.Header >
< SeedRadioGroupField.Label weight = {labelWeight}>
{label}
{showRequiredIndicator ? < SeedRadioGroupField.RequiredIndicator /> : null }
{indicator != null ? (
< SeedRadioGroupField.IndicatorText >{indicator}</ SeedRadioGroupField.IndicatorText >
) : null }
</ SeedRadioGroupField.Label >
</ SeedRadioGroupField.Header >
) : null }
< SeedRadioGroup.Root >{children}</ SeedRadioGroup.Root >
{renderFooter ? (
< SeedRadioGroupField.Footer >
{renderDescription ? (
< SeedRadioGroupField.Description >{description}</ SeedRadioGroupField.Description >
) : null }
{renderErrorMessage ? (
< SeedRadioGroupField.ErrorMessage >{errorMessage}</ SeedRadioGroupField.ErrorMessage >
) : null }
</ SeedRadioGroupField.Footer >
) : null }
</ SeedRadioGroupField.Root >
);
},
);
RadioGroup.displayName = "RadioGroup" ;
export interface RadioGroupItemProps extends SeedRadioGroup . ItemProps {
label ?: React . ReactNode ;
}
/**
* @see https://seed-design.io/lynx/components/radio-group
*/
export const RadioGroupItem = React. forwardRef < unknown , RadioGroupItemProps >(
({ label , children , "accessibility-label" : accessibilityLabel , ... otherProps }, ref ) => {
const defaultAccessibilityLabel = typeof label === "string" ? label : undefined ;
return (
< SeedRadioGroup.Item
ref = {ref}
accessibility-label = {accessibilityLabel ?? defaultAccessibilityLabel}
{ ... otherProps}
>
< SeedRadioGroup.ItemControl >
< SeedRadioGroup.ItemIndicator />
</ SeedRadioGroup.ItemControl >
{label != null ? < SeedRadioGroup.ItemLabel >{label}</ SeedRadioGroup.ItemLabel > : null }
{children}
</ SeedRadioGroup.Item >
);
},
);
RadioGroupItem.displayName = "RadioGroupItem" ;
export interface RadiomarkProps extends Omit < SeedRadioGroup . ItemControlProps , "children" > {}
/**
* @see https://seed-design.io/lynx/components/radio-group
*/
export const Radiomark = React. forwardRef < unknown , RadiomarkProps >(( props , ref ) => {
return (
< SeedRadioGroup.ItemControl ref = {ref} { ... props}>
< SeedRadioGroup.ItemIndicator />
</ SeedRadioGroup.ItemControl >
);
});
Radiomark.displayName = "Radiomark" ;
/**
* This file is a snippet from SEED Design, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export function ContactMethod () {
return (
< RadioGroup
label = "선호하는 연락 방법"
indicator = "필수"
description = "연락받을 방법을 선택해 주세요."
defaultValue = "chat"
>
< RadioGroupItem value = "chat" label = "채팅" />
< RadioGroupItem value = "phone" label = "전화" />
</ RadioGroup >
);
}
invalid가 true이면 description 대신 errorMessage를 표시합니다. weight, size, tone은 RadioGroupItem에 지정하며, 이전 버전에서 그룹에 지정한 값도 각 항목으로 옮겨야 합니다. Registry RadioGroup은 React와 같이 required와 readOnly를 받지 않습니다.
value?string | undefined controlled 선택 값입니다. 지정하면 Item 선택은 onValueChange만 호출합니다.
Type
string | undefined
defaultValue?string | undefined label?React.ReactNode
labelWeight?"bold" | "medium" | undefined
Type
"bold" | "medium" | undefined
Default
"medium"
indicator?React.ReactNode
showRequiredIndicator?boolean | undefined
description?React.ReactNode
errorMessage?React.ReactNode
onValueChange?(( value : string ) => void ) | undefined
disabled?boolean | undefined true이면 모든 Item의 press·사용자 tap handler·선택 변경이 막히고
Root accessibility-traits 기본값이 "disabled"가 됩니다.
Type
boolean | undefined
Default
false
invalid?boolean | undefined 선택 값이 유효하지 않은 상태입니다. Lynx에는 대응하는 접근성 속성이 없어
native 속성으로 만들지 않고 context로만 제공합니다.
Type
boolean | undefined
Default
false
label?React.ReactNode
valuestring
disabled?boolean | undefined true이면 이 Item의 press, 사용자 tap handler, 선택 변경이 막힙니다. Root가 disabled여도 막힙니다.
Type
boolean | undefined
Default
false
weight?"bold" | "regular" | undefined
Type
"bold" | "regular" | undefined
Default
"regular"
size?"large" | "medium" | undefined
Type
"large" | "medium" | undefined
Default
"medium"
tone?"brand" | "neutral" | undefined
Type
"brand" | "neutral" | undefined
Default
"brand"
tone?"brand" | "neutral" | undefined
Type
"brand" | "neutral" | undefined
Default
"brand"
size?"large" | "medium" | undefined
Type
"large" | "medium" | undefined
Default
"medium"
RadioGroup의 문자열 label은 그룹의 접근성 이름으로도 사용됩니다. RadioGroupItem의 문자열 label은 항목 이름으로 연결되며 선택 상태와 비활성 상태도 전달됩니다.
raw <text>를 사용하거나 항목에 텍스트가 없으면 accessibility-label을 지정하세요. raw <text>의 정적 텍스트는 접근성 이름을 만들 수 없습니다.
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" gap = "x5" >
< RadioGroup accessibility-label = "과일 선택" defaultValue = "apple" >
< RadioGroupItem value = "apple" label = "사과" size = "medium" tone = "neutral" />
< RadioGroupItem value = "banana" label = "바나나" size = "medium" tone = "neutral" />
< RadioGroupItem value = "orange" label = "오렌지" size = "medium" tone = "neutral" />
</ RadioGroup >
< RadioGroup accessibility-label = "색상 선택" defaultValue = "red" >
< RadioGroupItem value = "red" label = "빨간색" size = "large" tone = "neutral" />
< RadioGroupItem value = "blue" label = "파란색" size = "large" tone = "neutral" />
< RadioGroupItem value = "green" label = "초록색" size = "large" tone = "neutral" />
</ RadioGroup >
</ VStack >
);
}
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup accessibility-label = "과일 선택" defaultValue = "apple" >
< RadioGroupItem value = "apple" label = "사과" tone = "brand" size = "large" />
< RadioGroupItem value = "banana" label = "바나나" tone = "brand" size = "large" />
< RadioGroupItem value = "orange" label = "오렌지" tone = "brand" size = "large" />
</ RadioGroup >
</ VStack >
);
}
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup accessibility-label = "과일 선택" defaultValue = "apple" >
< RadioGroupItem value = "apple" label = "사과" tone = "neutral" size = "large" />
< RadioGroupItem value = "banana" label = "바나나" tone = "neutral" size = "large" />
< RadioGroupItem value = "orange" label = "오렌지" tone = "neutral" size = "large" />
</ RadioGroup >
</ VStack >
);
}
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup accessibility-label = "글꼴 굵기 선택" defaultValue = "regular" >
< RadioGroupItem
value = "regular"
label = "Regular"
weight = "regular"
tone = "neutral"
size = "large"
/>
< RadioGroupItem value = "bold" label = "Bold" weight = "bold" tone = "neutral" size = "large" />
</ RadioGroup >
</ VStack >
);
}
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
const longLabel =
"Consequat ut veniam aliqua deserunt occaecat enim occaecat veniam et et cillum nulla officia incididunt incididunt. Sint laboris labore occaecat fugiat culpa voluptate ullamco in elit dolore exercitation nulla." ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup accessibility-label = "Long label options" defaultValue = "medium" >
< RadioGroupItem value = "medium" label = {longLabel} size = "medium" tone = "neutral" />
< RadioGroupItem value = "large" label = {longLabel} size = "large" tone = "neutral" />
</ RadioGroup >
</ VStack >
);
}
import "./styles" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup accessibility-label = "Options with disabled" defaultValue = "option1" >
< RadioGroupItem value = "option1" label = "Active option" tone = "neutral" size = "large" />
< RadioGroupItem
value = "option2"
label = "Disabled option"
tone = "neutral"
size = "large"
disabled
/>
< RadioGroupItem value = "option3" label = "Another active option" tone = "neutral" size = "large" />
</ RadioGroup >
</ VStack >
);
}
RadioGroup의 onValueChange로 선택 값 변경을 감지할 수 있습니다. Controlled 모드에서는 value와 onValueChange를 함께 사용합니다.
import "./styles" ;
import { useState } from "@lynx-js/react" ;
import { VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
const [ count , setCount ] = useState ( 0 );
const [ lastValue , setLastValue ] = useState < string | null >( null );
return (
< VStack className = "radio-group-preview" gap = "x4" >
< RadioGroup
accessibility-label = "Fruit selection"
defaultValue = "apple"
onValueChange = {( value ) => {
setCount (( previous ) => previous + 1 );
setLastValue (value);
}}
>
< RadioGroupItem value = "apple" label = "Apple" tone = "neutral" size = "large" />
< RadioGroupItem value = "banana" label = "Banana" tone = "neutral" size = "large" />
< RadioGroupItem value = "orange" label = "Orange" tone = "neutral" size = "large" />
</ RadioGroup >
< text className = "radio-group-preview__status" >
onValueChange called: {count} times, last value: {lastValue ?? "-" }
</ text >
</ VStack >
);
}
Radiomark는 RadioGroupItem 안에서만 사용할 수 있습니다.
import "./styles" ;
import { HStack, RadioGroup as RadioGroupPrimitive, VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, Radiomark } from "@/components/ui/radio-group" ;
export default function Example () {
return (
< VStack className = "radio-group-preview" >
< RadioGroup accessibility-label = "Weight selection" defaultValue = "medium" >
< HStack gap = "x6" >
< RadioGroupPrimitive.Item
accessibility-label = "regular"
value = "regular"
size = "large"
tone = "neutral"
>
< VStack gap = "x2" align = "center" >
< Radiomark tone = "neutral" size = "large" />
< RadioGroupPrimitive.ItemLabel >regular</ RadioGroupPrimitive.ItemLabel >
</ VStack >
</ RadioGroupPrimitive.Item >
< RadioGroupPrimitive.Item
accessibility-label = "medium"
value = "medium"
size = "large"
tone = "neutral"
>
< VStack gap = "x2" align = "center" >
< Radiomark tone = "neutral" size = "large" />
< RadioGroupPrimitive.ItemLabel >medium</ RadioGroupPrimitive.ItemLabel >
</ VStack >
</ RadioGroupPrimitive.Item >
< RadioGroupPrimitive.Item
accessibility-label = "bold"
value = "bold"
size = "large"
tone = "neutral"
>
< VStack gap = "x2" align = "center" >
< Radiomark tone = "neutral" size = "large" />
< RadioGroupPrimitive.ItemLabel >bold</ RadioGroupPrimitive.ItemLabel >
</ VStack >
</ RadioGroupPrimitive.Item >
</ HStack >
</ RadioGroup >
</ VStack >
);
}
import "./styles" ;
import { useState } from "@lynx-js/react" ;
import { ActionButton, HStack, VStack } from "@seed-design/lynx-react" ;
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" ;
export default function Example () {
const [ contact , setContact ] = useState ( "email" );
const [ firstErrorMessage , setFirstErrorMessage ] = useState < string | undefined >();
const [ option , setOption ] = useState ( "option1" );
const [ secondErrorMessage , setSecondErrorMessage ] = useState < string | undefined >();
const handleFirstSubmit = () => {
setFirstErrorMessage (contact === "email" ? "이메일은 선택할 수 없습니다." : undefined );
};
const handleSecondSubmit = () => {
setSecondErrorMessage (option === "option1" ? "옵션 1은 선택할 수 없습니다." : undefined );
};
return (
< VStack className = "radio-group-preview" maxWidth = "640px" >
< HStack width = "full" gap = "x8" align = "flex-start" wrap = "wrap" justify = "center" >
< VStack
grow
shrink
minWidth = "240px"
style = {{ flexBasis: 0 }}
gap = "spacingY.componentDefault"
>
< RadioGroup
label = "선호하는 연락 방법"
indicator = "필수"
description = "이메일을 선택하고 제출해보세요."
value = {contact}
onValueChange = {setContact}
invalid = {firstErrorMessage != null }
errorMessage = {firstErrorMessage}
>
< RadioGroupItem value = "email" label = "이메일" tone = "neutral" size = "large" />
< RadioGroupItem value = "phone" label = "전화" tone = "neutral" size = "large" />
< RadioGroupItem value = "sms" label = "문자" tone = "neutral" size = "large" />
</ RadioGroup >
< ActionButton variant = "neutralSolid" bindtap = {handleFirstSubmit}>
제출
</ ActionButton >
</ VStack >
< VStack
grow
shrink
minWidth = "240px"
style = {{ flexBasis: 0 }}
gap = "spacingY.componentDefault"
>
< RadioGroup
label = "필수 선택"
labelWeight = "bold"
showRequiredIndicator
description = "옵션 1을 선택하고 제출해보세요."
value = {option}
onValueChange = {setOption}
invalid = {secondErrorMessage != null }
errorMessage = {secondErrorMessage}
>
< RadioGroupItem value = "option1" label = "옵션 1" tone = "neutral" size = "large" />
< RadioGroupItem value = "option2" label = "옵션 2" tone = "neutral" size = "large" disabled />
< RadioGroupItem value = "option3" label = "옵션 3" tone = "neutral" size = "large" />
</ RadioGroup >
< ActionButton variant = "neutralSolid" bindtap = {handleSecondSubmit}>
제출
</ ActionButton >
</ VStack >
</ HStack >
</ VStack >
);
}
Lynx RadioGroupItem은 누르는 동안 Radiomark를 줄입니다. 콘텐츠를 축소하는 SEED 컴포넌트 안에서는 이 효과를 생략합니다. React의 --seed-radiomark-feedback-scale CSS 변수는 제공하지 않습니다.
invalid 상태에서는 description을 제외하고 errorMessage만 화면과 접근성 트리에 표시합니다.
사용자 지정 아이콘은 Lynx 단색 아이콘 컴포넌트를 사용해야 합니다. raw <svg> 아이콘은 지원하지 않습니다.
값 변경은 onChange가 아닌 onValueChange(value: string)로 받습니다.
HTML <label>과 <input type="radio"> 대신 네이티브 <view>와 <text>를 렌더링합니다.
RadioGroupItem은 RadioGroup 안에서, Radiomark는 해당 항목 안에서 사용해야 합니다.
사용자 지정 아이콘 패키지는 Registry 기본 설치에 포함되지 않습니다.
bun add @karrotmarket/lynx-monochrome-icon@^1.20.0
현재 Lynx 플랫폼 제약으로 다음 기능이 지원되지 않습니다.
기능 웹 대응 설명 React Hook Form HTML form 라이브러리 value와 onValueChange 및 앱 수준 제출 로직으로 대체RadioGroupItemHiddenInput<input type="radio">Lynx에 HTML form 제출 모델 없음 namenative form field name Lynx에 native form 제출 모델 없음 focus / focusVisible키보드 포커스 Lynx에 키보드 포커스 개념 없음 onChange (raw DOM event)React.ChangeEvent의미 없음. onValueChange로 대체