refactor(mobile): Optimize header and form layouts
- Simplify NavigationHeader component by removing complex title centering logic - Remove fixed width constraints in TimelineSelectorProvider header actions - Add KeyboardAvoidingView to RSSHub form text inputs - Conditionally render form sections based on available keys Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -3,13 +3,12 @@ import { router, useNavigation } from "expo-router"
|
||||
import type { FC, PropsWithChildren, ReactNode } from "react"
|
||||
import { createElement, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"
|
||||
import type { LayoutChangeEvent } from "react-native"
|
||||
import { StyleSheet, TouchableOpacity, useWindowDimensions, View } from "react-native"
|
||||
import { StyleSheet, TouchableOpacity, View } from "react-native"
|
||||
import type { AnimatedProps } from "react-native-reanimated"
|
||||
import Animated, {
|
||||
useAnimatedRef,
|
||||
useAnimatedStyle,
|
||||
useSharedValue,
|
||||
withSpring,
|
||||
withTiming,
|
||||
} from "react-native-reanimated"
|
||||
import type { DefaultStyle } from "react-native-reanimated/lib/typescript/hook/commonTypes"
|
||||
@@ -211,27 +210,6 @@ export const NavigationHeader = ({
|
||||
})
|
||||
}, [animatedRef, setHeaderHeight])
|
||||
|
||||
const [headerLeftWidth, setHeaderLeftWidth] = useState(0)
|
||||
|
||||
const titleBarWidth = useWindowDimensions().width
|
||||
const [titleWidth, setTitleWidth] = useState(0)
|
||||
|
||||
const titleShouldCenterTransformX = useMemo(() => {
|
||||
if (!titleWidth) return 0
|
||||
const halfTitleWidth = titleWidth / 2
|
||||
const currentTitleCenterX =
|
||||
titlebarPaddingHorizontal + headerLeftWidth + titleMarginHorizontal + halfTitleWidth
|
||||
const centerX = titleBarWidth / 2
|
||||
|
||||
const transformX = centerX - currentTitleCenterX
|
||||
return transformX
|
||||
}, [titleBarWidth, titleWidth, headerLeftWidth])
|
||||
const titleTransformX = useSharedValue(0)
|
||||
|
||||
useEffect(() => {
|
||||
titleTransformX.value = withSpring(titleShouldCenterTransformX)
|
||||
}, [titleShouldCenterTransformX, titleTransformX])
|
||||
|
||||
return (
|
||||
<Animated.View
|
||||
ref={animatedRef}
|
||||
@@ -263,32 +241,28 @@ export const NavigationHeader = ({
|
||||
>
|
||||
{/* Left */}
|
||||
<View
|
||||
className="min-w-6 flex-row items-center justify-start"
|
||||
className="min-w-6 flex-1 flex-row items-center justify-start"
|
||||
pointerEvents={"box-none"}
|
||||
onLayout={useCallback((e: LayoutChangeEvent) => {
|
||||
setHeaderLeftWidth(e.nativeEvent.layout.width)
|
||||
}, [])}
|
||||
>
|
||||
<HeaderLeft canGoBack={canBack} />
|
||||
</View>
|
||||
{/* Center */}
|
||||
|
||||
<Animated.View
|
||||
onLayout={(e: LayoutChangeEvent) => {
|
||||
setTitleWidth(e.nativeEvent.layout.width)
|
||||
}}
|
||||
className="flex-1 items-center justify-center"
|
||||
className="items-center justify-center"
|
||||
pointerEvents={"box-none"}
|
||||
style={{
|
||||
marginHorizontal: titleMarginHorizontal,
|
||||
transform: [{ translateX: titleTransformX }],
|
||||
}}
|
||||
>
|
||||
{headerTitleAbsolute ? <View /> : headerTitle}
|
||||
</Animated.View>
|
||||
|
||||
{/* Right */}
|
||||
<View className="min-w-6 flex-row items-center justify-end" pointerEvents={"box-none"}>
|
||||
<View
|
||||
className="min-w-6 flex-1 flex-row items-center justify-end"
|
||||
pointerEvents={"box-none"}
|
||||
>
|
||||
<RightButton canGoBack={canBack} />
|
||||
</View>
|
||||
<View
|
||||
|
||||
@@ -20,7 +20,6 @@ import { getFeed } from "@/src/store/feed/getter"
|
||||
|
||||
import { useEntryListContext, useSelectedFeedTitle } from "./atoms"
|
||||
|
||||
const HEADER_ACTIONS_GROUP_WIDTH = 60
|
||||
export function TimelineSelectorProvider({ children }: { children: React.ReactNode }) {
|
||||
const scrollY = useAnimatedValue(0)
|
||||
const viewTitle = useSelectedFeedTitle()
|
||||
@@ -37,16 +36,8 @@ export function TimelineSelectorProvider({ children }: { children: React.ReactNo
|
||||
headerLeft={useMemo(
|
||||
() =>
|
||||
isTimeline || isSubscriptions
|
||||
? () => (
|
||||
<View style={{ width: HEADER_ACTIONS_GROUP_WIDTH }}>
|
||||
<HomeLeftAction />
|
||||
</View>
|
||||
)
|
||||
: () => (
|
||||
<View style={{ width: HEADER_ACTIONS_GROUP_WIDTH }}>
|
||||
<DefaultHeaderBackButton canGoBack={true} />
|
||||
</View>
|
||||
),
|
||||
? () => <HomeLeftAction />
|
||||
: () => <DefaultHeaderBackButton canGoBack={true} />,
|
||||
[isTimeline, isSubscriptions],
|
||||
)}
|
||||
headerRight={useMemo(() => {
|
||||
@@ -69,16 +60,7 @@ export function TimelineSelectorProvider({ children }: { children: React.ReactNo
|
||||
})()
|
||||
|
||||
if (Component)
|
||||
return () => (
|
||||
<View
|
||||
style={{
|
||||
width: HEADER_ACTIONS_GROUP_WIDTH,
|
||||
}}
|
||||
className="flex-row items-center justify-end"
|
||||
>
|
||||
{Component()}
|
||||
</View>
|
||||
)
|
||||
return () => <View className="flex-row items-center justify-end">{Component()}</View>
|
||||
return
|
||||
}, [isFeed, isTimeline, isSubscriptions, params])}
|
||||
headerHideableBottom={isTimeline || isSubscriptions ? TimelineViewSelector : undefined}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { router, Stack, useLocalSearchParams } from "expo-router"
|
||||
import { memo, useEffect, useMemo, useState } from "react"
|
||||
import type { FieldErrors } from "react-hook-form"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { Linking, Text, TouchableOpacity, View } from "react-native"
|
||||
import { KeyboardAvoidingView, Linking, Text, TouchableOpacity, View } from "react-native"
|
||||
import { z } from "zod"
|
||||
|
||||
import { BlurEffectWithBottomBorder } from "@/src/components/common/BlurEffect"
|
||||
@@ -127,64 +127,68 @@ function FormImpl({ route, routePrefix, name }: RsshubFormParams) {
|
||||
|
||||
<PortalHost>
|
||||
<SafeModalScrollView className="bg-system-grouped-background">
|
||||
<View className="bg-secondary-system-grouped-background mx-2 mt-2 gap-4 rounded-lg px-3 py-6">
|
||||
{keys.map((keyItem) => {
|
||||
const parameters = normalizeRSSHubParameters(route.parameters[keyItem.name]!)
|
||||
{keys.length > 0 && (
|
||||
<View className="bg-secondary-system-grouped-background mx-2 mt-2 gap-4 rounded-lg px-3 py-6">
|
||||
{keys.map((keyItem) => {
|
||||
const parameters = normalizeRSSHubParameters(route.parameters[keyItem.name]!)
|
||||
|
||||
return (
|
||||
<View key={keyItem.name}>
|
||||
{!parameters?.options && (
|
||||
<Controller
|
||||
name={keyItem.name}
|
||||
control={form.control}
|
||||
rules={{
|
||||
required: !keyItem.optional,
|
||||
// validate: (value) => {
|
||||
// return dynamicFormSchema.safeParse({
|
||||
// [keyItem.name]: value,
|
||||
// }).success
|
||||
// },
|
||||
}}
|
||||
render={({ field: { onChange, onBlur, ref, value } }) => (
|
||||
<TextField
|
||||
label={keyItem.name}
|
||||
required={!keyItem.optional}
|
||||
wrapperClassName="mt-2"
|
||||
placeholder={formPlaceholder[keyItem.name]}
|
||||
onBlur={onBlur}
|
||||
onChangeText={onChange}
|
||||
defaultValue={defaultValue[keyItem.name] ?? ""}
|
||||
value={value ?? ""}
|
||||
ref={ref}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
return (
|
||||
<View key={keyItem.name}>
|
||||
{!parameters?.options && (
|
||||
<Controller
|
||||
name={keyItem.name}
|
||||
control={form.control}
|
||||
rules={{
|
||||
required: !keyItem.optional,
|
||||
// validate: (value) => {
|
||||
// return dynamicFormSchema.safeParse({
|
||||
// [keyItem.name]: value,
|
||||
// }).success
|
||||
// },
|
||||
}}
|
||||
render={({ field: { onChange, onBlur, ref, value } }) => (
|
||||
<KeyboardAvoidingView behavior="padding">
|
||||
<TextField
|
||||
label={keyItem.name}
|
||||
required={!keyItem.optional}
|
||||
wrapperClassName="mt-2"
|
||||
placeholder={formPlaceholder[keyItem.name]}
|
||||
onBlur={onBlur}
|
||||
onChangeText={onChange}
|
||||
defaultValue={defaultValue[keyItem.name] ?? ""}
|
||||
value={value ?? ""}
|
||||
ref={ref}
|
||||
/>
|
||||
</KeyboardAvoidingView>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
|
||||
{!!parameters?.options && (
|
||||
<Controller
|
||||
name={keyItem.name}
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<Select
|
||||
label={keyItem.name}
|
||||
options={parameters.options ?? []}
|
||||
value={value}
|
||||
onValueChange={onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
{!!parameters?.options && (
|
||||
<Controller
|
||||
name={keyItem.name}
|
||||
control={form.control}
|
||||
render={({ field: { onChange, value } }) => (
|
||||
<Select
|
||||
label={keyItem.name}
|
||||
options={parameters.options ?? []}
|
||||
value={value}
|
||||
onValueChange={onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
|
||||
{!!parameters && (
|
||||
<Text className="text-secondary-label ml-2 mt-1 text-xs">
|
||||
{parameters.description}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
)
|
||||
})}
|
||||
</View>
|
||||
{!!parameters && (
|
||||
<Text className="text-secondary-label ml-2 mt-1 text-xs">
|
||||
{parameters.description}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
)
|
||||
})}
|
||||
</View>
|
||||
)}
|
||||
<Maintainers maintainers={route.maintainers} />
|
||||
|
||||
{!!route.description && (
|
||||
|
||||
Reference in New Issue
Block a user