1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22import Primitive from '~/core/primitives/Primitive' ;
33
4- type RadioPrimitiveProps = {
5- onClick : ( data : any ) => void ;
6- onChange : ( data : any ) => void ;
7- checked : boolean ;
4+ export type RadioPrimitiveProps = {
5+ onClick ? : ( data : any ) => void ;
6+ onChange ? : ( data : any ) => void ;
7+ checked ? : boolean ;
88 name : string ;
99 value : string ;
1010} ;
1111
1212const RadioPrimitive = ( { name = '' , value = '' , checked = false , onClick, onChange, ...props } :RadioPrimitiveProps ) => {
13+ const [ isChecked , setIsChecked ] = useState ( checked ) ;
1314 const dataAttributes = {
14- 'data-checked' : checked . toString ( )
15- } ;
16-
17- const handleOnClick = ( event : React . MouseEvent < HTMLInputElement > ) => {
18- const isChecked = event . target ?. checked ;
19- if ( typeof onClick === 'function' ) {
20- onClick ( {
21- value,
22- checked : isChecked
23- } ) ;
24- }
15+ 'data-checked' : isChecked . toString ( )
2516 } ;
2617
2718 const handleOnChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
2819 const isChecked = event . target . checked ;
29-
20+ console . log ( isChecked ) ;
3021 if ( typeof onChange === 'function' ) {
22+ setIsChecked ( isChecked ) ;
3123 onChange ( {
3224 value,
3325 checked : isChecked
@@ -36,7 +28,7 @@ const RadioPrimitive = ({ name = '', value = '', checked = false, onClick, onCha
3628 } ;
3729
3830 return (
39- < Primitive . input id = { value } type = 'radio' value = { value } name = { name } onClick = { handleOnClick } onChange = { handleOnChange } { ...props } { ...dataAttributes } />
31+ < Primitive . input id = { value } type = 'radio' value = { value } name = { name } onChange = { handleOnChange } { ...props } { ...dataAttributes } />
4032 ) ;
4133} ;
4234
0 commit comments