-
-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathindex.tsx
More file actions
96 lines (84 loc) · 2.88 KB
/
Copy pathindex.tsx
File metadata and controls
96 lines (84 loc) · 2.88 KB
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
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Alert, Button, Text, View } from "react-native";
import {
KeyboardAwareScrollView,
KeyboardStickyView,
} from "react-native-keyboard-controller";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import TextInput from "../../../components/TextInput";
import { styles } from "./styles";
import type { ExamplesStackParamList } from "../../../navigation/ExamplesStack";
import type { StackScreenProps } from "@react-navigation/stack";
import type { LayoutChangeEvent } from "react-native";
type Props = StackScreenProps<ExamplesStackParamList>;
const variants = ["v1", "v2", "v3"] as const;
type Variant = (typeof variants)[number];
export default function AwareScrollViewStickyFooter({ navigation }: Props) {
const { bottom } = useSafeAreaInsets();
const [footerHeight, setFooterHeight] = useState(0);
const [variant, setVariant] = useState<Variant>("v1");
const handleLayout = useCallback((evt: LayoutChangeEvent) => {
setFooterHeight(evt.nativeEvent.layout.height);
}, []);
const offset = useMemo(
() => ({ closed: 0, opened: variant === "v1" ? 0 : bottom }),
[bottom, variant],
);
const offsetV3 = useMemo(
() => ({ closed: -50, opened: bottom - 25 }),
[bottom],
);
useEffect(() => {
navigation.setOptions({
headerRight: () => (
<Text
style={styles.header}
onPress={() => {
const index = variants.indexOf(variant);
setVariant(variants[index === variants.length - 1 ? 0 : index + 1]);
}}
>
{variant}
</Text>
),
});
}, [variant]);
const v1v2 = variant === "v1" || variant === "v2";
return (
<View
style={[
styles.pageContainer,
{ paddingBottom: variant === "v1" ? 0 : bottom },
]}
>
<KeyboardAwareScrollView
bottomOffset={(v1v2 ? footerHeight : 0) + 50}
contentContainerStyle={styles.content}
keyboardShouldPersistTaps="handled"
style={styles.container}
>
{new Array(10).fill(0).map((_, i) => (
<TextInput
key={i}
keyboardType={i % 2 === 0 ? "numeric" : "default"}
placeholder={`TextInput#${i}`}
/>
))}
</KeyboardAwareScrollView>
{v1v2 && (
<KeyboardStickyView offset={offset}>
<View style={styles.footer} onLayout={handleLayout}>
<Text style={styles.footerText}>A mocked sticky footer</Text>
<TextInput placeholder="Amount" style={styles.inputInFooter} />
<Button title="Click me" onPress={() => Alert.alert("Clicked")} />
</View>
</KeyboardStickyView>
)}
{variant === "v3" && (
<KeyboardStickyView offset={offsetV3}>
<View style={styles.circle} />
</KeyboardStickyView>
)}
</View>
);
}