-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathFormScreen.tsx
189 lines (167 loc) · 4.45 KB
/
FormScreen.tsx
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
import React, {Component} from 'react';
import {
Button,
Modal,
SafeAreaView,
ScrollView,
Switch,
Text,
TextInput,
View,
} from 'react-native';
import KeyboardManager, {PreviousNextView} from 'react-native-keyboard-manager';
const INPUT_KEYS = [
'input1',
'input2',
'input3',
'input4',
'input5',
'input6',
'textarea1',
'textarea2',
'textarea3',
'textarea4',
];
type StateType = {
modalVisible?: boolean;
enableDisable?: boolean;
inputsValues: {
[key: string]: string;
};
};
class FormScreen extends Component<any, StateType> {
constructor(props: any) {
super(props);
this.state = {
modalVisible: false,
enableDisable: true,
inputsValues: {},
};
}
componentDidMount() {
KeyboardManager.resignFirstResponder();
}
componentDidUpdate() {
KeyboardManager.isKeyboardShowing().then(isShowing => {
console.log('isKeyboardShowing: ' + isShowing);
});
}
handleEnableDisable = (value: boolean) => {
KeyboardManager.setEnable(value);
this.setState({
enableDisable: value,
});
};
handleShowModal = () => {
this.setState({
modalVisible: true,
});
};
handleOtherScreen = () => {
this.props.navigation.navigate('OtherScreen');
};
handleCloseModal = () => {
this.setState({
modalVisible: false,
});
};
getRef<T>(key: string) {
// eslint-disable-next-line react/no-string-refs
return this.refs[key] as T;
}
renderInput = (ref: string, index: number) => {
let nextRef = INPUT_KEYS[index + 1];
const nextFocus = () => {
if (!nextRef) {
return;
}
this.getRef<TextInput>(nextRef)?.focus();
};
const multiline = ref.startsWith('textarea');
return (
<View key={ref} style={{padding: 10}}>
<Text>{ref}</Text>
<TextInput
style={{
minHeight: 40,
borderColor: '#000000',
borderWidth: 1,
borderRadius: 2,
paddingLeft: 5,
// maxHeight is recommended for multiline, to prevent infinite grown
maxHeight: multiline ? 300 : undefined,
}}
ref={ref}
value={this.state.inputsValues[ref] || ''}
onChangeText={text => {
this.setState(previous => ({
inputsValues: {
...previous.inputsValues,
[ref]: text,
},
}));
}}
placeholder={ref}
onSubmitEditing={!multiline ? nextFocus : undefined}
multiline={multiline}
numberOfLines={multiline ? 10 : 1}
returnKeyType={multiline ? 'default' : 'next'}
onLayout={() => {
// When multiline=true and the input height changes, it updates the keyboard position.
KeyboardManager.reloadLayoutIfNeeded();
}}
/>
</View>
);
};
renderContent = () => {
return (
<ScrollView>
<View style={{alignItems: 'center'}}>
<Text style={{marginTop: 10, textAlign: 'center'}}>
React-Native Keyboard Manager
</Text>
<View
style={{
marginTop: 10,
flexDirection: 'row',
alignItems: 'center',
}}>
<Text>Enable/Disable </Text>
<Switch
onValueChange={this.handleEnableDisable}
value={this.state.enableDisable}
/>
</View>
</View>
<View>{INPUT_KEYS.map(this.renderInput)}</View>
</ScrollView>
);
};
render() {
return (
<View style={{flex: 1}}>
<SafeAreaView style={{flex: 1}}>
<Button onPress={this.handleShowModal} title="Show in modal" />
<Button
onPress={this.handleOtherScreen}
title="Navigate to other screen"
/>
{this.renderContent()}
<Modal
onRequestClose={this.handleCloseModal}
visible={this.state.modalVisible}>
<SafeAreaView style={{flex: 1, backgroundColor: '#e1e1e1'}}>
<Button onPress={this.handleCloseModal} title="Close modal" />
{/* Inside a native modal the inputs need to be wrapped inside PreviousNextView */}
<PreviousNextView style={{flex: 1}}>
{this.renderContent()}
</PreviousNextView>
</SafeAreaView>
</Modal>
</SafeAreaView>
</View>
);
}
}
export default FormScreen;