luqin / react-icheck

:radio_button: iCheck components built with React. Highly customizable checkbox, radio buttons and radio group.
https://luqin.github.io/react-icheck
177 stars 40 forks source link

How can I know the id of the checkbox component? #36

Closed emacsuser123 closed 7 years ago

emacsuser123 commented 7 years ago

Hello.

I'm using a Checkbox component, and it is being returned in a dinamically-sized loop (items array is variable in length and content), so that it looks like the following:

this.props.items.map(function(item, i){
               return (
                 <div key={i}>
                   <Checkbox
                     checkboxClass={chkboxclass}
                     increaseArea="20%"
                     label={item.title}
                     defaultChecked
                     onChange={hdlchangefn}
                   />
                   <br/>
                 </div>
               )})

So, I need the onChange handler "hdlchangefn" to be a unique function, to be used no matter which Checkbox invoked it.

Is there any way in the hdlchangefn handler to access the id "i" of the enclosing

element? It is the only way that I have in order to know which specific checkbox changed.

Thanks a lot. -Bob V

luqin commented 7 years ago
hdlchangefn.bing(this, item, i)
emacsuser123 commented 7 years ago

Hello. Yes, thanks. Doing the bind on the onChange property when the is called, I can pass additional parameters to the callback function, as you suggested. If I pass the "i" parameter there, I get the proper value on the callback.

Right now, I have the following: <Checkbox checkboxClass={chkboxclass} increaseArea="20%" label={item.title} defaultChecked onChange={hdlchangefn.bind(this, item, i)}/> My question now is: Given the above, how can I get the "checked" value now?

If I declare the callback function as: handleChange(item, i, checked) { ... } I get checked as an 'object', not a boolean.

Thanks a lot. Bob V.

emacsuser123 commented 7 years ago

Hello. Forget about my last question.

I've already found a solution, by means of data-mode property.

Thanks.

luqin commented 7 years ago