Skip to content

Commit 47caa75

Browse files
committed
Configured prettier for the project
1 parent 2d9b0e3 commit 47caa75

9 files changed

+111
-112
lines changed

package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"test:basic": "run-s lint build",
3333
"test:karma": "karma start test/karma.conf.js --single-run",
3434
"test:nodom": "mocha test/no-dom-test.js",
35-
"postinstall": "node test/setup-pre-commit-hook"
35+
"precommit": "npm test && lint-staged"
3636
},
3737
"devDependencies": {
3838
"babel-cli": "^6.24.1",
@@ -42,15 +42,18 @@
4242
"babel-preset-es2015": "^6.24.1",
4343
"chai": "^3.5.0",
4444
"eslint": "^3.4.0",
45+
"husky": "^0.13.3",
4546
"karma": "^1.4.0",
4647
"karma-chai": "^0.1.0",
4748
"karma-mocha": "^1.3.0",
4849
"karma-phantomjs-launcher": "^1.0.0",
4950
"karma-spec-reporter": "0.0.26",
5051
"karma-webpack": "^2.0.2",
52+
"lint-staged": "^3.4.2",
5153
"mocha": "^3.2.0",
5254
"npm-run-all": "^4.0.2",
5355
"phantomjs-prebuilt": "^2.1.7",
56+
"prettier": "^1.3.1",
5457
"react": "^15.5.x",
5558
"react-dom": "^15.5.x",
5659
"react-test-renderer": "^15.5.x",
@@ -69,5 +72,12 @@
6972
"plugins": [
7073
"transform-class-properties"
7174
]
75+
},
76+
"lint-staged": {
77+
"{src,test}/**/*.js": [
78+
"prettier --print-width=120 --single-quote --trailing-comma=all --write",
79+
"eslint --fix",
80+
"git add"
81+
]
7282
}
7383
}

