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

Commit 077b633

Browse files
committed
add onupdate
1 parent 8acfa5c commit 077b633

File tree

6 files changed

+789
-561
lines changed

6 files changed

+789
-561
lines changed

.eslintrc

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
22
"parser": "babel-eslint",
3-
"ecmaFeatures": {
4-
"jsx": true
5-
},
63

74
"plugins": [
85
"react"

package.json

+17-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"README.md",
88
"lib/Scrollspy.js",
99
"lib/throttle.js",
10+
"lib/is-equal-array.js",
1011
"package.json",
1112
"webpack.config.babel.js",
1213
".eslintrc"
@@ -33,42 +34,42 @@
3334
"author": "makotot",
3435
"license": "MIT",
3536
"devDependencies": {
36-
"autoprefixer": "^7.1.1",
37-
"ava": "^0.19.1",
37+
"autoprefixer": "^7.1.2",
38+
"ava": "^0.22.0",
3839
"babel-cli": "^6.24.1",
3940
"babel-core": "^6.25.0",
4041
"babel-eslint": "^7.2.3",
41-
"babel-loader": "^7.0.0",
42+
"babel-loader": "^7.1.1",
4243
"babel-plugin-transform-object-assign": "^6.22.0",
4344
"babel-plugin-transform-runtime": "^6.23.0",
44-
"babel-preset-env": "^1.5.2",
45+
"babel-preset-env": "^1.6.0",
4546
"babel-preset-react": "^6.24.1",
4647
"babel-preset-react-hmre": "^1.1.1",
4748
"construct-css": "https://github.com/makotot/construct.git",
4849
"css-loader": "^0.28.4",
4950
"cssnano": "^3.10.0",
50-
"enzyme": "^2.8.2",
51-
"eslint": "^3.19.0",
52-
"eslint-loader": "^1.7.1",
53-
"eslint-plugin-react": "^7.0.1",
54-
"html-webpack-plugin": "^2.28.0",
51+
"enzyme": "^2.9.1",
52+
"eslint": "^4.4.1",
53+
"eslint-loader": "^1.9.0",
54+
"eslint-plugin-react": "^7.2.1",
55+
"html-webpack-plugin": "^2.30.1",
5556
"node-sass": "^4.5.3",
5657
"normalize.css": "^7.0.0",
57-
"postcss-loader": "^1.3.3",
58+
"postcss-loader": "^2.0.6",
5859
"react-addons-perf": "^15.4.2",
5960
"react-highlight": "^0.10.0",
60-
"react-test-renderer": "^15.5.4",
61-
"sass-loader": "^6.0.5",
62-
"style-loader": "^0.17.0",
63-
"webpack": "^2.6.1",
64-
"webpack-dev-server": "^2.4.5"
61+
"react-test-renderer": "^15.6.1",
62+
"sass-loader": "^6.0.6",
63+
"style-loader": "^0.18.2",
64+
"webpack": "^3.5.4",
65+
"webpack-dev-server": "^2.7.1"
6566
},
6667
"peerDependencies": {
6768
"react": ">=0.14.0",
6869
"react-dom": ">=0.14.0"
6970
},
7071
"dependencies": {
71-
"babel-runtime": "^6.23.0",
72+
"babel-runtime": "^6.25.0",
7273
"classnames": "^2.2.5",
7374
"prop-types": "^15.5.10"
7475
},

postcss.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

src/js/app.jsx

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class App extends React.Component {
2929
className="c-side-nav__list nav-list"
3030
style={ {fontWeight: 300} }
3131
offset={ -20 }
32+
onUpdate={
33+
() => {
34+
console.log(this)
35+
}
36+
}
3237
>
3338
<li className="c-side-nav__item"><a href="#section-1" className="c-side-nav__link">Getting Started</a></li>
3439
<li className="c-side-nav__item"><a href="#section-2" className="c-side-nav__link">Example</a></li>
@@ -136,6 +141,10 @@ class App extends React.Component {
136141
<td className="c-table__data">rootEl</td>
137142
<td className="c-table__data">Name of the element of scrollable container that can be used with <code>{'querySelector'}</code> [optional].</td>
138143
</tr>
144+
<tr>
145+
<td className="c-table__data">onUpdate</td>
146+
<td className="c-table__data">Function to be executed when the active item has been updated [optional].</td>
147+
</tr>
139148
</tbody>
140149
</table>
141150
</div>

src/js/lib/Scrollspy.jsx

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types'
22
import React from 'react'
33
import classNames from 'classnames'
44
import throttle from './throttle'
5+
import isEqualArray from './is-equal-array'
56

67
export default class Scrollspy extends React.Component {
78

@@ -14,6 +15,7 @@ export default class Scrollspy extends React.Component {
1415
componentTag: PropTypes.string,
1516
offset: PropTypes.number,
1617
rootEl: PropTypes.string,
18+
onUpdate: PropTypes.func,
1719
}
1820
}
1921

@@ -24,6 +26,7 @@ export default class Scrollspy extends React.Component {
2426
style: {},
2527
componentTag: 'ul',
2628
offset: 0,
29+
onUpdate() {},
2730
}
2831
}
2932

@@ -178,13 +181,24 @@ export default class Scrollspy extends React.Component {
178181

179182
_spy (targets) {
180183
const elemensViewState = this._getElemsViewState(targets)
184+
const currentStatuses = this.state.inViewState
181185

182186
this.setState({
183187
inViewState: elemensViewState.viewStatusList,
184188
isScrolledPast: elemensViewState.scrolledPast
189+
}, () => {
190+
this._update(currentStatuses)
185191
})
186192
}
187193

194+
_update (prevStatuses) {
195+
if (isEqualArray(this.state.inViewState, prevStatuses)) {
196+
return
197+
}
198+
199+
this.props.onUpdate()
200+
}
201+
188202
_handleSpy () {
189203
throttle(this._spy(), 100)
190204
}

0 commit comments

Comments
 (0)