forked from APSL/react-native-keyboard-aware-scroll-view
-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeyboardAwareHOC.js
794 lines (725 loc) · 28.3 KB
/
KeyboardAwareHOC.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/* @flow */
import React from 'react'
import PropTypes from 'prop-types'
import ReactNative, {
Keyboard,
Platform,
UIManager,
TextInput,
findNodeHandle,
Animated,
Dimensions
} from 'react-native'
import { isIphoneX } from 'react-native-iphone-x-helper'
import type { KeyboardAwareInterface } from './KeyboardAwareInterface'
import { NativeModules } from 'react-native';
const { RNTKeyboardAwareScrollView } = NativeModules;
const _KAM_DEFAULT_TAB_BAR_HEIGHT: number = isIphoneX() ? 83 : 49
const _KAM_KEYBOARD_OPENING_TIME: number = 250
const _KAM_EXTRA_LAYOUT_DELAY: number = 200
const _KEYBOARD_OPEN_CLOSE_USER_NOTICE_THRESHOLD = 500;
const _KAM_EXTRA_HEIGHT: number = 75
const supportedKeyboardEvents = [
'keyboardWillShow',
'keyboardDidShow',
'keyboardWillHide',
'keyboardDidHide',
'keyboardWillChangeFrame',
'keyboardDidChangeFrame'
]
const keyboardEventToCallbackName = (eventName: string) =>
'on' + eventName[0].toUpperCase() + eventName.substring(1)
const keyboardEventPropTypes = supportedKeyboardEvents.reduce(
(acc: Object, eventName: string) => ({
...acc,
[keyboardEventToCallbackName(eventName)]: PropTypes.func
}),
{}
)
const keyboardAwareHOCTypeEvents = supportedKeyboardEvents.reduce(
(acc: Object, eventName: string) => ({
...acc,
[keyboardEventToCallbackName(eventName)]: Function
}),
{}
)
export type KeyboardAwareHOCProps = {
viewIsInsideTabBar?: boolean,
resetScrollToCoords?: {
x: number,
y: number
},
enableResetScrollToCoords?: boolean,
enableAutomaticScroll?: boolean,
extraHeight?: number,
extraScrollHeight?: number,
extraBottomInset?: number,
inputAccessoryViewHeight: number,
keyboardOpeningTime?: number,
onScroll?: Function,
onLayout?: Function,
update?: Function,
contentContainerStyle?: any,
enableOnAndroid?: boolean,
innerRef?: Function,
...keyboardAwareHOCTypeEvents
}
export type KeyboardAwareHOCState = {
keyboardSpace: number,
keyboardEndCoordinatesScreenY: number,
}
export type ElementLayout = {
x: number,
y: number,
width: number,
height: number
}
export type ContentOffset = {
x: number,
y: number
}
export type ScrollPosition = {
x: number,
y: number,
animated: boolean
}
export type ScrollIntoViewOptions = ?{
getScrollPosition?: (
parentLayout: ElementLayout,
childLayout: ElementLayout,
contentOffset: ContentOffset
) => ScrollPosition
}
export type KeyboardAwareHOCOptions = ?{
enableOnAndroid: boolean,
contentContainerStyle: ?Object,
enableAutomaticScroll: boolean,
extraHeight: number,
extraScrollHeight: number,
extraBottomInset: number,
inputAccessoryViewHeight: number,
enableResetScrollToCoords: boolean,
keyboardOpeningTime: number,
viewIsInsideTabBar: boolean,
refPropName: string,
extractNativeRef: Function
}
function getDisplayName(WrappedComponent: React$Component) {
return WrappedComponent && (WrappedComponent.displayName || WrappedComponent.name) || 'Component'
}
const ScrollIntoViewDefaultOptions: KeyboardAwareHOCOptions = {
enableOnAndroid: false,
contentContainerStyle: undefined,
enableAutomaticScroll: true,
extraHeight: _KAM_EXTRA_HEIGHT,
extraScrollHeight: 0,
extraBottomInset: 0,
inputAccessoryViewHeight: 0,
enableResetScrollToCoords: true,
keyboardOpeningTime: _KAM_KEYBOARD_OPENING_TIME,
viewIsInsideTabBar: false,
// The ref prop name that will be passed to the wrapped component to obtain a ref
// If your ScrollView is already wrapped, maybe the wrapper permit to get a ref
// For example, with glamorous-native ScrollView, you should use "innerRef"
refPropName: 'ref',
// Sometimes the ref you get is a ref to a wrapped view (ex: Animated.ScrollView)
// We need access to the imperative API of a real native ScrollView so we need extraction logic
extractNativeRef: (ref: Object) => {
// getNode() permit to support Animated.ScrollView automatically
// see https://github.com/facebook/react-native/issues/19650
// see https://stackoverflow.com/questions/42051368/scrollto-is-undefined-on-animated-scrollview/48786374
if (ref.getNode) {
return ref.getNode()
} else {
return ref
}
}
}
function KeyboardAwareHOC(
ScrollableComponent: React$Component,
userOptions: KeyboardAwareHOCOptions
) {
const hocOptions: KeyboardAwareHOCOptions = {
...ScrollIntoViewDefaultOptions,
...userOptions
}
return class
extends React.Component<KeyboardAwareHOCProps, KeyboardAwareHOCState>
implements KeyboardAwareInterface {
_rnkasv_keyboardView: any
keyboardWillShowEvent: ?Function
keyboardWillHideEvent: ?Function
position: ContentOffset
defaultResetScrollToCoords: ?{ x: number, y: number }
mountedComponent: boolean
handleOnScroll: Function
handleOnLayout: Function
refreshScrollForField: Function
state: KeyboardAwareHOCState
parentLayout: any
bottomDistanceToWindow: number
topDistanceToWindow: number
keyboardWillHideTime: number
static displayName = `KeyboardAware${getDisplayName(ScrollableComponent)}`
static propTypes = {
viewIsInsideTabBar: PropTypes.bool,
resetScrollToCoords: PropTypes.shape({
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired
}),
enableResetScrollToCoords: PropTypes.bool,
enableAutomaticScroll: PropTypes.bool,
extraHeight: PropTypes.number,
extraScrollHeight: PropTypes.number,
keyboardOpeningTime: PropTypes.number,
onScroll: PropTypes.oneOfType([
PropTypes.func, // Normal listener
PropTypes.object // Animated.event listener
]),
onLayout: PropTypes.oneOfType([
PropTypes.func, // Normal listener
PropTypes.object // Animated.event listener
]),
update: PropTypes.func,
contentContainerStyle: PropTypes.any,
enableOnAndroid: PropTypes.bool,
innerRef: PropTypes.func,
...keyboardEventPropTypes
}
// HOC options are used to init default props, so that these options can be overriden with component props
static defaultProps = {
enableAutomaticScroll: hocOptions.enableAutomaticScroll,
extraHeight: hocOptions.extraHeight,
extraScrollHeight: hocOptions.extraScrollHeight,
extraBottomInset: hocOptions.extraBottomInset,
inputAccessoryViewHeight: hocOptions.inputAccessoryViewHeight,
enableResetScrollToCoords: hocOptions.enableResetScrollToCoords,
keyboardOpeningTime: hocOptions.keyboardOpeningTime,
viewIsInsideTabBar: hocOptions.viewIsInsideTabBar,
enableOnAndroid: hocOptions.enableOnAndroid
}
constructor(props: KeyboardAwareHOCProps) {
super(props)
this.keyboardWillShowEvent = undefined
this.keyboardWillHideEvent = undefined
this.callbacks = {}
this.position = { x: 0, y: 0 }
this.defaultResetScrollToCoords = null
const keyboardSpace: number = props.viewIsInsideTabBar
? _KAM_DEFAULT_TAB_BAR_HEIGHT
: 0
this.state = { keyboardSpace }
this.refreshScrollForField = this._refreshScrollForField.bind( this );
}
async componentDidMount() {
this.mountedComponent = true
// Keyboard events
if (Platform.OS === 'ios') {
this.keyboardWillShowEvent = Keyboard.addListener(
'keyboardWillShow',
this._updateKeyboardSpace
)
this.keyboardWillHideEvent = Keyboard.addListener(
'keyboardWillHide',
this._resetKeyboardSpace
)
} else if (Platform.OS === 'android' && this.props.enableOnAndroid) {
this.keyboardWillShowEvent = Keyboard.addListener(
'keyboardDidShow',
this._updateKeyboardSpace
)
this.keyboardWillHideEvent = Keyboard.addListener(
'keyboardDidHide',
this._resetKeyboardSpace
)
}
supportedKeyboardEvents.forEach((eventName: string) => {
const callbackName = keyboardEventToCallbackName(eventName)
if (this.props[callbackName]) {
this.callbacks[eventName] = Keyboard.addListener(
eventName,
this.props[callbackName]
)
}
})
}
componentWillReceiveProps(nextProps: KeyboardAwareHOCProps) {
if (nextProps.viewIsInsideTabBar !== this.props.viewIsInsideTabBar) {
const keyboardSpace: number = nextProps.viewIsInsideTabBar
? _KAM_DEFAULT_TAB_BAR_HEIGHT
: 0
if (this.state.keyboardSpace !== keyboardSpace) {
this.setState({ keyboardSpace })
}
}
}
componentWillUnmount() {
this.mountedComponent = false
this.keyboardWillShowEvent && this.keyboardWillShowEvent.remove()
this.keyboardWillHideEvent && this.keyboardWillHideEvent.remove()
Object.values(this.callbacks).forEach((callback: Object) =>
callback.remove()
)
}
getScrollResponder = () => {
return (
this._rnkasv_keyboardView &&
this._rnkasv_keyboardView.getScrollResponder()
)
}
scrollToPosition = (x: number, y: number, animated: boolean = true) => {
const responder = this.getScrollResponder()
responder && responder.scrollResponderScrollTo({ x, y, animated })
}
scrollToEnd = (animated?: boolean = true) => {
const responder = this.getScrollResponder()
responder && responder.scrollResponderScrollToEnd({ animated })
}
scrollForExtraHeightOnAndroid = (extraHeight: number) => {
this.scrollToPosition(0, this.position.y + extraHeight, true)
}
/**
* @param keyboardOpeningTime: takes a different keyboardOpeningTime in consideration.
* @param extraHeight: takes an extra height in consideration.
*/
scrollToFocusedInput = (
reactNode: any,
extraHeight?: number,
keyboardOpeningTime?: number
) => {
if (extraHeight === undefined) {
extraHeight = this.props.extraHeight || 0
}
if (keyboardOpeningTime === undefined) {
keyboardOpeningTime = this.props.keyboardOpeningTime || 0
}
setTimeout(() => {
if (!this.mountedComponent) {
return
}
const responder = this.getScrollResponder()
responder &&
responder.scrollResponderScrollNativeHandleToKeyboard(
reactNode,
extraHeight,
true
)
}, keyboardOpeningTime)
}
scrollIntoView = async (
element: React.Element<*>,
options: ScrollIntoViewOptions = {}
) => {
if (!this._rnkasv_keyboardView || !element) {
return
}
const [parentLayout, childLayout] = await Promise.all([
this._measureElement(this._rnkasv_keyboardView),
this._measureElement(element)
])
const getScrollPosition =
options.getScrollPosition || this._defaultGetScrollPosition
const { x, y, animated } = getScrollPosition(
parentLayout,
childLayout,
this.position
)
this.scrollToPosition(x, y, animated)
}
_defaultGetScrollPosition = (
parentLayout: ElementLayout,
childLayout: ElementLayout,
contentOffset: ContentOffset
): ScrollPosition => {
return {
x: 0,
y: Math.max(0, childLayout.y - parentLayout.y + contentOffset.y),
animated: true
}
}
_measureElement = (element: React.Element<*>): Promise<ElementLayout> => {
const node = findNodeHandle(element)
return new Promise((resolve: ElementLayout => void) => {
UIManager.measureInWindow(
node,
(x: number, y: number, width: number, height: number) => {
resolve({ x, y, width, height })
}
)
})
}
async _calculateTopBottomDistanceIfNeeded() {
if ( !this.parentLayout ) {
const parentLayout = await this._measureElement(this._rnkasv_keyboardView);
this.parentLayout = parentLayout;
const { height: fullHeight } = Dimensions.get( 'window' );
this.bottomDistanceToWindow = fullHeight - ( parentLayout.y + parentLayout.height );
this.topDistanceToWindow = parentLayout.y;
}
}
_wasKeyboardAlreadyOpen(keyboardWillOpenTime: number) {
//happens when keyboard opens/closes very fast that user won't even notice
return ( keyboardWillOpenTime - this.keyboardWillHideTime ) < _KEYBOARD_OPEN_CLOSE_USER_NOTICE_THRESHOLD;
}
_calculateLayoutDelayOnKeyboardWillOpen(keyboardWillOpenTime: number) {
var layoutDelay = 0;
if ( this._wasKeyboardAlreadyOpen( keyboardWillOpenTime ) ) {
layoutDelay = _KAM_EXTRA_LAYOUT_DELAY;
this.keyboardWillHideTime = 0; //Reset until next keyboard hide
}
return layoutDelay;
}
// Keyboard actions
_updateKeyboardSpace = async (keyboardEvent: Object) => {
const layoutDelay = this._calculateLayoutDelayOnKeyboardWillOpen( Date.now() );
// Automatically scroll to focused TextInput
if (this.props.enableAutomaticScroll) {
let keyboardSpace: number =
keyboardEvent.endCoordinates.height + this.props.extraBottomInset
if (this.props.viewIsInsideTabBar) {
keyboardSpace -= _KAM_DEFAULT_TAB_BAR_HEIGHT
}
this.setState({ keyboardSpace, keyboardEndCoordinatesScreenY: keyboardEvent.endCoordinates.screenY})
setTimeout(() => {
this._refreshScrollForField(null);
}, layoutDelay); //give some time to layout
}
if (!this.props.resetScrollToCoords) {
if (!this.defaultResetScrollToCoords) {
this.defaultResetScrollToCoords = this.position
}
}
}
_scrollingDelayFor( fieldId: ?number ) {
if ( fieldId === undefined ) { //triggered by keyboard
return this.props.keyboardOpeningTime || 0
}
return 0; //triggered programmatically
}
_viewIsDescendantOf(
currentlyFocusedField: number,
ancestorViewId: number,
callback: (isAncestor: boolean) => void
) {
if ( Platform.OS === 'ios' ) {
RNTKeyboardAwareScrollView.viewIsDescendantOf(
currentlyFocusedField,
ancestorViewId,
( error: ?string, isAncestor: boolean ) => {
if ( error ) {
console.warn(`RNTKeyboardAwareScrollView.viewIsDescendantOf call returned error: ${ error }`);
return;
}
callback(isAncestor);
}
);
} else {
UIManager.viewIsDescendantOf(
currentlyFocusedField,
ancestorViewId,
callback
)
}
}
async _refreshScrollForField( fieldId: ?number ) {
await this._calculateTopBottomDistanceIfNeeded();
if ( !this.state ) {
return;
}
const { keyboardSpace, keyboardEndCoordinatesScreenY } = this.state;
const currentlyFocusedField = findNodeHandle(TextInput.State.currentlyFocusedInput())
const responder = this.getScrollResponder()
if (!currentlyFocusedField || !responder ) {
return
}
const scrollingDelay = this._scrollingDelayFor( fieldId );
//it is ok to not have a fieldId but if we do then it should be same with currentlyFocusedField
if ( !( fieldId && ( currentlyFocusedField == fieldId ) || !fieldId ) ) {
return
}
this._viewIsDescendantOf(
currentlyFocusedField,
responder.getInnerViewNode(),
(isAncestor: boolean) => {
if (isAncestor) {
if (Platform.OS === 'android') {
this._scrollToBottomOfComponent(currentlyFocusedField, keyboardEndCoordinatesScreenY, keyboardSpace);
} else {
RNTKeyboardAwareScrollView.measureSelectionInWindow(
currentlyFocusedField,
(error: ?string, fieldX: number, fieldY: number, fieldWidth: number, fieldHeight: number,
caretX: number, caretY: number, caretRelativeX: number, caretRelativeY: number,
caretWidth: number, caretHeight: number, textInputBottomTextInset: number ) => {
const viewableBottomCoordinateY = this._viewableBottomCoordinateY( keyboardEndCoordinatesScreenY );
if ( error ) {
//Try old way
this._scrollToBottomOfComponent(currentlyFocusedField, viewableBottomCoordinateY, keyboardSpace);
} else {
// adjust scroll offset only if caret will remain out of viewable area
if ( this._isCaretOutOfViewableArea(caretY, caretHeight, viewableBottomCoordinateY) ) {
//Decide if we should scroll to the caret or to the bottom of the component
if( this._shouldScrollToBottomOfComponent(fieldY, fieldHeight, caretY, caretHeight, textInputBottomTextInset, viewableBottomCoordinateY) )
{
//Scroll till the bottom of component
this._doScrollToBottomOfComponent(currentlyFocusedField, fieldY, fieldHeight, viewableBottomCoordinateY, keyboardSpace, scrollingDelay);
} else {
//Scroll till the caret is visible
this._scrollToCaret(currentlyFocusedField, caretRelativeY, caretHeight, viewableBottomCoordinateY, scrollingDelay);
}
}
}
});
}
}
}
)
}
_viewableBottomCoordinateY( keyboardEndCoordinatesScreenY: ?number ) {
// When the soft keyboard is not present (using an external keyboard), `keyboardEndCoordinatesScreenY` is undefined.
// We will use the top most Y point from the input accessory view as the keyboard screen Y value.
const { height: windowHeight } = Dimensions.get( 'window' );
return keyboardEndCoordinatesScreenY ? keyboardEndCoordinatesScreenY : windowHeight - this.props.inputAccessoryViewHeight;
}
_totalVerticalDistanceToWindow() {
return this.topDistanceToWindow + this.bottomDistanceToWindow + this.props.extraBottomInset;
}
_scrollToCaret(
currentlyFocusedField: number,
caretRelativeY: number,
caretHeight: number,
keyboardEndCoordinatesScreenY: number,
keyboardOpeningTime?: number)
{
if ( keyboardOpeningTime === undefined ) {
keyboardOpeningTime = this.props.keyboardOpeningTime || 0
}
const measureLayoutSuccessHandler = (
x: number,
y: number,
width: number,
height: number) => {
const extraScrollHeigth = caretHeight; //Show one extra line below the caret.
const keyboardYOnWindow = keyboardEndCoordinatesScreenY;
const textViewYOnScrollview = y;
const totalAccessoriesHeight = this.props.inputAccessoryViewHeight + this.topDistanceToWindow;
const caretBottomYOnScrollview = textViewYOnScrollview + caretRelativeY + caretHeight;
let scrollOffsetY = caretBottomYOnScrollview + totalAccessoriesHeight + extraScrollHeigth - keyboardYOnWindow;
scrollOffsetY = Math.max(0, scrollOffsetY); //prevent negative scroll offset
const responder = this.getScrollResponder();
responder && responder.scrollResponderScrollTo( { x: 0, y: scrollOffsetY, animated: true } );
}
const measureLayoutErrorHandler = ( e: Object ) => {
console.error('Error measuring text field: ', e);
}
setTimeout( () => {
if ( !this.mountedComponent ) {
return
}
const responder = this.getScrollResponder();
responder &&
UIManager.measureLayout(
ReactNative.findNodeHandle(currentlyFocusedField),
ReactNative.findNodeHandle(responder.getInnerViewNode()),
measureLayoutErrorHandler,
measureLayoutSuccessHandler,
);
}, keyboardOpeningTime);
}
_isCaretAtLastLine(caretY: number, caretHeight: number, fieldY: number, fieldHeight: number, textInputBottomTextInset: number) {
return ( caretY + 2*caretHeight + textInputBottomTextInset ) > ( fieldY + fieldHeight )
}
_isFocusedAreaFitsInViewableArea(caretY: number, fieldY: number, fieldHeight: number, keyboardEndCoordinatesScreenY: number) {
const distanceBetweenCaretAndBottomOfField = fieldY + fieldHeight - caretY + this.props.extraScrollHeight;
const viewableAreaStartY = this.topDistanceToWindow;
const viewableAreaEndY = keyboardEndCoordinatesScreenY - this.props.inputAccessoryViewHeight;
const viewableAreaHeight = viewableAreaEndY - viewableAreaStartY;
return viewableAreaHeight > distanceBetweenCaretAndBottomOfField;
}
_isCaretUnderKeyboard(caretY: number, caretHeight: number, keyboardEndCoordinatesScreenY) {
const caretBottomPosition = caretY + caretHeight;
const keyboardPosition = keyboardEndCoordinatesScreenY
return caretBottomPosition > (keyboardPosition - this.props.inputAccessoryViewHeight);
}
_isCaretAboveViewableArea(caretY: number) { //caret may remain above the viewable area when orientation changes
return this.topDistanceToWindow > caretY;
}
_isCaretOutOfViewableArea(caretY: number, caretHeight: number, keyboardEndCoordinatesScreenY: number) {
return this._isCaretUnderKeyboard(caretY, caretHeight, keyboardEndCoordinatesScreenY ) ||
this._isCaretAboveViewableArea(caretY);
}
_shouldScrollToBottomOfComponent(fieldY: number, fieldHeight: number, caretY: number, caretHeight: number, textInputBottomTextInset: number, keyboardEndCoordinatesScreenY: number)
{
//is there enough place in the viewable area when keyboard is open?
return this._isFocusedAreaFitsInViewableArea( caretY, fieldY, fieldHeight, keyboardEndCoordinatesScreenY) &&
//is the caret at the last line of the component?
this._isCaretAtLastLine(caretY, caretHeight, fieldY, fieldHeight, textInputBottomTextInset);
}
_scrollToBottomOfComponent(currentlyFocusedField: number, keyboardEndCoordinatesScreenY: number, keyboardSpace: number) {
UIManager.measureInWindow(
currentlyFocusedField,
(x: number, y: number, width: number, height: number) => {
this._doScrollToBottomOfComponent(currentlyFocusedField, y, height, keyboardEndCoordinatesScreenY, keyboardSpace);
}
);
}
_extraScrollHeight() {
const verticalDistanceToWindow = (this.state.keyboardEndCoordinatesScreenY !== undefined) ? this.topDistanceToWindow : this._totalVerticalDistanceToWindow();
return this.props.extraScrollHeight + verticalDistanceToWindow + this.props.inputAccessoryViewHeight;
}
_doScrollToBottomOfComponent(currentlyFocusedField: number, y: number, height: number, keyboardEndCoordinatesScreenY: number, keyboardSpace: number, keyboardOpeningTime?: number) {
// Check if the TextInput will be hidden by the keyboard
const textInputBottomPosition = y + height
const keyboardPosition = keyboardEndCoordinatesScreenY
const totalExtraHeight =
this._extraScrollHeight() + this.props.extraHeight
if (Platform.OS === 'ios') {
if (
textInputBottomPosition >
keyboardPosition - totalExtraHeight
) {
this._scrollToFocusedInputWithNodeHandle(
currentlyFocusedField,
null,
keyboardOpeningTime
)
}
} else {
// On android, the system would scroll the text input just
// above the keyboard so we just need to scroll the extra
// height part
if (textInputBottomPosition > keyboardPosition) {
// Since the system already scrolled the whole view up
// we should reduce that amount
let adjustedKeyboardSpace =
keyboardSpace -
(textInputBottomPosition - keyboardPosition)
this.setState({ adjustedKeyboardSpace })
this.scrollForExtraHeightOnAndroid(totalExtraHeight)
} else if (
textInputBottomPosition >
keyboardPosition - totalExtraHeight
) {
this.scrollForExtraHeightOnAndroid(
totalExtraHeight -
(keyboardPosition - textInputBottomPosition)
)
}
}
}
_resetKeyboardSpace = () => {
this.keyboardWillHideTime = Date.now();
const keyboardSpace: number = this.props.viewIsInsideTabBar
? _KAM_DEFAULT_TAB_BAR_HEIGHT
: 0
this.setState({ keyboardSpace, keyboardEndCoordinatesScreenY: undefined })
// Reset scroll position after keyboard dismissal
if (this.props.enableResetScrollToCoords === false) {
this.defaultResetScrollToCoords = null
return
} else if (this.props.resetScrollToCoords) {
this.scrollToPosition(this.props.resetScrollToCoords.x, this.props.resetScrollToCoords.y, true)
} else {
if (this.defaultResetScrollToCoords) {
this.scrollToPosition(
this.defaultResetScrollToCoords.x,
this.defaultResetScrollToCoords.y,
true
)
this.defaultResetScrollToCoords = null
} else {
this.scrollToPosition(0, 0, true)
}
}
}
_scrollToFocusedInputWithNodeHandle = (
nodeID: number,
extraHeight?: number,
keyboardOpeningTime?: number
) => {
if (extraHeight === undefined) {
extraHeight = this.props.extraHeight
}
const reactNode = ReactNative.findNodeHandle(nodeID)
this.scrollToFocusedInput(
reactNode,
extraHeight + this._extraScrollHeight(),
keyboardOpeningTime !== undefined
? keyboardOpeningTime
: this.props.keyboardOpeningTime || 0
)
}
_handleOnScroll = (
e: SyntheticEvent<*> & { nativeEvent: { contentOffset: number } }
) => {
this.position = e.nativeEvent.contentOffset
}
_handleOnLayout = () => {
this.parentLayout = null;
}
_handleRef = (ref: React.Component<*>) => {
this._rnkasv_keyboardView = ref ? hocOptions.extractNativeRef(ref) : ref
if (this.props.innerRef) {
this.props.innerRef(this._rnkasv_keyboardView)
}
}
update = () => {
const currentlyFocusedField = TextInput.State.currentlyFocusedField()
const responder = this.getScrollResponder()
if (!currentlyFocusedField || !responder) {
return
}
this._scrollToFocusedInputWithNodeHandle(currentlyFocusedField)
}
render() {
const { enableOnAndroid, contentContainerStyle, onScroll, onLayout } = this.props
let newContentContainerStyle
if (Platform.OS === 'android' && enableOnAndroid) {
newContentContainerStyle = [].concat(contentContainerStyle).concat({
paddingBottom:
((contentContainerStyle || {}).paddingBottom || 0) +
this.state.keyboardSpace
})
}
const refProps = { [hocOptions.refPropName]: this._handleRef }
return (
<ScrollableComponent
{...refProps}
keyboardDismissMode='interactive'
contentInset={{ bottom: this.state.keyboardSpace }}
automaticallyAdjustContentInsets={false}
showsVerticalScrollIndicator={true}
scrollEventThrottle={1}
{...this.props}
contentContainerStyle={
newContentContainerStyle || contentContainerStyle
}
keyboardSpace={this.state.keyboardSpace}
getScrollResponder={this.getScrollResponder}
scrollToPosition={this.scrollToPosition}
refreshScrollForField={this.refreshScrollForField}
scrollToEnd={this.scrollToEnd}
scrollForExtraHeightOnAndroid={this.scrollForExtraHeightOnAndroid}
scrollToFocusedInput={this.scrollToFocusedInput}
scrollIntoView={this.scrollIntoView}
resetKeyboardSpace={this._resetKeyboardSpace}
handleOnScroll={this._handleOnScroll}
handleOnLayout={this._handleOnLayout}
update={this.update}
onScroll={Animated.forkEvent(onScroll, this._handleOnScroll)}
onLayout={Animated.forkEvent(onLayout, this._handleOnLayout)}
/>
)
}
}
}
// Allow to pass options, without breaking change, and curried for composition
// listenToKeyboardEvents(ScrollView);
// listenToKeyboardEvents(options)(Comp);
const listenToKeyboardEvents = (configOrComp: any) => {
if (typeof configOrComp === 'object' && !configOrComp.displayName) {
return (Comp: Function) => KeyboardAwareHOC(Comp, configOrComp)
} else {
return KeyboardAwareHOC(configOrComp)
}
}
export default listenToKeyboardEvents