--- title: Radio description: "`Radio`は、ユーザーが複数の選択肢の中から1つの値を選択するために使用されるコンポーネントです。" links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/radio - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-radio--basic --- ```tsx 織田信長 豊臣秀吉 徳川家康 ``` ## 使い方 ```tsx import { Radio, RadioGroup } from "@yamada-ui/react" ``` ```tsx import { Radio, RadioGroup } from "@/components/ui" ``` ```tsx import { Radio, RadioGroup } from "@workspaces/ui" ``` ```tsx ``` ### itemsを使う ```tsx const items = useMemo( () => [ { label: "織田信長", value: "1" }, { label: "豊臣秀吉", value: "2" }, { label: "徳川家康", value: "3" }, ], [], ) return ``` ### バリアントを変更する ```tsx const items = useMemo( () => [ { label: "Checked", value: "1" }, { label: "No checked", value: "2" }, ], [], ) return ( {(variant, index) => ( )} ) ``` ### サイズを変更する ```tsx const items = useMemo( () => [ { label: "Checked", value: "1" }, { label: "No checked", value: "2" }, ], [], ) return ( {(size, index) => ( )} ) ``` ### デフォルト値を設定する デフォルト値を設定する場合は、`defaultValue`に値を設定します。 ```tsx const items = useMemo( () => [ { label: "織田信長", value: "1" }, { label: "豊臣秀吉", value: "2" }, { label: "徳川家康", value: "3" }, ], [], ) return ``` ### デフォルトで選択済みにする デフォルトで選択済みにする場合は、`defaultChecked`を`true`に設定します。 ```tsx const items = useMemo( () => [ { defaultChecked: true, label: "織田信長", value: "1" }, { label: "豊臣秀吉", value: "2" }, { label: "徳川家康", value: "3" }, ], [], ) return ``` ### 方向を変更する 方向を変更する場合は、`orientation`に`"horizontal"`または`"vertical"`を設定します。デフォルトは`"vertical"`です。 ```tsx const items = useMemo( () => [ { label: "織田信長", value: "1" }, { label: "豊臣秀吉", value: "2" }, { label: "徳川家康", value: "3" }, ], [], ) return ``` ### 形を変更する 形を変更する場合は、`shape`に`"circle"`や`"rounded"`などを設定します。デフォルトは`"circle"`です。 ```tsx const items = useMemo( () => [ { label: "織田信長", value: "1" }, { label: "豊臣秀吉", value: "2" }, { label: "徳川家康", value: "3" }, ], [], ) return ( {(shape) => ( )} ) ``` ### 無効にする 無効にする場合は、`disabled`を`true`に設定します。 ```tsx {(variant) => ( {toTitleCase(variant)} )} ``` ### 読み取り専用にする 読み取り専用にする場合は、`readOnly`を`true`に設定します。 ```tsx {(variant) => ( {toTitleCase(variant)} )} ``` ### 無効な入力にする 無効な入力にする場合は、`invalid`を`true`に設定します。 ```tsx {(variant) => ( {toTitleCase(variant)} )} ``` ### ボーダーの色を変更する ボーダーの色を変更する場合は、`focusBorderColor`または`errorBorderColor`に値を設定します。 ```tsx Default border color Custom border color Custom border color ``` ### カスタムコンポーネントを使う ```tsx const { getInputProps, getLabelProps, getRootProps } = useRadioGroup({ defaultValue: "1", }) const CustomRadio = ({ children, ...rest }) => { return ( {children} ) } return ( 織田信長 豊臣秀吉 徳川家康 ) ``` ### 制御する ```tsx const [value, setValue] = useState("1") const items = useMemo( () => [ { label: "織田信長", value: "1" }, { label: "豊臣秀吉", value: "2" }, { label: "徳川家康", value: "3" }, ], [], ) return ``` ## Props ### Radio | Prop | Default | Type | Description | | ------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"solid"` | `"outline" \| "solid" \| "subtle" \| "surface"` | The variant of the component. | | `checked` | - | `boolean` | If `true`, the radio will be checked. | | `defaultChecked` | `false` | `boolean` | If `true`, the radio will be initially checked. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is focused. | | `id` | - | `string` | id assigned to input. | | `indicatorProps` | - | `RadioIndicatorProps` | Props for the indicator component. | | `inputProps` | - | `HTMLStyledProps<"input">` | Props for the input element. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `labelProps` | - | `RadioLabelProps` | Props for the label component. | | `name` | - | `string` | The name of the input field in a radio. | | `onBlur` | - | `FocusEventHandler` | The callback invoked when the radio is blurred. | | `onChange` | - | `ChangeEventHandler` | The callback invoked when the checked state changes. | | `onFocus` | - | `FocusEventHandler` | The callback invoked when the radio is focused. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `rootProps` | - | `HTMLStyledProps<"label">` | Props for the label element. | | `shape` | `"circle"` | `"circle" \| "rounded" \| "square"` | The shape of the component | | `value` | - | `Y` | The value of the radio. | ### RadioGroup.Root | Prop | Default | Type | Description | | ------------------ | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"solid"` | `"outline" \| "solid" \| "subtle" \| "surface"` | The variant of the component. | | `align` | - | `"-moz-initial" \| "baseline" \| "center" \| "end" \| "flex-end" \| "flex-start" \| "inherit" \| "initial" \| "normal" \| "revert-layer" ...` | The CSS `align-items` property. | | `attached` | `false` | `boolean` | If `true`, the borderRadius of button that are direct children will be altered to look flushed together. | | `basis` | - | `"-moz-fit-content" \| "-moz-initial" \| "-moz-max-content" \| "-moz-min-content" \| "-webkit-auto" \| "0.5" \| "1.5" \| "1" \| "1/12" \| "1/2" ...` | The CSS `flex-basis` property. | | `defaultValue` | - | `Y` | The initial value of the radio group. | | `direction` | - | `"-moz-initial" \| "column-reverse" \| "column" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "row-reverse" \| "row" \| "unset" ...` | The CSS `flex-direction` property. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is focused. | | `grow` | `false` | `boolean` | If `true`, the children will grow to fill the available space. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `items` | `[]` | `RadioItem[]` | If provided, generate options based on items. | | `justify` | - | `"-moz-initial" \| "center" \| "end" \| "flex-end" \| "flex-start" \| "inherit" \| "initial" \| "left" \| "normal" \| "revert-layer" ...` | The CSS `justify-content` property. | | `onChange` | - | `(value: Y) => void` | The callback fired when any children radio is checked or unchecked. | | `orientation` | `"horizontal"` | `"horizontal" \| "vertical"` | The orientation of the group. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `shape` | `"circle"` | `"circle" \| "rounded" \| "square"` | The shape of the component | | `shrink` | - | `"-moz-initial" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "unset" \| number & {} ...` | The CSS `flex-shrink` property. | | `stacking` | - | `"first-on-top" \| "last-on-top"` | The stacking order of the group. | | `value` | - | `Y` | The value of the radio group. | | `wrap` | - | `"-moz-initial" \| "inherit" \| "initial" \| "nowrap" \| "revert-layer" \| "revert" \| "unset" \| "wrap-reverse" \| "wrap" ...` | The CSS `flex-wrap` property. | ### RadioGroup.Item | Prop | Default | Type | Description | | ------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"lg" \| "md" \| "sm"` | The size of the component. | | `variant` | `"solid"` | `"outline" \| "solid" \| "subtle" \| "surface"` | The variant of the component. | | `checked` | - | `boolean` | If `true`, the radio will be checked. | | `defaultChecked` | `false` | `boolean` | If `true`, the radio will be initially checked. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is focused. | | `id` | - | `string` | id assigned to input. | | `indicatorProps` | - | `RadioIndicatorProps` | Props for the indicator component. | | `inputProps` | - | `HTMLStyledProps<"input">` | Props for the input element. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `labelProps` | - | `RadioLabelProps` | Props for the label component. | | `name` | - | `string` | The name of the input field in a radio. | | `onBlur` | - | `FocusEventHandler` | The callback invoked when the radio is blurred. | | `onChange` | - | `ChangeEventHandler` | The callback invoked when the checked state changes. | | `onFocus` | - | `FocusEventHandler` | The callback invoked when the radio is focused. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `rootProps` | - | `HTMLStyledProps<"label">` | Props for the label element. | | `shape` | `"circle"` | `"circle" \| "rounded" \| "square"` | The shape of the component | | `value` | - | `Y` | The value of the radio. | ## アクセシビリティ 現在、v2の移行に伴い、このセクションは更新中です。