gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Accessing underlying component for custom template? #162

Closed gruntruk closed 9 years ago

gruntruk commented 9 years ago

Hi, I have a form in which I'm rendering via a custom template given I'm using a custom component type from React Boostrap within that template. For example:

var options = {
  template: this.renderAccordion
}

...

renderAccordion: function() {
  return <Accordion ref="accordion" onChange={this.onPanelChange}/>
}

The accordion component is not accessible via the usual this.refs from the form's parent component. I'm trying to understand how I can obtain the underlying component instance of the tcomb form's template so that I can utilize its API.

Thanks

volkanunsal commented 9 years ago

I think that should be in the argument given to the callback function, i.e. locals.

renderAccordion: function(locals) {
  return <Accordion ref="accordion" onChange={this.onPanelChange}/>
}
gruntruk commented 9 years ago

Sorry if my question is not clear. I want to access the accordion instance from the onPanelChange callback function. So something like:

onPanelChange: function() {
   // lookup accordion instance to do equivalent of this.refs.accordion.setState({}) 
}
volkanunsal commented 9 years ago

That's tricky. I don't know how to do it exactly. But why can't you access the instance through this.refs.accordion? You may then also need to bind the function to this to make sure you have access to the refs.

renderAccordion: function(locals) {
  return <Accordion ref="accordion" onChange={this.onPanelChange.bind(this)}/>
}
onPanelChange: function(){
   // this.refs.accordion
}
volkanunsal commented 9 years ago

Ah, I think I misunderstood what you were asking again, but the solution should work. :smile:

gruntruk commented 9 years ago

The bind is redundant. Sorry for confusion. The ref is accessible just nested down within the form. So it is accessible (assuming my form is called just 'form') as:

this.refs.form.refs.input.refs.accordion

Thanks for your help!