1// Basically the same as React Native's text input, but the long press bug is
2// fixed. Look at KBTextInput(Manager) in the android folder.
3
4import React from 'react'
5import {
6  requireNativeComponent,
7  Text,
8  TextInput as RNTextInput,
9  UIManager,
10  TouchableWithoutFeedback,
11} from 'react-native'
12import {isAndroid} from '../constants/platform'
13
14let KBInputText = RNTextInput
15
16if (isAndroid) {
17  let NativeTextInput: any
18  class _KBInputText extends RNTextInput {
19    render() {
20      // @ts-ignore we added this
21      const {forwardedRef} = this.props
22      if (!isAndroid) {
23        return <RNTextInput {...this.props} ref={forwardedRef} />
24      }
25      const p = {
26        ...this.props,
27      }
28
29      p.style = [this.props.style]
30      // @ts-ignore TS doesn't know about this method
31      p.autoCapitalize = UIManager.getViewManagerConfig('AndroidTextInput').Constants.AutoCapitalizationType[
32        p.autoCapitalize || 'sentences'
33      ]
34
35      let children = p.children
36      let childCount = 0
37      React.Children.forEach(children, () => ++childCount)
38      if (childCount > 1) {
39        children = <Text>{children}</Text>
40      }
41
42      if (p.selection && p.selection.end == null) {
43        p.selection = {
44          end: p.selection.start,
45          start: p.selection.start,
46        }
47      }
48      if (!NativeTextInput) {
49        NativeTextInput = isAndroid ? requireNativeComponent('KBTextInput') : {}
50      }
51      const textContainer = (
52        <NativeTextInput
53          // @ts-ignore This neets
54          ref={this._setNativeRef}
55          {...p}
56          // Default p.allowFontScaling will true from RNTextInput
57          allowFontScaling={p.allowFontScaling}
58          mostRecentEventCount={0}
59          text={p.value || p.defaultValue || ''}
60          children={children}
61          disableFullscreenUI={p.disableFullscreenUI}
62          textBreakStrategy={p.textBreakStrategy}
63          // @ts-ignore no RN types
64          onFocus={this._onFocus}
65          // @ts-ignore no RN types
66          onBlur={this._onBlur}
67          // @ts-ignore no RN types
68          onChange={this._onChange}
69          // @ts-ignore no RN types
70          onSelectionChange={this._onSelectionChange}
71          // @ts-ignore no RN types
72          onTextInput={this._onTextInput}
73          // @ts-ignore no RN types
74          onScroll={this._onScroll}
75        />
76      )
77
78      return (
79        <TouchableWithoutFeedback
80          onLayout={p.onLayout}
81          accessible={p.accessible}
82          accessibilityLabel={p.accessibilityLabel}
83          // @ts-ignore
84          onPress={this._onPress}
85          accessibilityRole={p.accessibilityRole}
86          accessibilityStates={p.accessibilityStates}
87          // @ts-ignore no RN types
88          nativeID={this.props.nativeID}
89          testID={this.props.testID}
90        >
91          {textContainer}
92        </TouchableWithoutFeedback>
93      )
94    }
95  }
96  KBInputText = _KBInputText
97}
98
99export default (KBInputText as unknown) as typeof RNTextInput
100