Skip to content

Commit e1e0b1f

Browse files
authored
Merge pull request formio#327 from j-perl/improvement/custom-component-ref-callback
pass down a ref callback to custom component instead of relying on the r…
2 parents 5aba2e8 + 4897981 commit e1e0b1f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/components/ReactComponent.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class ReactComponent extends Field {
1111
*/
1212
constructor(component, options, data) {
1313
super(component, options, data);
14+
this.reactInstance = null;
1415
}
1516

1617
/**
@@ -41,6 +42,15 @@ export default class ReactComponent extends Field {
4142
return super.render(`<div ref="react-${this.id}"></div>`);
4243
}
4344

45+
/**
46+
* Callback ref to store a reference to the node.
47+
*
48+
* @param element - the node
49+
*/
50+
setReactInstance(element) {
51+
this.reactInstance = element;
52+
}
53+
4454
/**
4555
* The third phase of component building where the component has been attached to the DOM as 'element' and is ready
4656
* to have its javascript events attached.
@@ -58,7 +68,7 @@ export default class ReactComponent extends Field {
5868
});
5969

6070
if (this.refs[`react-${this.id}`]) {
61-
this.reactInstance = this.attachReact(this.refs[`react-${this.id}`]);
71+
this.attachReact(this.refs[`react-${this.id}`], this.setReactInstance.bind(this));
6272
if (this.shouldSetValue) {
6373
this.setValue(this.dataForSetting);
6474
this.updateValue(this.dataForSetting);
@@ -82,8 +92,9 @@ export default class ReactComponent extends Field {
8292
* Override this function to insert your custom component.
8393
*
8494
* @param element
95+
* @param ref - callback ref
8596
*/
86-
attachReact(element) {
97+
attachReact(element, ref) {
8798
return;
8899
}
89100

0 commit comments

Comments
 (0)