Skip to content

Commit 95a3ff3

Browse files
author
Fabien JUIF
authored
#39 / Custom name for load function in props (#40)
1 parent ac5e92e commit 95a3ff3

File tree

7 files changed

+4168
-34
lines changed

7 files changed

+4168
-34
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default loader(Component, { wait: false })
4242
```
4343
In this example, the loader component doesn't wait for props. `this.props.load` is called once, but the `LoadingIndicator` component isn't displayed.
4444

45-
### Load as a parameter
45+
### Load as a function parameter
4646
```es6
4747
import loader from 'hoc-react-loader'
4848

@@ -54,5 +54,17 @@ In this case, the loader calls `this.props.load` if it exists *AND* the `load` p
5454

5555
The default `wait` parameter value is `false`. It means that in this example the `LoadingIndicator` isn't displayed.
5656

57+
### Load as a string parameter
58+
```es6
59+
import loader from 'hoc-react-loader'
60+
61+
const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>
62+
63+
export default loader(Component, { load: 'myLoader' })
64+
```
65+
In this case, the loader calls `this.props.myLoader` if it exists.
66+
67+
The default `wait` parameter value is `false`. It means that in this example the `LoadingIndicator` isn't displayed.
68+
5769
### Wait as a function
5870
The `wait` parameter can also be a function. Then the `context` and `props` are given to it, and it should return the array of props to wait for.

build/core.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,45 @@ var _react2 = _interopRequireDefault(_react);
1414

1515
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1616

17+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18+
1719
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1820

1921
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
2022

2123
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
2224

25+
var getTypeOf = function getTypeOf(something) {
26+
var getType = {};
27+
return something && getType.toString.call(something);
28+
};
29+
2330
// http://stackoverflow.com/a/7356528
2431
var isFunction = function isFunction(functionToCheck) {
25-
var getType = {};
26-
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
32+
var type = getTypeOf(functionToCheck);
33+
return type && type === '[object Function]';
34+
};
35+
36+
var isString = function isString(stringToCheck) {
37+
var type = getTypeOf(stringToCheck);
38+
return type && type === '[object String]';
2739
};
40+
2841
var getDisplayName = function getDisplayName(c) {
2942
return c.displayName || c.name || 'Component';
3043
};
3144

