Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 0a78cad

Browse files
committed
Adding getRef={...} prop. Fixes #555
1 parent 40af581 commit 0a78cad

File tree

7 files changed

+64
-0
lines changed

7 files changed

+64
-0
lines changed

src/components/control-component.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const propTypes = {
101101
dispatch: PropTypes.func,
102102
getState: PropTypes.func,
103103
}),
104+
getRef: PropTypes.func,
104105
};
105106

106107
const defaultStrategy = {
@@ -564,10 +565,15 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
564565
controlProps = emptyControlProps,
565566
component,
566567
control,
568+
getRef,
567569
} = this.props;
568570

569571
const mappedProps = omit(this.getMappedProps(), disallowedProps);
570572

573+
if (getRef) {
574+
mappedProps.ref = getRef;
575+
}
576+
571577
// If there is an existing control, clone it
572578
if (control) {
573579
return cloneElement(
@@ -585,6 +591,8 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
585591
}
586592
}
587593

594+
Control.displayName = 'Control';
595+
588596
Control.propTypes = propTypes;
589597

590598
Control.defaultProps = {

src/components/field-component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const fieldPropTypes = {
5757
componentMap: PropTypes.object,
5858
dynamic: PropTypes.bool,
5959
dispatch: PropTypes.func,
60+
getRef: PropTypes.func,
6061

6162
// Calculated props
6263
fieldValue: PropTypes.object,

src/components/form-component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const propTypes = {
4242
}),
4343
onUpdate: PropTypes.func,
4444
onChange: PropTypes.func,
45+
getRef: PropTypes.func,
4546
};
4647

4748
const defaultStrategy = {
@@ -118,6 +119,7 @@ function createFormClass(s = defaultStrategy) {
118119
this._node = node;
119120

120121
this._node.submit = this.handleSubmit;
122+
if (this.props.getRef) this.props.getRef(node);
121123
}
122124

123125
validate(nextProps, initial = false) {

src/components/local-form-component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class LocalForm extends React.Component {
2222
}
2323
}
2424

25+
LocalForm.displayName = 'LocalForm';
26+
2527
LocalForm.propTypes = {
2628
store: PropTypes.shape({
2729
subscribe: PropTypes.func,

src/utils/resolve-model.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export default function wrapWithModelResolver(WrappedComponent) {
4141
}
4242
}
4343

44+
ResolvedModelWrapper.displayName = `Modeled(${WrappedComponent.displayName})`;
45+
4446
ResolvedModelWrapper.propTypes = {
4547
model: PropTypes.any,
4648
};

test/field-component-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,7 @@ Object.keys(testContexts).forEach((testKey) => {
18551855
const filter = ({ constructor }) =>
18561856
constructor.displayName === 'Connect(Control)';
18571857
const components = TestUtils.findAllInRenderedTree(field, filter);
1858+
console.log(components);
18581859
assert.lengthOf(components, 1, 'exactly one connected Control was rendered');
18591860
const [component] = components;
18601861
const oldStateProps = component.stateProps;

test/ref-spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint react/no-multi-comp:0 react/jsx-no-bind:0 */
2+
import { assert } from 'chai';
3+
import React from 'react';
4+
import TestUtils from 'react-addons-test-utils';
5+
6+
import { testCreateStore, testRender } from './utils';
7+
8+
import {
9+
modelReducer,
10+
formReducer,
11+
Control,
12+
Form,
13+
LocalForm,
14+
} from '../src';
15+
16+
describe('getRef()', () => {
17+
const components = [
18+
[Control, 'input'],
19+
[Form, 'form'],
20+
[LocalForm, 'form'],
21+
];
22+
23+
const store = testCreateStore({
24+
test: modelReducer('test', {}),
25+
testForm: formReducer('test', {}),
26+
});
27+
28+
components.forEach(([RRFComponent, tag]) => {
29+
it(`should retrieve a component instance from ${RRFComponent.displayName}`, () => {
30+
let instance;
31+
32+
function getRef(node) {
33+
instance = node;
34+
}
35+
36+
const control = testRender(
37+
<RRFComponent
38+
model="test"
39+
getRef={getRef}
40+
/>, store);
41+
42+
const input = TestUtils.findRenderedDOMComponentWithTag(
43+
control, tag);
44+
45+
assert.equal(instance, input);
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)