|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { |
| 3 | + Modal, |
| 4 | + StyleSheet, |
| 5 | + Text, |
| 6 | + TextInput, |
| 7 | + TouchableOpacity, |
| 8 | + View, |
| 9 | +} from "react-native"; |
| 10 | +import { KeyboardAvoidingView } from "react-native-keyboard-controller"; |
| 11 | + |
| 12 | +type Behavior = "padding" | "height" | "position"; |
| 13 | +const behaviors: Behavior[] = ["padding", "height", "position"]; |
| 14 | + |
| 15 | +function KAVContent({ |
| 16 | + behavior, |
| 17 | + keyboardVerticalOffset, |
| 18 | + automaticOffset, |
| 19 | +}: { |
| 20 | + behavior: Behavior; |
| 21 | + keyboardVerticalOffset: number; |
| 22 | + automaticOffset: boolean; |
| 23 | +}) { |
| 24 | + return ( |
| 25 | + // @ts-expect-error discriminated union doesn't narrow with dynamic behavior |
| 26 | + <KeyboardAvoidingView |
| 27 | + automaticOffset={automaticOffset} |
| 28 | + behavior={behavior} |
| 29 | + contentContainerStyle={ |
| 30 | + behavior === "position" ? styles.container : undefined |
| 31 | + } |
| 32 | + keyboardVerticalOffset={keyboardVerticalOffset} |
| 33 | + style={styles.content} |
| 34 | + > |
| 35 | + <View style={styles.inner}> |
| 36 | + <Text style={styles.heading}>Header</Text> |
| 37 | + <View style={styles.inputs}> |
| 38 | + <TextInput |
| 39 | + placeholder="Username" |
| 40 | + placeholderTextColor="#7C7C7C" |
| 41 | + style={styles.textInput} |
| 42 | + /> |
| 43 | + <TextInput |
| 44 | + secureTextEntry |
| 45 | + placeholder="Password" |
| 46 | + placeholderTextColor="#7C7C7C" |
| 47 | + style={styles.textInput} |
| 48 | + /> |
| 49 | + </View> |
| 50 | + <TouchableOpacity style={styles.button}> |
| 51 | + <Text style={styles.buttonText}>Submit</Text> |
| 52 | + </TouchableOpacity> |
| 53 | + </View> |
| 54 | + </KeyboardAvoidingView> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +export default function KeyboardAvoidingViewAutomaticExample() { |
| 59 | + const [behavior, setBehavior] = useState<Behavior>(behaviors[0]); |
| 60 | + const [automaticOffset, setAutomaticOffset] = useState(true); |
| 61 | + const [showModal, setShowModal] = useState(false); |
| 62 | + const [offset, setOffset] = useState(0); |
| 63 | + const offsets = [0, 50, 100]; |
| 64 | + |
| 65 | + return ( |
| 66 | + <> |
| 67 | + <View style={styles.settings}> |
| 68 | + <TouchableOpacity |
| 69 | + style={styles.settingsButton} |
| 70 | + onPress={() => { |
| 71 | + const index = behaviors.indexOf(behavior); |
| 72 | + |
| 73 | + setBehavior( |
| 74 | + behaviors[index === behaviors.length - 1 ? 0 : index + 1], |
| 75 | + ); |
| 76 | + }} |
| 77 | + > |
| 78 | + <Text style={styles.settingsText}>{behavior}</Text> |
| 79 | + </TouchableOpacity> |
| 80 | + <TouchableOpacity |
| 81 | + style={styles.settingsButton} |
| 82 | + onPress={() => { |
| 83 | + const index = offsets.indexOf(offset); |
| 84 | + |
| 85 | + setOffset(offsets[index === offsets.length - 1 ? 0 : index + 1]); |
| 86 | + }} |
| 87 | + > |
| 88 | + <Text style={styles.settingsText}>+{offset}</Text> |
| 89 | + </TouchableOpacity> |
| 90 | + <TouchableOpacity |
| 91 | + style={styles.settingsButton} |
| 92 | + onPress={() => setAutomaticOffset((v) => !v)} |
| 93 | + > |
| 94 | + <Text style={styles.settingsText}> |
| 95 | + {automaticOffset ? "Auto" : "Manual"} |
| 96 | + </Text> |
| 97 | + </TouchableOpacity> |
| 98 | + <TouchableOpacity |
| 99 | + style={styles.settingsButton} |
| 100 | + onPress={() => setShowModal(true)} |
| 101 | + > |
| 102 | + <Text style={styles.settingsText}>Modal</Text> |
| 103 | + </TouchableOpacity> |
| 104 | + </View> |
| 105 | + <KAVContent |
| 106 | + automaticOffset={automaticOffset} |
| 107 | + behavior={behavior} |
| 108 | + keyboardVerticalOffset={offset} |
| 109 | + /> |
| 110 | + <Modal |
| 111 | + animationType="slide" |
| 112 | + presentationStyle="pageSheet" |
| 113 | + visible={showModal} |
| 114 | + onRequestClose={() => setShowModal(false)} |
| 115 | + > |
| 116 | + <View style={styles.modalHeader}> |
| 117 | + <TouchableOpacity onPress={() => setShowModal(false)}> |
| 118 | + <Text style={styles.closeButton}>Close</Text> |
| 119 | + </TouchableOpacity> |
| 120 | + <Text style={styles.modalTitle}> |
| 121 | + Modal ({automaticOffset ? "Auto" : "Manual"}) |
| 122 | + </Text> |
| 123 | + </View> |
| 124 | + <KAVContent |
| 125 | + automaticOffset={automaticOffset} |
| 126 | + behavior={behavior} |
| 127 | + keyboardVerticalOffset={offset} |
| 128 | + /> |
| 129 | + </Modal> |
| 130 | + </> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +const styles = StyleSheet.create({ |
| 135 | + container: { |
| 136 | + flex: 1, |
| 137 | + }, |
| 138 | + content: { |
| 139 | + flex: 1, |
| 140 | + }, |
| 141 | + heading: { |
| 142 | + fontSize: 36, |
| 143 | + marginBottom: 48, |
| 144 | + fontWeight: "600", |
| 145 | + }, |
| 146 | + inner: { |
| 147 | + padding: 24, |
| 148 | + flex: 1, |
| 149 | + justifyContent: "space-between", |
| 150 | + }, |
| 151 | + inputs: {}, |
| 152 | + textInput: { |
| 153 | + height: 44, |
| 154 | + borderColor: "#000000", |
| 155 | + borderWidth: 1, |
| 156 | + borderRadius: 10, |
| 157 | + marginBottom: 36, |
| 158 | + paddingLeft: 10, |
| 159 | + }, |
| 160 | + button: { |
| 161 | + marginTop: 12, |
| 162 | + height: 44, |
| 163 | + borderRadius: 10, |
| 164 | + backgroundColor: "rgb(40, 64, 147)", |
| 165 | + justifyContent: "center", |
| 166 | + alignItems: "center", |
| 167 | + }, |
| 168 | + buttonText: { |
| 169 | + fontWeight: "500", |
| 170 | + fontSize: 16, |
| 171 | + color: "white", |
| 172 | + }, |
| 173 | + settings: { |
| 174 | + flexDirection: "row", |
| 175 | + gap: 8, |
| 176 | + padding: 8, |
| 177 | + }, |
| 178 | + settingsButton: { |
| 179 | + paddingHorizontal: 12, |
| 180 | + paddingVertical: 8, |
| 181 | + borderRadius: 8, |
| 182 | + backgroundColor: "#E8E8E8", |
| 183 | + }, |
| 184 | + settingsText: { |
| 185 | + fontSize: 14, |
| 186 | + fontWeight: "500", |
| 187 | + }, |
| 188 | + modalHeader: { |
| 189 | + flexDirection: "row", |
| 190 | + alignItems: "center", |
| 191 | + padding: 16, |
| 192 | + }, |
| 193 | + closeButton: { |
| 194 | + color: "#007AFF", |
| 195 | + fontSize: 16, |
| 196 | + }, |
| 197 | + modalTitle: { |
| 198 | + fontSize: 16, |
| 199 | + fontWeight: "600", |
| 200 | + flex: 1, |
| 201 | + textAlign: "center", |
| 202 | + marginRight: 40, |
| 203 | + }, |
| 204 | +}); |
0 commit comments