1- import React , { forwardRef } from "react" ;
1+ import React , { forwardRef , ReactNode } from "react" ;
22import { RadioGroup , StyledRadioItem , StyledIndicator , Label , Flex , StyledRadioButton } from "./Radio.styles" ;
33import * as RadioGroupPrimitive from "@radix-ui/react-radio-group" ;
44import { generateRandomId } from "../helper" ;
55import { PREFIX_CLS } from "./constants" ;
66
7+ interface WrapperProps {
8+ children : ReactNode ;
9+ }
10+
711export type RadioItem = {
812 label ?: string ;
913 value : string ;
1014 disabled ?: boolean ;
1115 required ?: boolean ;
16+ wrapper ?: ( { children } : WrapperProps ) => ReactNode ;
1217} ;
1318
1419type RadioButtonType = {
@@ -40,6 +45,10 @@ export type RadioProps = {
4045 id ?: string ;
4146} ;
4247
48+ const defaultWrapper = ( { children } : WrapperProps ) => {
49+ return < > { children } </ > ;
50+ } ;
51+
4352const Radio = forwardRef < HTMLInputElement , RadioProps > (
4453 ( { defaultValue, value, items, onChange, required, orientation, dir, id = generateRandomId ( ) , ...props } , ref ) => {
4554 return (
@@ -56,23 +65,33 @@ const Radio = forwardRef<HTMLInputElement, RadioProps>(
5665 ref = { ref }
5766 >
5867 { items &&
59- items . map ( ( item , i ) => (
60- < Flex dir = { dir } key = { item . value } >
61- < StyledRadioItem
62- value = { item . value }
63- disabled = { item . disabled }
64- required = { item . required }
65- { ...props . itemStyle }
66- id = { `${ id } ${ item . value } ${ i } ` }
67- className = { `${ PREFIX_CLS } ${ props . className ? props . className : "" } ` }
68- >
69- < StyledIndicator />
70- </ StyledRadioItem >
71- < Label dir = { dir } htmlFor = { `${ id } ${ item . value } ${ i } ` } >
72- { item . label }
73- </ Label >
74- </ Flex >
75- ) ) }
68+ items . map ( ( item , i ) => {
69+ const { wrapper = defaultWrapper } = item ;
70+
71+ return (
72+ < >
73+ { wrapper ( {
74+ children : (
75+ < Flex dir = { dir } key = { item . value } >
76+ < StyledRadioItem
77+ value = { item . value }
78+ disabled = { item . disabled }
79+ required = { item . required }
80+ { ...props . itemStyle }
81+ id = { `${ id } ${ item . value } ${ i } ` }
82+ className = { `${ PREFIX_CLS } ${ props . className ? props . className : "" } ` }
83+ >
84+ < StyledIndicator />
85+ </ StyledRadioItem >
86+ < Label dir = { dir } htmlFor = { `${ id } ${ item . value } ${ i } ` } >
87+ { item . label }
88+ </ Label >
89+ </ Flex >
90+ ) ,
91+ } ) }
92+ </ >
93+ ) ;
94+ } ) }
7695 { ! items && props . children }
7796 </ RadioGroup >
7897 ) ;
0 commit comments