Skip to content

Commit 2b24911

Browse files
authored
feat: expose inner element when using HOC (#399)
Use `innerRef={(c) => { this.myInput = c; }}` on your HOC to access the internal element
1 parent 15493dc commit 2b24911

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

API.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- [validate](#validate)
3838
- [formNoValidate](#formnovalidate)
3939
- [Formsy.HOC](#formsyhoc)
40+
- [innerRef](#innerRef)
4041
- [Formsy.Decorator](#formsydecorator)
4142
- [Formsy.addValidationRule](#formsyaddvalidationrule)
4243
- [Validators](#validators)
@@ -566,7 +567,7 @@ The same methods as the mixin are exposed to the HOC version of the element comp
566567
```jsx
567568
import {HOC} from 'formsy-react';
568569

569-
class MyInput extends React.Component {
570+
class MyInputHoc extends React.Component {
570571
render() {
571572
return (
572573
<div>
@@ -575,9 +576,30 @@ class MyInput extends React.Component {
575576
);
576577
}
577578
};
578-
export default HOC(MyInput);
579+
export default HOC(MyInputHoc);
579580
```
580581

582+
#### <a name="innerRef">innerRef</a>
583+
584+
Use an `innerRef` prop to get a reference to your DOM node.
585+
586+
```jsx
587+
var MyForm = React.createClass({
588+
componentDidMount() {
589+
this.searchInput.focus()
590+
},
591+
render: function () {
592+
return (
593+
<Formsy.Form>
594+
<MyInputHoc name="search" innerRef={(c) => { this.searchInput = c; }} />
595+
</Formsy.Form>
596+
);
597+
}
598+
})
599+
```
600+
601+
Sets a class name on the form itself.
602+
581603
### <a name="formsydecorator">Formsy.Decorator</a>
582604
The same methods as the mixin are exposed to the decorator version of the element component, though through the `props`, not on the instance.
583605
```jsx

src/HOC.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ module.exports = function (Component) {
44
return React.createClass({
55
displayName: 'Formsy(' + getDisplayName(Component) + ')',
66
mixins: [Mixin],
7+
78
render: function () {
8-
return React.createElement(Component, {
9+
const { innerRef } = this.props;
10+
const propsForElement = {
911
setValidations: this.setValidations,
1012
setValue: this.setValue,
1113
resetValue: this.resetValue,
@@ -22,7 +24,12 @@ module.exports = function (Component) {
2224
showError: this.showError,
2325
isValidValue: this.isValidValue,
2426
...this.props
25-
});
27+
};
28+
29+
if (innerRef) {
30+
propsForElement.ref = innerRef;
31+
}
32+
return React.createElement(Component, propsForElement);
2633
}
2734
});
2835
};

tests/Formsy-spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@ import TestUtils from 'react-addons-test-utils';
44

55
import Formsy from './..';
66
import TestInput from './utils/TestInput';
7+
import TestInputHoc from './utils/TestInputHoc';
78
import immediate from './utils/immediate';
89
import sinon from 'sinon';
910

1011
export default {
1112

1213
'Setting up a form': {
14+
'should expose the users DOM node through an innerRef prop': function (test) {
15+
const TestForm = React.createClass({
16+
render() {
17+
return (
18+
<Formsy.Form>
19+
<TestInputHoc name="name" innerRef={(c) => { this.name = c; }} />
20+
</Formsy.Form>
21+
);
22+
}
23+
});
24+
25+
const form = TestUtils.renderIntoDocument(<TestForm/>);
26+
const input = form.name;
27+
test.equal(input.methodOnWrappedInstance('foo'), 'foo');
28+
29+
test.done();
30+
},
1331

1432
'should render a form into the document': function (test) {
1533

tests/utils/TestInputHoc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { HOC as formsyHoc } from './../..';
3+
4+
const defaultProps = {
5+
methodOnWrappedInstance(param) {
6+
return param;
7+
},
8+
render() {
9+
return (<input />);
10+
},
11+
};
12+
13+
export default formsyHoc(React.createClass(defaultProps));

0 commit comments

Comments
 (0)