@codeleap/mobile 2.3.8 → 2.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -3
- package/src/components/ActionIcon/index.tsx +32 -0
- package/src/components/ActionIcon/styles.ts +97 -0
- package/src/components/ActivityIndicator/index.tsx +50 -0
- package/src/components/ActivityIndicator/styles.ts +68 -0
- package/src/components/Animated.tsx +34 -0
- package/src/components/AutoComplete/index.tsx +163 -0
- package/src/components/AutoComplete/styles.ts +44 -0
- package/src/components/Backdrop/index.tsx +48 -0
- package/src/components/Backdrop/styles.ts +33 -0
- package/src/components/Button/index.tsx +155 -0
- package/src/components/Button/styles.ts +129 -0
- package/src/components/Calendar/index.tsx +65 -0
- package/src/components/Calendar/style.ts +35 -0
- package/src/components/Calendar/types.ts +102 -0
- package/src/components/Checkbox/index.tsx +91 -0
- package/src/components/Checkbox/styles.ts +81 -0
- package/src/components/ContentView/index.tsx +63 -0
- package/src/components/ContentView/styles.ts +24 -0
- package/src/components/Drawer/index.tsx +33 -0
- package/src/components/Drawer/styles.ts +43 -0
- package/src/components/EmptyPlaceholder/index.tsx +88 -0
- package/src/components/EmptyPlaceholder/styles.ts +58 -0
- package/src/components/FileInput/index.tsx +181 -0
- package/src/components/FileInput/styles.ts +15 -0
- package/src/components/Grid/index.tsx +117 -0
- package/src/components/Grid/styles.ts +11 -0
- package/src/components/Icon/index.tsx +69 -0
- package/src/components/Icon/styles.ts +57 -0
- package/src/components/Image/index.tsx +91 -0
- package/src/components/Image/styles.ts +20 -0
- package/src/components/ImageView/Spotlight.tsx +157 -0
- package/src/components/ImageView/component.tsx +38 -0
- package/src/components/ImageView/index.ts +2 -0
- package/src/components/InputLabel/index.tsx +38 -0
- package/src/components/InputLabel/styles.ts +19 -0
- package/src/components/List/PaginationIndicator.tsx +71 -0
- package/src/components/List/index.tsx +114 -0
- package/src/components/List/styles.ts +19 -0
- package/src/components/Modal/index.tsx +218 -0
- package/src/components/Modal/styles.ts +153 -0
- package/src/components/MultiSelect/index.tsx +138 -0
- package/src/components/MultiSelect/styles.ts +18 -0
- package/src/components/MultiSelect/types.ts +42 -0
- package/src/components/Navigation/Navigation.tsx +54 -0
- package/src/components/Navigation/constants.ts +8 -0
- package/src/components/Navigation/index.tsx +3 -0
- package/src/components/Navigation/types.ts +35 -0
- package/src/components/Navigation/utils.tsx +57 -0
- package/src/components/Pager/index.tsx +121 -0
- package/src/components/Pager/styles.ts +81 -0
- package/src/components/RadioInput/index.tsx +106 -0
- package/src/components/RadioInput/styles.ts +67 -0
- package/src/components/Scroll/index.tsx +124 -0
- package/src/components/Scroll/styles.ts +18 -0
- package/src/components/Sections/index.tsx +91 -0
- package/src/components/SegmentedControl/index.tsx +204 -0
- package/src/components/SegmentedControl/styles.ts +89 -0
- package/src/components/Select/index.tsx +167 -0
- package/src/components/Select/styles.ts +62 -0
- package/src/components/Select/types.ts +43 -0
- package/src/components/Slider/Mark.tsx +46 -0
- package/src/components/Slider/Thumb.tsx +29 -0
- package/src/components/Slider/index.tsx +130 -0
- package/src/components/Slider/styles.ts +76 -0
- package/src/components/Slider/types.ts +30 -0
- package/src/components/Switch/index.tsx +91 -0
- package/src/components/Switch/styles.ts +38 -0
- package/src/components/Text/index.tsx +124 -0
- package/src/components/Text/styles.ts +50 -0
- package/src/components/TextInput/index.tsx +319 -0
- package/src/components/TextInput/styles.ts +127 -0
- package/src/components/Touchable/index.tsx +174 -0
- package/src/components/Touchable/styles.ts +28 -0
- package/src/components/View/index.tsx +103 -0
- package/src/components/View/styles.ts +24 -0
- package/src/components/components.ts +42 -0
- package/src/components/defaultStyles.ts +62 -0
- package/src/components/legacy/Modal/index.tsx +163 -0
- package/src/components/legacy/Modal/styles.ts +125 -0
- package/src/components/legacy/Pager/index.tsx +242 -0
- package/src/components/legacy/Pager/styles.ts +51 -0
- package/src/components/legacy/index.ts +2 -0
- package/src/modules/documentPicker.ts +7 -0
- package/src/modules/fastImage.ts +2 -0
- package/src/modules/imageCropPicker.d.ts +497 -0
- package/src/modules/index.d.ts +682 -0
- package/src/modules/reactNavigation.ts +15 -0
- package/src/modules/textInputMask.ts +11 -0
- package/src/modules/types/documentPicker.d.ts +215 -0
- package/src/modules/types/fileTypes.ts +138 -0
- package/src/modules/types/textInputMask.ts +9 -0
- package/src/types/index.ts +1 -0
- package/src/types/utility.ts +9 -0
- package/src/utils/KeyboardAware/context.tsx +75 -0
- package/src/utils/KeyboardAware/index.ts +17 -0
- package/src/utils/KeyboardAware/keyboardHooks.ts +124 -0
- package/src/utils/KeyboardAware/lib/KeyboardAwareFlatList.ts +4 -0
- package/src/utils/KeyboardAware/lib/KeyboardAwareHOC.tsx +618 -0
- package/src/utils/KeyboardAware/lib/KeyboardAwareInterface.ts +13 -0
- package/src/utils/KeyboardAware/lib/KeyboardAwareScrollView.ts +6 -0
- package/src/utils/KeyboardAware/lib/KeyboardAwareSectionList.ts +6 -0
- package/src/utils/KeyboardAware/types.ts +159 -0
- package/src/utils/ModalManager/components.tsx +112 -0
- package/src/utils/ModalManager/context.tsx +260 -0
- package/src/utils/ModalManager/index.ts +16 -0
- package/src/utils/OSAlert.ts +180 -0
- package/src/utils/PermissionManager/context.tsx +302 -0
- package/src/utils/PermissionManager/index.ts +20 -0
- package/src/utils/PermissionManager/types.ts +24 -0
- package/src/utils/hooks.ts +163 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/input.ts +51 -0
- package/src/utils/misc.ts +83 -0
- package/src/utils/notifications.ts +206 -0
- package/src/utils/theme.ts +58 -0
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
/* eslint-disable max-lines */
|
|
2
|
+
/* @flow */
|
|
3
|
+
|
|
4
|
+
import React from 'react'
|
|
5
|
+
import PropTypes from 'prop-types'
|
|
6
|
+
import {
|
|
7
|
+
Keyboard,
|
|
8
|
+
Platform,
|
|
9
|
+
UIManager,
|
|
10
|
+
TextInput,
|
|
11
|
+
findNodeHandle,
|
|
12
|
+
Animated,
|
|
13
|
+
Dimensions,
|
|
14
|
+
KeyboardEventName,
|
|
15
|
+
EventSubscription,
|
|
16
|
+
KeyboardEventListener,
|
|
17
|
+
NativeSyntheticEvent,
|
|
18
|
+
ScrollView,
|
|
19
|
+
} from 'react-native'
|
|
20
|
+
import type { KeyboardAwareInterface } from './KeyboardAwareInterface'
|
|
21
|
+
export function isIphoneX() {
|
|
22
|
+
const dimen = Dimensions.get('window')
|
|
23
|
+
return (
|
|
24
|
+
Platform.OS === 'ios' &&
|
|
25
|
+
!Platform.isPad &&
|
|
26
|
+
!Platform.isTVOS &&
|
|
27
|
+
((dimen.height === 780 || dimen.width === 780)
|
|
28
|
+
|| (dimen.height === 812 || dimen.width === 812)
|
|
29
|
+
|| (dimen.height === 844 || dimen.width === 844)
|
|
30
|
+
|| (dimen.height === 896 || dimen.width === 896)
|
|
31
|
+
|| (dimen.height === 926 || dimen.width === 926))
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const _KAM_DEFAULT_TAB_BAR_HEIGHT: number = isIphoneX() ? 83 : 49
|
|
36
|
+
const _KAM_KEYBOARD_OPENING_TIME = 250
|
|
37
|
+
const _KAM_EXTRA_HEIGHT = 75
|
|
38
|
+
|
|
39
|
+
const supportedKeyboardEvents: KeyboardEventName[] = [
|
|
40
|
+
'keyboardWillShow',
|
|
41
|
+
'keyboardDidShow',
|
|
42
|
+
'keyboardWillHide',
|
|
43
|
+
'keyboardDidHide',
|
|
44
|
+
'keyboardWillChangeFrame',
|
|
45
|
+
'keyboardDidChangeFrame',
|
|
46
|
+
]
|
|
47
|
+
const keyboardEventToCallbackName = (eventName: string) => 'on' + eventName[0].toUpperCase() + eventName.substring(1)
|
|
48
|
+
const keyboardEventPropTypes = supportedKeyboardEvents.reduce(
|
|
49
|
+
(acc: Object, eventName: string) => ({
|
|
50
|
+
...acc,
|
|
51
|
+
[keyboardEventToCallbackName(eventName)]: PropTypes.func,
|
|
52
|
+
}),
|
|
53
|
+
{},
|
|
54
|
+
)
|
|
55
|
+
const keyboardAwareHOCTypeEvents = supportedKeyboardEvents.reduce(
|
|
56
|
+
(acc: Object, eventName: string) => ({
|
|
57
|
+
...acc,
|
|
58
|
+
[keyboardEventToCallbackName(eventName)]: Function,
|
|
59
|
+
}),
|
|
60
|
+
{},
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
export type KeyboardAwareHOCProps = {
|
|
64
|
+
viewIsInsideTabBar?: boolean
|
|
65
|
+
resetScrollToCoords?: {
|
|
66
|
+
x: number
|
|
67
|
+
y: number
|
|
68
|
+
}
|
|
69
|
+
enableResetScrollToCoords?: boolean
|
|
70
|
+
enableAutomaticScroll?: boolean
|
|
71
|
+
extraHeight?: number
|
|
72
|
+
extraScrollHeight?: number
|
|
73
|
+
keyboardOpeningTime?: number
|
|
74
|
+
onScroll?: Function
|
|
75
|
+
update?: Function
|
|
76
|
+
contentContainerStyle?: any
|
|
77
|
+
enableOnAndroid?: boolean
|
|
78
|
+
Scrollable: any
|
|
79
|
+
innerRef?: Function
|
|
80
|
+
hocOptions?: any
|
|
81
|
+
} & typeof keyboardAwareHOCTypeEvents
|
|
82
|
+
export type KeyboardAwareHOCState = {
|
|
83
|
+
keyboardSpace: number
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type ElementLayout = {
|
|
87
|
+
x: number
|
|
88
|
+
y: number
|
|
89
|
+
width: number
|
|
90
|
+
height: number
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type ContentOffset = {
|
|
94
|
+
x: number
|
|
95
|
+
y: number
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type ScrollPosition = {
|
|
99
|
+
x: number
|
|
100
|
+
y: number
|
|
101
|
+
animated: boolean
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type ScrollIntoViewOptions = {
|
|
105
|
+
getScrollPosition?: (
|
|
106
|
+
parentLayout: ElementLayout,
|
|
107
|
+
childLayout: ElementLayout,
|
|
108
|
+
contentOffset: ContentOffset
|
|
109
|
+
) => ScrollPosition
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type KeyboardAwareHOCOptions = {
|
|
113
|
+
enableOnAndroid: boolean
|
|
114
|
+
contentContainerStyle?: Object
|
|
115
|
+
enableAutomaticScroll: boolean
|
|
116
|
+
extraHeight: number
|
|
117
|
+
extraScrollHeight: number
|
|
118
|
+
enableResetScrollToCoords: boolean
|
|
119
|
+
keyboardOpeningTime: number
|
|
120
|
+
viewIsInsideTabBar: boolean
|
|
121
|
+
refPropName: string
|
|
122
|
+
extractNativeRef: Function
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function getDisplayName(WrappedComponent: React.ComponentClass) {
|
|
126
|
+
return (
|
|
127
|
+
(WrappedComponent &&
|
|
128
|
+
(WrappedComponent.displayName || WrappedComponent.name)) ||
|
|
129
|
+
'Component'
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const ScrollIntoViewDefaultOptions: KeyboardAwareHOCOptions = {
|
|
134
|
+
enableOnAndroid: false,
|
|
135
|
+
contentContainerStyle: undefined,
|
|
136
|
+
enableAutomaticScroll: true,
|
|
137
|
+
extraHeight: _KAM_EXTRA_HEIGHT,
|
|
138
|
+
extraScrollHeight: 0,
|
|
139
|
+
enableResetScrollToCoords: true,
|
|
140
|
+
keyboardOpeningTime: _KAM_KEYBOARD_OPENING_TIME,
|
|
141
|
+
viewIsInsideTabBar: false,
|
|
142
|
+
|
|
143
|
+
// The ref prop name that will be passed to the wrapped component to obtain a ref
|
|
144
|
+
// If your ScrollView is already wrapped, maybe the wrapper permit to get a ref
|
|
145
|
+
// For example, with glamorous-native ScrollView, you should use "innerRef"
|
|
146
|
+
refPropName: 'ref',
|
|
147
|
+
// Sometimes the ref you get is a ref to a wrapped view (ex: Animated.ScrollView)
|
|
148
|
+
// We need access to the imperative API of a real native ScrollView so we need extraction logic
|
|
149
|
+
extractNativeRef: (ref: any) => {
|
|
150
|
+
// getNode() permit to support Animated.ScrollView automatically, but is deprecated since RN 0.62
|
|
151
|
+
// see https://github.com/facebook/react-native/issues/19650
|
|
152
|
+
// see https://stackoverflow.com/questions/42051368/scrollto-is-undefined-on-animated-scrollview/48786374
|
|
153
|
+
// see https://github.com/facebook/react-native/commit/66e72bb4e00aafbcb9f450ed5db261d98f99f82a
|
|
154
|
+
const shouldCallGetNode = !Platform.constants || (Platform.constants.reactNativeVersion.major === 0 && Platform.constants.reactNativeVersion.minor < 62)
|
|
155
|
+
if (ref.getNode && shouldCallGetNode) {
|
|
156
|
+
return ref.getNode()
|
|
157
|
+
} else {
|
|
158
|
+
return ref
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
class KeyboardAwareScrollable extends React.Component<KeyboardAwareHOCProps, KeyboardAwareHOCState> implements KeyboardAwareInterface {
|
|
164
|
+
_rnkasv_keyboardView: any
|
|
165
|
+
|
|
166
|
+
keyboardWillShowEvent: EventSubscription
|
|
167
|
+
|
|
168
|
+
keyboardWillHideEvent: EventSubscription
|
|
169
|
+
|
|
170
|
+
position: ContentOffset
|
|
171
|
+
|
|
172
|
+
defaultResetScrollToCoords?: { x: number; y: number }
|
|
173
|
+
|
|
174
|
+
mountedComponent: boolean
|
|
175
|
+
|
|
176
|
+
handleOnScroll: Function
|
|
177
|
+
|
|
178
|
+
callbacks: Record<string, EventSubscription>
|
|
179
|
+
|
|
180
|
+
state: KeyboardAwareHOCState
|
|
181
|
+
|
|
182
|
+
static displayName = `KeyboardAware`
|
|
183
|
+
|
|
184
|
+
static propTypes = {
|
|
185
|
+
viewIsInsideTabBar: PropTypes.bool,
|
|
186
|
+
resetScrollToCoords: PropTypes.shape({
|
|
187
|
+
x: PropTypes.number.isRequired,
|
|
188
|
+
y: PropTypes.number.isRequired,
|
|
189
|
+
}),
|
|
190
|
+
enableResetScrollToCoords: PropTypes.bool,
|
|
191
|
+
enableAutomaticScroll: PropTypes.bool,
|
|
192
|
+
extraHeight: PropTypes.number,
|
|
193
|
+
extraScrollHeight: PropTypes.number,
|
|
194
|
+
keyboardOpeningTime: PropTypes.number,
|
|
195
|
+
onScroll: PropTypes.oneOfType([
|
|
196
|
+
PropTypes.func, // Normal listener
|
|
197
|
+
PropTypes.object, // Animated.event listener
|
|
198
|
+
]),
|
|
199
|
+
update: PropTypes.func,
|
|
200
|
+
contentContainerStyle: PropTypes.any,
|
|
201
|
+
enableOnAndroid: PropTypes.bool,
|
|
202
|
+
innerRef: PropTypes.func,
|
|
203
|
+
...keyboardEventPropTypes,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// HOC options are used to init default props, so that these options can be overriden with component props
|
|
207
|
+
static defaultProps = {
|
|
208
|
+
enableAutomaticScroll: ScrollIntoViewDefaultOptions.enableAutomaticScroll,
|
|
209
|
+
extraHeight: ScrollIntoViewDefaultOptions.extraHeight,
|
|
210
|
+
extraScrollHeight: ScrollIntoViewDefaultOptions.extraScrollHeight,
|
|
211
|
+
enableResetScrollToCoords: ScrollIntoViewDefaultOptions.enableResetScrollToCoords,
|
|
212
|
+
keyboardOpeningTime: ScrollIntoViewDefaultOptions.keyboardOpeningTime,
|
|
213
|
+
viewIsInsideTabBar: ScrollIntoViewDefaultOptions.viewIsInsideTabBar,
|
|
214
|
+
enableOnAndroid: ScrollIntoViewDefaultOptions.enableOnAndroid,
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
constructor(props: KeyboardAwareHOCProps) {
|
|
218
|
+
super(props)
|
|
219
|
+
this.keyboardWillShowEvent = undefined
|
|
220
|
+
this.keyboardWillHideEvent = undefined
|
|
221
|
+
this.callbacks = {}
|
|
222
|
+
this.position = { x: 0, y: 0 }
|
|
223
|
+
this.defaultResetScrollToCoords = null
|
|
224
|
+
const keyboardSpace: number = props.viewIsInsideTabBar
|
|
225
|
+
? _KAM_DEFAULT_TAB_BAR_HEIGHT
|
|
226
|
+
: 0
|
|
227
|
+
this.state = { keyboardSpace }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
componentDidMount() {
|
|
231
|
+
this.mountedComponent = true
|
|
232
|
+
// Keyboard events
|
|
233
|
+
if (Platform.OS === 'ios') {
|
|
234
|
+
this.keyboardWillShowEvent = Keyboard.addListener(
|
|
235
|
+
'keyboardWillShow',
|
|
236
|
+
this._updateKeyboardSpace,
|
|
237
|
+
)
|
|
238
|
+
this.keyboardWillHideEvent = Keyboard.addListener(
|
|
239
|
+
'keyboardWillHide',
|
|
240
|
+
this._resetKeyboardSpace,
|
|
241
|
+
)
|
|
242
|
+
} else if (Platform.OS === 'android' && this.props.enableOnAndroid) {
|
|
243
|
+
this.keyboardWillShowEvent = Keyboard.addListener(
|
|
244
|
+
'keyboardDidShow',
|
|
245
|
+
this._updateKeyboardSpace,
|
|
246
|
+
)
|
|
247
|
+
this.keyboardWillHideEvent = Keyboard.addListener(
|
|
248
|
+
'keyboardDidHide',
|
|
249
|
+
this._resetKeyboardSpace,
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
supportedKeyboardEvents.forEach((eventName) => {
|
|
254
|
+
const callbackName = keyboardEventToCallbackName(eventName)
|
|
255
|
+
if (this.props[callbackName]) {
|
|
256
|
+
this.callbacks[eventName] = Keyboard.addListener(
|
|
257
|
+
eventName,
|
|
258
|
+
this.props[callbackName],
|
|
259
|
+
)
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
componentDidUpdate(prevProps: KeyboardAwareHOCProps) {
|
|
265
|
+
if (this.props.viewIsInsideTabBar !== prevProps.viewIsInsideTabBar) {
|
|
266
|
+
const keyboardSpace: number = this.props.viewIsInsideTabBar
|
|
267
|
+
? _KAM_DEFAULT_TAB_BAR_HEIGHT
|
|
268
|
+
: 0
|
|
269
|
+
if (this.state.keyboardSpace !== keyboardSpace) {
|
|
270
|
+
this.setState({ keyboardSpace })
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
componentWillUnmount() {
|
|
276
|
+
this.mountedComponent = false
|
|
277
|
+
this.keyboardWillShowEvent && this.keyboardWillShowEvent.remove()
|
|
278
|
+
this.keyboardWillHideEvent && this.keyboardWillHideEvent.remove()
|
|
279
|
+
Object.values(this.callbacks).forEach((callback) => callback.remove(),
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
getScrollResponder = () => {
|
|
284
|
+
return (
|
|
285
|
+
this._rnkasv_keyboardView &&
|
|
286
|
+
this._rnkasv_keyboardView.getScrollResponder &&
|
|
287
|
+
this._rnkasv_keyboardView.getScrollResponder()
|
|
288
|
+
)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
scrollToPosition = (x: number, y: number, animated = true) => {
|
|
292
|
+
const responder = this.getScrollResponder()
|
|
293
|
+
if (!responder) {
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
if (responder.scrollResponderScrollTo) {
|
|
297
|
+
// React Native < 0.65
|
|
298
|
+
responder.scrollResponderScrollTo({ x, y, animated })
|
|
299
|
+
} else if (responder.scrollTo) {
|
|
300
|
+
// React Native >= 0.65
|
|
301
|
+
responder.scrollTo({ x, y, animated })
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
scrollToEnd = (animated = true) => {
|
|
306
|
+
const responder = this.getScrollResponder()
|
|
307
|
+
if (!responder) {
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
if (responder.scrollResponderScrollToEnd) {
|
|
311
|
+
// React Native < 0.65
|
|
312
|
+
responder.scrollResponderScrollToEnd({ animated })
|
|
313
|
+
} else if (responder.scrollToEnd) {
|
|
314
|
+
// React Native >= 0.65
|
|
315
|
+
responder.scrollToEnd({ animated })
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
scrollForExtraHeightOnAndroid = (extraHeight: number) => {
|
|
320
|
+
this.scrollToPosition(0, this.position.y + extraHeight, true)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @param keyboardOpeningTime: takes a different keyboardOpeningTime in consideration.
|
|
325
|
+
* @param extraHeight: takes an extra height in consideration.
|
|
326
|
+
*/
|
|
327
|
+
scrollToFocusedInput = (
|
|
328
|
+
reactNode: any,
|
|
329
|
+
extraHeight?: number,
|
|
330
|
+
keyboardOpeningTime?: number,
|
|
331
|
+
) => {
|
|
332
|
+
if (extraHeight === undefined) {
|
|
333
|
+
extraHeight = this.props.extraHeight || 0
|
|
334
|
+
}
|
|
335
|
+
if (keyboardOpeningTime === undefined) {
|
|
336
|
+
keyboardOpeningTime = this.props.keyboardOpeningTime || 0
|
|
337
|
+
}
|
|
338
|
+
setTimeout(() => {
|
|
339
|
+
if (!this.mountedComponent) {
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
const responder = this.getScrollResponder()
|
|
343
|
+
responder &&
|
|
344
|
+
responder.scrollResponderScrollNativeHandleToKeyboard(
|
|
345
|
+
reactNode,
|
|
346
|
+
extraHeight,
|
|
347
|
+
true,
|
|
348
|
+
)
|
|
349
|
+
}, keyboardOpeningTime)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
scrollIntoView = async (
|
|
353
|
+
element: React.ComponentClass,
|
|
354
|
+
options: ScrollIntoViewOptions = {},
|
|
355
|
+
) => {
|
|
356
|
+
if (!this._rnkasv_keyboardView || !element) {
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const [parentLayout, childLayout] = await Promise.all([
|
|
361
|
+
this._measureElement(this._rnkasv_keyboardView),
|
|
362
|
+
this._measureElement(element),
|
|
363
|
+
])
|
|
364
|
+
|
|
365
|
+
const getScrollPosition =
|
|
366
|
+
options.getScrollPosition || this._defaultGetScrollPosition
|
|
367
|
+
const { x, y, animated } = getScrollPosition(
|
|
368
|
+
parentLayout,
|
|
369
|
+
childLayout,
|
|
370
|
+
this.position,
|
|
371
|
+
)
|
|
372
|
+
this.scrollToPosition(x, y, animated)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
_defaultGetScrollPosition = (
|
|
376
|
+
parentLayout: ElementLayout,
|
|
377
|
+
childLayout: ElementLayout,
|
|
378
|
+
contentOffset: ContentOffset,
|
|
379
|
+
): ScrollPosition => {
|
|
380
|
+
return {
|
|
381
|
+
x: 0,
|
|
382
|
+
y: Math.max(0, childLayout.y - parentLayout.y + contentOffset.y),
|
|
383
|
+
animated: true,
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
_measureElement = (element: React.ComponentClass): Promise<ElementLayout> => {
|
|
388
|
+
const node = findNodeHandle(element)
|
|
389
|
+
|
|
390
|
+
return new Promise((resolve: (el: ElementLayout) => void) => {
|
|
391
|
+
UIManager.measureInWindow(
|
|
392
|
+
node,
|
|
393
|
+
(x: number, y: number, width: number, height: number) => {
|
|
394
|
+
resolve({ x, y, width, height })
|
|
395
|
+
},
|
|
396
|
+
)
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Keyboard actions
|
|
401
|
+
_updateKeyboardSpace = (frames: Parameters<KeyboardEventListener>['0']) => {
|
|
402
|
+
// Automatically scroll to focused TextInput
|
|
403
|
+
if (this.props.enableAutomaticScroll) {
|
|
404
|
+
let keyboardSpace: number =
|
|
405
|
+
frames.endCoordinates.height + this.props.extraScrollHeight
|
|
406
|
+
if (this.props.viewIsInsideTabBar) {
|
|
407
|
+
keyboardSpace -= _KAM_DEFAULT_TAB_BAR_HEIGHT
|
|
408
|
+
}
|
|
409
|
+
this.setState({ keyboardSpace })
|
|
410
|
+
const currentlyFocusedField = TextInput.State.currentlyFocusedInput ?
|
|
411
|
+
findNodeHandle(TextInput.State.currentlyFocusedInput()) : TextInput.State.currentlyFocusedField()
|
|
412
|
+
const responder = this.getScrollResponder()
|
|
413
|
+
if (!currentlyFocusedField || !responder) {
|
|
414
|
+
return
|
|
415
|
+
}
|
|
416
|
+
// @ts-ignore
|
|
417
|
+
UIManager.viewIsDescendantOf(
|
|
418
|
+
currentlyFocusedField,
|
|
419
|
+
responder.getInnerViewNode(),
|
|
420
|
+
(isAncestor: boolean) => {
|
|
421
|
+
if (isAncestor) {
|
|
422
|
+
// Check if the TextInput will be hidden by the keyboard
|
|
423
|
+
UIManager.measureInWindow(
|
|
424
|
+
currentlyFocusedField,
|
|
425
|
+
(x: number, y: number, width: number, height: number) => {
|
|
426
|
+
const textInputBottomPosition = y + height
|
|
427
|
+
const keyboardPosition = frames.endCoordinates.screenY
|
|
428
|
+
const totalExtraHeight =
|
|
429
|
+
this.props.extraScrollHeight + this.props.extraHeight
|
|
430
|
+
if (Platform.OS === 'ios') {
|
|
431
|
+
if (
|
|
432
|
+
textInputBottomPosition >
|
|
433
|
+
keyboardPosition - totalExtraHeight
|
|
434
|
+
) {
|
|
435
|
+
this._scrollToFocusedInputWithNodeHandle(
|
|
436
|
+
currentlyFocusedField,
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
// On android, the system would scroll the text input just
|
|
441
|
+
// above the keyboard so we just neet to scroll the extra
|
|
442
|
+
// height part
|
|
443
|
+
if (textInputBottomPosition > keyboardPosition) {
|
|
444
|
+
// Since the system already scrolled the whole view up
|
|
445
|
+
// we should reduce that amount
|
|
446
|
+
keyboardSpace =
|
|
447
|
+
keyboardSpace -
|
|
448
|
+
(textInputBottomPosition - keyboardPosition)
|
|
449
|
+
this.setState({ keyboardSpace })
|
|
450
|
+
this.scrollForExtraHeightOnAndroid(totalExtraHeight)
|
|
451
|
+
} else if (
|
|
452
|
+
textInputBottomPosition >
|
|
453
|
+
keyboardPosition - totalExtraHeight
|
|
454
|
+
) {
|
|
455
|
+
this.scrollForExtraHeightOnAndroid(
|
|
456
|
+
totalExtraHeight -
|
|
457
|
+
(keyboardPosition - textInputBottomPosition),
|
|
458
|
+
)
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
)
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
)
|
|
466
|
+
}
|
|
467
|
+
if (!this.props.resetScrollToCoords) {
|
|
468
|
+
if (!this.defaultResetScrollToCoords) {
|
|
469
|
+
this.defaultResetScrollToCoords = this.position
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
_resetKeyboardSpace = () => {
|
|
475
|
+
const keyboardSpace: number = this.props.viewIsInsideTabBar
|
|
476
|
+
? _KAM_DEFAULT_TAB_BAR_HEIGHT
|
|
477
|
+
: 0
|
|
478
|
+
this.setState({ keyboardSpace })
|
|
479
|
+
// Reset scroll position after keyboard dismissal
|
|
480
|
+
if (this.props.enableResetScrollToCoords === false) {
|
|
481
|
+
this.defaultResetScrollToCoords = null
|
|
482
|
+
return
|
|
483
|
+
} else if (this.props.resetScrollToCoords) {
|
|
484
|
+
this.scrollToPosition(
|
|
485
|
+
this.props.resetScrollToCoords.x,
|
|
486
|
+
this.props.resetScrollToCoords.y,
|
|
487
|
+
true,
|
|
488
|
+
)
|
|
489
|
+
} else {
|
|
490
|
+
if (this.defaultResetScrollToCoords) {
|
|
491
|
+
this.scrollToPosition(
|
|
492
|
+
this.defaultResetScrollToCoords.x,
|
|
493
|
+
this.defaultResetScrollToCoords.y,
|
|
494
|
+
true,
|
|
495
|
+
)
|
|
496
|
+
this.defaultResetScrollToCoords = null
|
|
497
|
+
} else {
|
|
498
|
+
this.scrollToPosition(0, 0, true)
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
_scrollToFocusedInputWithNodeHandle = (
|
|
504
|
+
nodeID: number,
|
|
505
|
+
extraHeight?: number,
|
|
506
|
+
keyboardOpeningTime?: number,
|
|
507
|
+
) => {
|
|
508
|
+
if (extraHeight === undefined) {
|
|
509
|
+
extraHeight = this.props.extraHeight
|
|
510
|
+
}
|
|
511
|
+
const reactNode = findNodeHandle(nodeID)
|
|
512
|
+
this.scrollToFocusedInput(
|
|
513
|
+
reactNode,
|
|
514
|
+
extraHeight + this.props.extraScrollHeight,
|
|
515
|
+
keyboardOpeningTime !== undefined
|
|
516
|
+
? keyboardOpeningTime
|
|
517
|
+
: this.props.keyboardOpeningTime || 0,
|
|
518
|
+
)
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
_handleOnScroll = (
|
|
522
|
+
e: NativeSyntheticEvent<any> & { nativeEvent: { contentOffset: number } },
|
|
523
|
+
) => {
|
|
524
|
+
this.position = e.nativeEvent.contentOffset
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
_handleRef = (ref: React.ComponentClass<any>) => {
|
|
528
|
+
this._rnkasv_keyboardView = ref ? this.props.hocOptions.extractNativeRef(ref) : ref
|
|
529
|
+
if (this.props.innerRef) {
|
|
530
|
+
this.props.innerRef(this._rnkasv_keyboardView)
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
update = () => {
|
|
535
|
+
const currentlyFocusedField = TextInput.State.currentlyFocusedInput ?
|
|
536
|
+
findNodeHandle(TextInput.State.currentlyFocusedInput()) : TextInput.State.currentlyFocusedField()
|
|
537
|
+
const responder = this.getScrollResponder()
|
|
538
|
+
|
|
539
|
+
if (!currentlyFocusedField || !responder) {
|
|
540
|
+
return
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
this._scrollToFocusedInputWithNodeHandle(currentlyFocusedField)
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
render() {
|
|
547
|
+
const { enableOnAndroid, contentContainerStyle, onScroll, Scrollable } = this.props
|
|
548
|
+
let newContentContainerStyle
|
|
549
|
+
if (Platform.OS === 'android' && enableOnAndroid) {
|
|
550
|
+
newContentContainerStyle = [].concat(contentContainerStyle).concat({
|
|
551
|
+
paddingBottom:
|
|
552
|
+
((contentContainerStyle || {}).paddingBottom || 0) +
|
|
553
|
+
this.state.keyboardSpace,
|
|
554
|
+
})
|
|
555
|
+
}
|
|
556
|
+
const refProps = { [this.props.hocOptions.refPropName]: this._handleRef }
|
|
557
|
+
return (
|
|
558
|
+
<Scrollable
|
|
559
|
+
{...refProps}
|
|
560
|
+
keyboardDismissMode='interactive'
|
|
561
|
+
contentInset={{ bottom: this.state.keyboardSpace }}
|
|
562
|
+
automaticallyAdjustContentInsets={false}
|
|
563
|
+
showsVerticalScrollIndicator={true}
|
|
564
|
+
scrollEventThrottle={1}
|
|
565
|
+
{...this.props}
|
|
566
|
+
contentContainerStyle={
|
|
567
|
+
newContentContainerStyle || contentContainerStyle
|
|
568
|
+
}
|
|
569
|
+
keyboardSpace={this.state.keyboardSpace}
|
|
570
|
+
getScrollResponder={this.getScrollResponder}
|
|
571
|
+
scrollToPosition={this.scrollToPosition}
|
|
572
|
+
scrollToEnd={this.scrollToEnd}
|
|
573
|
+
scrollForExtraHeightOnAndroid={this.scrollForExtraHeightOnAndroid}
|
|
574
|
+
scrollToFocusedInput={this.scrollToFocusedInput}
|
|
575
|
+
scrollIntoView={this.scrollIntoView}
|
|
576
|
+
resetKeyboardSpace={this._resetKeyboardSpace}
|
|
577
|
+
handleOnScroll={this._handleOnScroll}
|
|
578
|
+
update={this.update}
|
|
579
|
+
// @ts-ignore
|
|
580
|
+
onScroll={Animated.forkEvent(onScroll, this._handleOnScroll)}
|
|
581
|
+
/>
|
|
582
|
+
)
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function KeyboardAwareHOC(
|
|
587
|
+
ScrollableComponent: React.ComponentClass<any>,
|
|
588
|
+
userOptions = ScrollIntoViewDefaultOptions,
|
|
589
|
+
) {
|
|
590
|
+
|
|
591
|
+
const hocOptions: KeyboardAwareHOCOptions = {
|
|
592
|
+
...ScrollIntoViewDefaultOptions,
|
|
593
|
+
...userOptions,
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
return React.forwardRef((props, ref) => <KeyboardAwareScrollable
|
|
597
|
+
hocOptions={hocOptions}
|
|
598
|
+
// @ts-ignore
|
|
599
|
+
ref={ref}
|
|
600
|
+
Scrollable={ScrollableComponent}
|
|
601
|
+
{...props}
|
|
602
|
+
/>)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// Allow to pass options, without breaking change, and curried for composition
|
|
606
|
+
// listenToKeyboardEvents(ScrollView);
|
|
607
|
+
// listenToKeyboardEvents(options)(Comp);
|
|
608
|
+
const listenToKeyboardEvents = <C extends React.ComponentType | React.ComponentClass>(configOrComp: C) => {
|
|
609
|
+
//@ts-ignore
|
|
610
|
+
if (typeof configOrComp === 'object' && !configOrComp.displayName) {
|
|
611
|
+
return ((Comp: Function) => KeyboardAwareHOC(Comp as React.ComponentClass, configOrComp)) as unknown as C
|
|
612
|
+
} else {
|
|
613
|
+
//@ts-ignore
|
|
614
|
+
return KeyboardAwareHOC(configOrComp) as C
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
export default listenToKeyboardEvents
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
|
|
3
|
+
export interface KeyboardAwareInterface {
|
|
4
|
+
getScrollResponder: () => void,
|
|
5
|
+
scrollToPosition: (x: number, y: number, animated?: boolean) => void,
|
|
6
|
+
scrollToEnd: (animated?: boolean) => void,
|
|
7
|
+
scrollForExtraHeightOnAndroid: (extraHeight: number) => void,
|
|
8
|
+
scrollToFocusedInput: (
|
|
9
|
+
reactNode: Object,
|
|
10
|
+
extraHeight: number,
|
|
11
|
+
keyboardOpeningTime: number
|
|
12
|
+
) => void
|
|
13
|
+
}
|