Skip to content

Commit 6c9272b

Browse files
authored
add method updateScroll to solve the scrollbar update issue (#35)
1 parent fb75435 commit 6c9272b

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,77 @@ All the callback 'onXXXX' can accept a parameter: the ref to the scrollbar conta
6767
```
6868

6969
### Methods
70-
There are no more methods on components. You should update the scroll position by using the [containerRef](#containerref).
70+
The following method can be called by the component ref:
71+
#### updateScroll
72+
Update the scrollbar(e.g. recalculate the size) manually.
73+
In the following case, the scrollbar will not update automatically, which cause the scrollbar size incorrect.
74+
```
75+
class Container extends Component {
76+
...
77+
render() {
78+
return (
79+
<ScrollBar>
80+
...
81+
<ChildComponent />
82+
...
83+
</ScrollBar>
84+
);
85+
}
86+
}
87+
88+
class ChildComponent extends Component {
89+
handleClick = () => {
90+
this.setState({
91+
show: !this.state.show,
92+
});
93+
}
94+
95+
render () {
96+
return (
97+
<div>
98+
<button onClick={this.handleClick} />
99+
{ this.state.show ? <div /> }
100+
</div>
101+
)
102+
}
103+
}
104+
```
105+
You need to call `updateScroll` to get the correct scrollbar size:
106+
```
107+
class Container extends Component {
108+
...
109+
render() {
110+
return (
111+
<ScrollBar
112+
ref = {(ref) => { this._scrollBarRef = ref; }}
113+
>
114+
...
115+
<ChildComponent
116+
onUpdateSize = {() => { this._scrollBarRef.updateScroll(); }}
117+
/>
118+
...
119+
</ScrollBar>
120+
);
121+
}
122+
}
123+
124+
class ChildComponent extends Component {
125+
handleClick = () => {
126+
this.setState({
127+
show: !this.state.show,
128+
}, () => this.props.onUpdateSize());
129+
}
130+
131+
render () {
132+
return (
133+
<div>
134+
<button onClick={this.handleClick} />
135+
{ this.state.show ? <div /> }
136+
</div>
137+
)
138+
}
139+
}
140+
```
71141

72142
### Example
73143
A working example can be found in the `example` directory. Please run `npm run example` in browser. (Must run `npm run build` for the first time)

src/scrollbar.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export default class ScrollBar extends Component {
5151
this._ps = null;
5252
}
5353

54+
updateScroll() {
55+
this._ps.update();
56+
}
57+
5458
handleRef(ref) {
5559
this._container = ref;
5660
this.props.containerRef(ref);

0 commit comments

Comments
 (0)