3245
exports.default = function (ComposedComponent) {
3346
var _class, _temp2;
3447

35-
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
48+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
49+
LoadingIndicator = _ref.LoadingIndicator,
50+
_ref$wait = _ref.wait,
51+
wait = _ref$wait === undefined ? ['loaded'] : _ref$wait,
52+
_ref$load = _ref.load,
53+
load = _ref$load === undefined ? undefined : _ref$load;
3654

37-
var LoadingIndicator = _ref.LoadingIndicator;
38-
var _ref$wait = _ref.wait;
39-
var wait = _ref$wait === undefined ? ['loaded'] : _ref$wait;
40-
var _ref$load = _ref.load;
41-
var load = _ref$load === undefined ? undefined : _ref$load;
55+
var loadFunctionName = isString(load) ? load : 'load';
4256

4357
return _temp2 = _class = function (_Component) {
4458
_inherits(_class, _Component);
@@ -75,13 +89,11 @@ exports.default = function (ComposedComponent) {
7589
// Anything else
7690
return !wait;
7791
}, _this.omitLoadInProps = function (props) {
78-
var isLoadAFunction = isFunction(props.load);
92+
var isLoadAFunction = isFunction(props[loadFunctionName]);
7993

8094
if (isLoadAFunction) {
8195
_this.setState({
82-
props: _extends({}, props, {
83-
load: undefined
84-
})
96+
props: _extends({}, props, _defineProperty({}, loadFunctionName, undefined))
8597
});
8698
} else {
8799
_this.setState({ props: props });
@@ -96,15 +108,15 @@ exports.default = function (ComposedComponent) {
96108
_createClass(_class, [{
97109
key: 'componentWillMount',
98110
value: function componentWillMount() {
99-
// Load from props
100-
if (this.omitLoadInProps(this.props)) {
101-
this.props.load();
102-
}
103-
104111
// Load from hoc argument
105112
if (isFunction(load)) {
106113
load();
107114
}
115+
116+
// Load from props
117+
if (this.omitLoadInProps(this.props)) {
118+
this.props[loadFunctionName]();
119+
}
108120
}
109121
}, {
110122
key: 'render',

build/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1919
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
2020

2121
exports.default = function (ComposedComponent) {
22-
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
22+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2323

24-
var _ref$LoadingIndicator = _ref.LoadingIndicator;
25-
var LoadingIndicator = _ref$LoadingIndicator === undefined ? _TailSpin2.default : _ref$LoadingIndicator;
26-
27-
var rest = _objectWithoutProperties(_ref, ['LoadingIndicator']);
24+
var _ref$LoadingIndicator = _ref.LoadingIndicator,
25+
LoadingIndicator = _ref$LoadingIndicator === undefined ? _TailSpin2.default : _ref$LoadingIndicator,
26+
rest = _objectWithoutProperties(_ref, ['LoadingIndicator']);
2827

2928
return (0, _core2.default)(ComposedComponent, _extends({}, rest, { LoadingIndicator: LoadingIndicator }));
3029
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hoc-react-loader",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "Higher order component to call a load function from props at mount.",
55
"main": "build/index.js",
66
"peerDependencies": {
@@ -50,7 +50,8 @@
5050
},
5151
"author": "Fabien JUIF <[email protected]>",
5252
"contributors": [
53-
"Yvonnick FRIN <[email protected]>"
53+
"Yvonnick FRIN <[email protected]>",
54+
"Benjamin CAVY <[email protected]>"
5455
],
5556
"license": "MIT",
5657
"keywords": [

src/core.jsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import React, { Component, PropTypes } from 'react'
22

3+
const getTypeOf = (something) => {
4+
const getType = {}
5+
return something && getType.toString.call(something)
6+
}
7+
38
// http://stackoverflow.com/a/7356528
49
const isFunction = (functionToCheck) => {
5-
const getType = {}
6-
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'
10+
const type = getTypeOf(functionToCheck)
11+
return type && type === '[object Function]'
712
}
13+
14+
const isString = (stringToCheck) => {
15+
const type = getTypeOf(stringToCheck)
16+
return type && type === '[object String]'
17+
}
18+
819
const getDisplayName = (c) => c.displayName || c.name || 'Component'
920

1021
export default (
@@ -15,6 +26,8 @@ export default (
1526
load = undefined,
1627
} = {},
1728
) => {
29+
const loadFunctionName = isString(load) ? load : 'load'
30+
1831
return class extends Component {
1932
static displayName = `Loader(${getDisplayName(ComposedComponent)})`
2033
static propTypes = {
@@ -44,13 +57,13 @@ export default (
4457
}
4558

4659
omitLoadInProps = (props) => {
47-
const isLoadAFunction = isFunction(props.load)
60+
const isLoadAFunction = isFunction(props[loadFunctionName])
4861

4962
if (isLoadAFunction) {
5063
this.setState({
5164
props: {
5265
...props,
53-
load: undefined,
66+
[loadFunctionName]: undefined,
5467
},
5568
})
5669
} else {
@@ -61,15 +74,15 @@ export default (
6174
}
6275

6376
componentWillMount() {
64-
// Load from props
65-
if (this.omitLoadInProps(this.props)) {
66-
this.props.load()
67-
}
68-
6977
// Load from hoc argument
7078
if (isFunction(load)) {
7179
load()
7280
}
81+
82+
// Load from props
83+
if (this.omitLoadInProps(this.props)) {
84+
this.props[loadFunctionName]()
85+
}
7386
}
7487

7588
componentWillReceiveProps = (nextProps) => {

src/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ describe('react-loader', () => {
162162
loaded.find(TailSpin).node.should.exists
163163
})
164164

165+
it('should call matching props if the load parameter is a string', () => {
166+
// Mount
167+
const loadProp = spy(() => {})
168+
const loadPropName = 'customLoadFunction'
169+
const loaded = getWrapped({ load: loadPropName }, { [loadPropName]: loadProp })
170+
171+
// Load function is called
172+
// Graphic component isn't called
173+
// Loader should be Dots
174+
loadProp.should.have.been.called.once
175+
expect(loaded.find(Component).node).to.be.undefined
176+
loaded.find(TailSpin).node.should.exists
177+
})
178+
165179
it('should handle a `null` `load` props/parameter', () => {
166180
// Mount
167181
const loaded = getWrapped()

0 commit comments

Comments
 (0)