react-native-gifted-form icon indicating copy to clipboard operation
react-native-gifted-form copied to clipboard

Handling keyboard popup with ScrollView?

Open aksonov opened this issue 9 years ago • 21 comments

Sometime form could be large and text input could be invisible when keyboard is shown. How to fix it? I saw fixes like here: http://stackoverflow.com/questions/29313244/how-to-auto-slide-the-window-out-from-behind-keyboard-when-textinput-has-focus

It would be nice to have it as built-in functionality...

aksonov avatar Feb 15 '16 16:02 aksonov

I implemented such feature https://github.com/FaridSafi/react-native-gifted-form/blob/master/mixins/ContainerMixin.js#L43 If it's not working, then we have a new bug

FaridSafi avatar Feb 15 '16 16:02 FaridSafi

I guess i should use GiftedForm as container (root) view this way. But my widgets are not direct successors (because of layout, some of them should be grouped inside another view), so they will not be recognized, right? Now I have two GiftedForms with the same name - one for elements inside one view and another for submit button (which is placed at different place).

aksonov avatar Feb 15 '16 17:02 aksonov

Yes you can use as root. You can put all kinds of views inside GiftedForm, not only widgets

FaridSafi avatar Feb 15 '16 17:02 FaridSafi

But if i need to place 'input' widgets inside another view and that view will be inside GiftedForm, widgets will not be recognized as widgets (because they are not direct children)...

aksonov avatar Feb 15 '16 17:02 aksonov

correct, one solution can be to do like https://github.com/FaridSafi/react-native-gifted-form/blob/master/widgets/GroupWidget.js#L20 in your direct children

FaridSafi avatar Feb 15 '16 17:02 FaridSafi

Thanks, implemented it, but behavior is quite weird, it scrolls to right, not to bottom: simulator screen shot 15 2016 19 02 17

aksonov avatar Feb 15 '16 18:02 aksonov

weird, what if you replace https://github.com/FaridSafi/react-native-gifted-form/blob/c748128caf005a3aca5a624b1bf5210a92bb3698/mixins/ContainerMixin.js#L51 by

          this._scrollResponder.scrollWithoutAnimationTo(0, this._pageY + this._y - this._locationY - keyboardHeight + 44);          

FaridSafi avatar Feb 15 '16 18:02 FaridSafi

Works better, thanks, will you commit it? One problem - it doesn't scroll back to top when i focus on upper elements (i.e. restore position)

aksonov avatar Feb 15 '16 18:02 aksonov

I don't understand why x and y are inverted in your case. Which RN version are you using (I didn't test with last ones) ?

FaridSafi avatar Feb 15 '16 18:02 FaridSafi

0.19

aksonov avatar Feb 15 '16 20:02 aksonov

@danielweinmann Are you using 0.19 and experimenting scrollWithoutAnimationTo issue too ?

FaridSafi avatar Feb 16 '16 07:02 FaridSafi

Also I got two warnings (scrollWithoutAnimationTo looks deprecated now?) simulator screen shot 16 2016 12 31 22

aksonov avatar Feb 16 '16 11:02 aksonov

I didn't test but I guess it can be fixed by scrollTo({x, y, animated: false})

FaridSafi avatar Feb 16 '16 11:02 FaridSafi

Your text input widget with right image looks nice! you should share it :smiley:

FaridSafi avatar Feb 16 '16 11:02 FaridSafi

I could submit PR for scrollTo and widget enhancement but i can't now, because one PR is waiting already (i don't want to mix everything in one PR)

aksonov avatar Feb 16 '16 13:02 aksonov

@FaridSafi @aksonov I'm using 0.19 and I do experiment the issue ;)

Actually, I'm using scrollEnabled={false} in all my forms right now, since I don't need it so badly and didn't want to take the time to fix it.

danielweinmann avatar Feb 16 '16 23:02 danielweinmann

@FaridSafi I'm experiencing this issue as well (RN 0.19), and I've found using scrollTo instead of scrollWithoutAnimationTo seems to fix it.

rdhaliwal avatar Feb 17 '16 04:02 rdhaliwal

@FaridSafi Could we also restore scroll position once keyboard is disappeared?

aksonov avatar Feb 17 '16 09:02 aksonov

Thank you guys. I published 0.0.5 with support of react-native 0.20.0 ScrollView.scrollTo({x: 0, y: 5, animated: true})

@aksonov @FaridSafi Could we also restore scroll position once keyboard is disappeared? I couldn't manage to do it

FaridSafi avatar Feb 18 '16 00:02 FaridSafi

0.0.5 works great, thanks!

What the problem with scroll restoring? Have you tried just to subscribe to keyboard events, like described here: https://github.com/johanneslumpe/react-native-keyboardevents

Current hardcode keyboard height looks not good.

aksonov avatar Feb 18 '16 09:02 aksonov

@aksonov I can't remember exactly what was the problem with restoring but it might be related to the contentInset of the ScrollView

I didn't try keyboard events when I implemented it. | agree hardcore keyboard height is not good solution.

Sorry I can't work on this right now. If someone has time to improve the scrolling, I recommend to use the ScrollView Props onKeyboardWillShow/onKeyboardWillHide on iOS and onKeyboardDidShow/onKeyboardDidHide on Android (if onKeyboardWillShow doesn't work in Android)

  onKeyboardWillShow(e) {
    console.log('keyboard height '+(e.endCoordinates ? e.endCoordinates.height : e.end.height));
  },

FaridSafi avatar Feb 18 '16 18:02 FaridSafi