Skip to content

Commit 9ce9b2c

Browse files
committed
Fixed scroll to bottom.
Note: works only for llaid out content
1 parent 64e7c7d commit 9ce9b2c

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "git",
88
"url": "https://github.com/wix/react-native-keyboard-aware-scrollview.git"
99
},
10-
"version": "0.0.4",
10+
"version": "0.0.5",
1111
"description": "React Native Keyboard Aware ScrollView - auto adjust content ScrollView",
1212
"nativePackage": true,
1313
"bugs": {

src/KeyboardAwareBase.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var ScrollViewManager = NativeModules.ScrollViewManager;
88
export default class KeyboardAwareBase extends React.Component {
99
constructor(props) {
1010
super(props);
11-
this._bind('_onKeyboardWillShow', '_onKeyboardWillHide', '_addKeyboardEventListeners', '_removeKeyboardListeners', '_scrollToFocusedTextInput', '_onKeyboardAwareViewLayout', 'scrollToBottom');
11+
this._bind('_onKeyboardWillShow', '_onKeyboardWillHide', '_addKeyboardEventListeners', '_removeKeyboardListeners', '_scrollToFocusedTextInput', '_onKeyboardAwareViewLayout', 'scrollToBottom', 'scrollBottomOnNextSizeChange');
1212
this.state = {keyboardHeight: 0};
1313
}
1414

@@ -31,13 +31,26 @@ export default class KeyboardAwareBase extends React.Component {
3131

3232
componentWillMount() {
3333
this._addKeyboardEventListeners();
34-
3534
}
3635

3736
_onKeyboardAwareViewLayout(layout) {
3837
this._keyboardAwareView.layout = layout;
38+
this._keyboardAwareView.contentOffset = {x: 0, y: 0};
39+
this._updateKeyboardAwareViewContentSize();
40+
}
41+
42+
_onKeyboardAwareViewScroll(contentOffset) {
43+
this._keyboardAwareView.contentOffset = contentOffset;
44+
this._updateKeyboardAwareViewContentSize();
45+
}
46+
47+
_updateKeyboardAwareViewContentSize() {
3948
ScrollViewManager.getContentSize(React.findNodeHandle(this._keyboardAwareView), (res)=> {
4049
this._keyboardAwareView.contentSize = res;
50+
if(this.state.scrollBottomOnNextSizeChange) {
51+
this.scrollToBottom();
52+
this.state.scrollBottomOnNextSizeChange = false;
53+
}
4154
})
4255
}
4356

@@ -67,16 +80,23 @@ export default class KeyboardAwareBase extends React.Component {
6780

6881
this.setState({keyboardHeight: newKeyboardHeight});
6982
}
70-
83+
7184
_onKeyboardWillHide(event) {
85+
const keyboardHeight = this.state.keyboardHeight;
7286
this.setState({keyboardHeight: 0});
73-
if(this._keyboardAwareView) {
74-
this._keyboardAwareView.scrollTo({x: 0, y: 0, animated: true});
75-
}
87+
88+
const yOffset = Math.max(this._keyboardAwareView.contentOffset.y - keyboardHeight, 0);
89+
this._keyboardAwareView.scrollTo({x: 0, y: yOffset, animated: true});
90+
}
91+
92+
scrollBottomOnNextSizeChange() {
93+
this.state.scrollBottomOnNextSizeChange = true;
7694
}
7795

7896
scrollToBottom() {
79-
const bottomYOffset = this._keyboardAwareView.contentSize.height - this._keyboardAwareView.layout.height + this._keyboardAwareView.props.contentInset.bottom;
80-
this._keyboardAwareView.scrollTo({x: 0, y: bottomYOffset, animated: true});
97+
if (this._keyboardAwareView) {
98+
const bottomYOffset = this._keyboardAwareView.contentSize.height - this._keyboardAwareView.layout.height + this._keyboardAwareView.props.contentInset.bottom;
99+
this._keyboardAwareView.scrollTo({x: 0, y: bottomYOffset, animated: true});
100+
}
81101
}
82102
}

src/KeyboardAwareListView.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ export default class KeyboardAwareListView extends KeyboardAwareBase {
1515
onLayout={(layoutEvent) => {
1616
this._onKeyboardAwareViewLayout(layoutEvent.nativeEvent.layout);
1717
}}
18+
onScroll={(event) => {
19+
this._onKeyboardAwareViewScroll(event.nativeEvent.contentOffset)
20+
}}
21+
onContentSizeChange={() => {
22+
this._updateKeyboardAwareViewContentSize();
23+
}}
24+
scrollEventThrottle={200}
1825
/>
1926
);
2027
}
21-
}
28+
}

src/KeyboardAwareScrollView.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export default class KeyboardAwareScrollView extends KeyboardAwareBase {
1616
onLayout={(layoutEvent) => {
1717
this._onKeyboardAwareViewLayout(layoutEvent.nativeEvent.layout);
1818
}}
19+
onScroll={(event) => {
20+
this._onKeyboardAwareViewScroll(event.nativeEvent.contentOffset)
21+
}}
22+
onContentSizeChange={() => {
23+
this._updateKeyboardAwareViewContentSize();
24+
}}
25+
scrollEventThrottle={200}
1926
/>
2027
);
2128
}

0 commit comments

Comments
 (0)