src/generateOutsideCheck.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function findHighest(current, componentNode, ignoreClass) {
3131
// a layered approach, too, but that requires going back to
3232
// thinking in terms of Dom node nesting, running counter
3333
// to React's 'you shouldn't care about the DOM' philosophy.
34-
while(current.parentNode) {
34+
while (current.parentNode) {
3535
if (isNodeFound(current, componentNode, ignoreClass)) {
3636
return true;
3737
}
@@ -51,7 +51,14 @@ function clickedScrollbar(evt) {
5151
* Generate the event handler that checks whether a clicked DOM node
5252
* is inside of, or lives outside of, our Component's node tree.
5353
*/
54-
export default function generateOutsideCheck(componentNode, eventHandler, ignoreClass, excludeScrollbar, preventDefault, stopPropagation) {
54+
export default function generateOutsideCheck(
55+
componentNode,
56+
eventHandler,
57+
ignoreClass,
58+
excludeScrollbar,
59+
preventDefault,
60+
stopPropagation,
61+
) {
5562
return function(evt) {
5663
if (preventDefault) {
5764
evt.preventDefault();
@@ -60,7 +67,7 @@ export default function generateOutsideCheck(componentNode, eventHandler, ignore
6067
evt.stopPropagation();
6168
}
6269
const current = evt.target;
63-
if((excludeScrollbar && clickedScrollbar(evt)) || (findHighest(current, componentNode, ignoreClass) !== document)) {
70+
if ((excludeScrollbar && clickedScrollbar(evt)) || findHighest(current, componentNode, ignoreClass) !== document) {
6471
return;
6572
}
6673
eventHandler(evt);

src/index.js

+28-24
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ const handlers = [];
1717
*/
1818
export default function onClickOutsideHOC(WrappedComponent, config) {
1919
return class onClickOutside extends Component {
20-
static displayName = `OnClickOutside(${ WrappedComponent.displayName || WrappedComponent.name || 'Component' })`
20+
static displayName = `OnClickOutside(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
2121

2222
static defaultProps = {
2323
eventTypes: ['mousedown', 'touchstart'],
2424
excludeScrollbar: (config && config.excludeScrollbar) || false,
2525
outsideClickIgnoreClass: 'ignore-react-onclickoutside',
2626
preventDefault: false,
2727
stopPropagation: false,
28-
}
28+
};
2929

30-
static getClass = () => WrappedComponent.getClass ? WrappedComponent.getClass() : WrappedComponent
30+
static getClass = () => (WrappedComponent.getClass ? WrappedComponent.getClass() : WrappedComponent);
3131

3232
/**
3333
* Access the WrappedComponent's instance.
@@ -41,7 +41,7 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
4141
}
4242

4343
// this is given meaning in componentDidMount/componentDidUpdate
44-
__outsideClickHandler = null
44+
__outsideClickHandler = null;
4545

4646
/**
4747
* Add click listeners to the current document,
@@ -51,27 +51,31 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
5151
// If we are in an environment without a DOM such
5252
// as shallow rendering or snapshots then we exit
5353
// early to prevent any unhandled errors being thrown.
54-
if (typeof document === 'undefined' || !document.createElement){
54+
if (typeof document === 'undefined' || !document.createElement) {
5555
return;
5656
}
5757

5858
const instance = this.getInstance();
5959

60-
if(config && typeof config.handleClickOutside === 'function') {
60+
if (config && typeof config.handleClickOutside === 'function') {
6161
this.__clickOutsideHandlerProp = config.handleClickOutside(instance);
62-
if(typeof this.__clickOutsideHandlerProp !== 'function') {
63-
throw new Error('WrappedComponent lacks a function for processing outside click events specified by the handleClickOutside config option.');
62+
if (typeof this.__clickOutsideHandlerProp !== 'function') {
63+
throw new Error(
64+
'WrappedComponent lacks a function for processing outside click events specified by the handleClickOutside config option.',
65+
);
6466
}
65-
} else if(typeof instance.handleClickOutside === 'function') {
67+
} else if (typeof instance.handleClickOutside === 'function') {
6668
if (Component.prototype.isPrototypeOf(instance)) {
6769
this.__clickOutsideHandlerProp = instance.handleClickOutside.bind(instance);
6870
} else {
6971
this.__clickOutsideHandlerProp = instance.handleClickOutside;
7072
}
71-
} else if(typeof instance.props.handleClickOutside === 'function') {
73+
} else if (typeof instance.props.handleClickOutside === 'function') {
7274
this.__clickOutsideHandlerProp = instance.props.handleClickOutside;
7375
} else {
74-
throw new Error('WrappedComponent lacks a handleClickOutside(event) function for processing outside click events.');
76+
throw new Error(
77+
'WrappedComponent lacks a handleClickOutside(event) function for processing outside click events.',
78+
);
7579
}
7680

7781
// TODO: try to get rid of this, could be done with function ref, might be problematic for SFC though, they do not expose refs
@@ -132,7 +136,7 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
132136
document.addEventListener(eventName, fn, handlerOptions);
133137
});
134138
}
135-
}
139+
};
136140

137141
/**
138142
* Can be called to explicitly disable event listening
@@ -147,17 +151,17 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
147151
}
148152
events.forEach(eventName => document.removeEventListener(eventName, fn));
149153
}
150-
}
154+
};
151155

152156
addOutsideClickHandler() {
153-
const fn = this.__outsideClickHandler = generateOutsideCheck(
157+
const fn = (this.__outsideClickHandler = generateOutsideCheck(
154158
findDOMNode(this.getInstance()),
155159
this.__clickOutsideHandlerProp,
156160
this.props.outsideClickIgnoreClass,
157161
this.props.excludeScrollbar,
158162
this.props.preventDefault,
159-
this.props.stopPropagation
160-
);
163+
this.props.stopPropagation,
164+
));
161165

162166
const pos = registeredComponents.length;
163167
registeredComponents.push(this);
@@ -178,23 +182,23 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
178182

179183
if (pos > -1) {
180184
// clean up so we don't leak memory
181-
if (handlers[pos]) { handlers.splice(pos, 1); }
185+
if (handlers[pos]) {
186+
handlers.splice(pos, 1);
187+
}
182188
registeredComponents.splice(pos, 1);
183189
}
184190
}
185191

186-
getRef = ref => this.instanceRef = ref
192+
getRef = ref => (this.instanceRef = ref);
187193

188194
/**
189195
* Pass-through render
190196
*/
191197
render() {
192-
var props = Object.keys(this.props)
193-
.filter(prop => prop !== 'excludeScrollbar')
194-
.reduce((props, prop) => {
195-
props[prop] = this.props[prop];
196-
return props;
197-
}, {});
198+
var props = Object.keys(this.props).filter(prop => prop !== 'excludeScrollbar').reduce((props, prop) => {
199+
props[prop] = this.props[prop];
200+
return props;
201+
}, {});
198202

199203
if (WrappedComponent.prototype.isReactComponent) {
200204
props.ref = this.getRef;

test/browser/test1.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
(function test1(onClickOutside) {
2-
32
onClickOutside = onClickOutside.default;
43

54
/**
@@ -9,7 +8,7 @@
98
constructor(props) {
109
super(props);
1110
this.state = {
12-
highlight: false
11+
highlight: false,
1312
};
1413
}
1514

@@ -24,11 +23,11 @@
2423
}
2524

2625
render() {
27-
var className = 'concentric' + (this.state.highlight? ' highlight' : '');
26+
var className = 'concentric' + (this.state.highlight ? ' highlight' : '');
2827
return React.createElement('div', {
2928
className: className,
3029
children: this.props.children,
31-
onClick: e => this.highlight(e)
30+
onClick: e => this.highlight(e),
3231
});
3332
}
3433
}
@@ -45,11 +44,11 @@
4544
children: React.createElement(Nested, {
4645
id: 3,
4746
stopPropagation: true,
48-
children: React.createElement('div', { className: 'label', children: ['test'] })
49-
})
50-
})
47+
children: React.createElement('div', { className: 'label', children: ['test'] }),
48+
}),
49+
}),
5150
});
5251
};
5352

5453
ReactDOM.render(React.createElement(App), document.getElementById('app1'));
55-
}(onClickOutside)); /* global onClickOutside */
54+
})(onClickOutside); /* global onClickOutside */

test/browser/test2.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
(function test2(onClickOutside) {
2-
32
onClickOutside = onClickOutside.default;
43

54
class BasePopup extends React.Component {
@@ -20,21 +19,19 @@
2019
constructor(props) {
2120
super(props);
2221
this.state = {
23-
hideToolbox: true
22+
hideToolbox: true,
2423
};
2524
}
2625
render() {
27-
return (
28-
React.createElement('div', {
29-
children: [
30-
React.createElement('button', {
31-
onClick: e => this.state.hideToolbox && this.show(e),
32-
children: 'show text'
33-
}),
34-
this.state.hideToolbox ? null : React.createElement(Popup, {hide: e => this.hide(e)})
35-
]
36-
})
37-
);
26+
return React.createElement('div', {
27+
children: [
28+
React.createElement('button', {
29+
onClick: e => this.state.hideToolbox && this.show(e),
30+
children: 'show text',
31+
}),
32+
this.state.hideToolbox ? null : React.createElement(Popup, { hide: e => this.hide(e) }),
33+
],
34+
});
3835
}
3936
show() {
4037
console.log('test2 - show');
@@ -47,5 +44,4 @@
4744
}
4845

4946
ReactDOM.render(React.createElement(App), document.getElementById('app2'));
50-
51-
}(onClickOutside)); /* global onClickOutside */
47+
})(onClickOutside); /* global onClickOutside */

test/karma.conf.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
module.exports = function(config) {
22
config.set({
3-
4-
files: [
5-
'test.js'
6-
],
3+
files: ['test.js'],
74

85
frameworks: ['mocha', 'chai'],
96

107
preprocessors: {
11-
'test.js': ['webpack']
8+
'test.js': ['webpack'],
129
},
1310

1411
reporters: ['spec'],
1512

1613
webpack: {
1714
devtool: 'inline-source-map',
1815
module: {
19-
loaders: [{
20-
test: /.js$/,
21-
loader: 'babel',
22-
query: {
23-
presets: [['es2015', { 'loose': true }]],
24-
plugins: ['transform-class-properties']
25-
}
26-
}]
27-
}
16+
loaders: [
17+
{
18+
test: /.js$/,
19+
loader: 'babel',
20+
query: {
21+
presets: [['es2015', { loose: true }]],
22+
plugins: ['transform-class-properties'],
23+
},
24+
},
25+
],
26+
},
2827
},
2928

3029
webpackMiddleware: {
3130
noInfo: true,
32-
stats: { errorDetails: true }
31+
stats: { errorDetails: true },
3332
},
3433

3534
plugins: [
@@ -40,7 +39,6 @@ module.exports = function(config) {
4039
require('karma-spec-reporter'),
4140
],
4241

43-
browsers: ['PhantomJS']
44-
42+
browsers: ['PhantomJS'],
4543
});
4644
};

test/no-dom-test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ var renderer = require('react-test-renderer');
44
var requireHijack = require('require-hijack');
55

66
describe('onclickoutside hoc with no DOM', function() {
7-
87
class Component extends React.Component {
9-
108
handleClickOutside() {}
119

1210
render() {
@@ -19,7 +17,7 @@ describe('onclickoutside hoc with no DOM', function() {
1917
it('should not throw an error if rendered in an environment with no DOM', function() {
2018
// Needed until React 15.4 lands due to https://github.com/facebook/react/issues/7386.
2119
requireHijack.replace('react-dom').with({
22-
render: function(){}
20+
render: function() {},
2321
});
2422

2523
// Must import this after we mock out ReactDOM to prevent the inject error.
@@ -31,5 +29,4 @@ describe('onclickoutside hoc with no DOM', function() {
3129
var renderInstance = renderer.create(element);
3230
assert(renderInstance.toJSON().type === 'div', 'wrapped component renders a div');
3331
});
34-
3532
});

test/setup-pre-commit-hook.js

-4
This file was deleted.

0 commit comments

Comments
 (0)