From 85832105b4e6a860d34374cfa7e6c01a06cc54a3 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 3 Jan 2025 23:58:02 +0800 Subject: [PATCH] fix(rn): rsshub form styles Signed-off-by: Innei --- .../src/components/ui/form/PickerIos.tsx | 94 +++++++++++++++++++ apps/mobile/src/components/ui/form/Select.tsx | 60 +++++------- .../src/components/ui/form/TextField.tsx | 38 +++++--- .../src/screens/(modal)/rsshub-form.tsx | 7 +- 4 files changed, 147 insertions(+), 52 deletions(-) create mode 100644 apps/mobile/src/components/ui/form/PickerIos.tsx diff --git a/apps/mobile/src/components/ui/form/PickerIos.tsx b/apps/mobile/src/components/ui/form/PickerIos.tsx new file mode 100644 index 000000000..5c871f276 --- /dev/null +++ b/apps/mobile/src/components/ui/form/PickerIos.tsx @@ -0,0 +1,94 @@ +/* eslint-disable @eslint-react/no-array-index-key */ +import { cn } from "@follow/utils" +import { Portal } from "@gorhom/portal" +import { Picker } from "@react-native-picker/picker" +import { useMemo, useState } from "react" +import type { StyleProp, ViewStyle } from "react-native" +import { Pressable, Text, View } from "react-native" +import Animated, { SlideOutDown } from "react-native-reanimated" +import { useEventCallback } from "usehooks-ts" + +import { MingcuteDownLineIcon } from "@/src/icons/mingcute_down_line" +import { useColor } from "@/src/theme/colors" + +import { BlurEffect } from "../../common/HeaderBlur" + +interface PickerIosProps { + options: { label: string; value: T }[] + + value: T + onValueChange: (value: T) => void + + wrapperClassName?: string + wrapperStyle?: StyleProp +} +export function PickerIos({ + options, + value, + onValueChange, + wrapperClassName, + wrapperStyle, +}: PickerIosProps) { + const [isOpen, setIsOpen] = useState(false) + + const [currentValue, setCurrentValue] = useState(() => { + if (!value) { + return options[0].value + } + return value + }) + + const valueToLabelMap = useMemo(() => { + return options.reduce((acc, option) => { + acc.set(option.value, option.label) + return acc + }, new Map()) + }, [options]) + + const handleChangeValue = useEventCallback((value: T) => { + setCurrentValue(value) + onValueChange(value) + }) + + const systemFill = useColor("text") + + return ( + <> + {/* Trigger */} + setIsOpen(!isOpen)}> + + {valueToLabelMap.get(currentValue)} + + + + + + {/* Picker */} + {isOpen && ( + + setIsOpen(false)} + className="absolute inset-0 flex flex-row items-end" + > + + + e.stopPropagation()}> + + {options.map((option, index) => ( + + ))} + + + + + + )} + + ) +} diff --git a/apps/mobile/src/components/ui/form/Select.tsx b/apps/mobile/src/components/ui/form/Select.tsx index e894d8afa..1ce3ccc56 100644 --- a/apps/mobile/src/components/ui/form/Select.tsx +++ b/apps/mobile/src/components/ui/form/Select.tsx @@ -1,17 +1,14 @@ -/* eslint-disable @eslint-react/no-array-index-key */ import { cn } from "@follow/utils" -import { Portal } from "@gorhom/portal" -import { Picker } from "@react-native-picker/picker" import { useMemo, useState } from "react" import type { StyleProp, ViewStyle } from "react-native" -import { Pressable, Text, View } from "react-native" -import Animated, { SlideOutDown } from "react-native-reanimated" +import { Text, View } from "react-native" +import ContextMenu from "react-native-context-menu-view" import { useEventCallback } from "usehooks-ts" import { MingcuteDownLineIcon } from "@/src/icons/mingcute_down_line" import { useColor } from "@/src/theme/colors" -import { BlurEffect } from "../../common/HeaderBlur" +import { FormLabel } from "./Label" interface SelectProps { options: { label: string; value: T }[] @@ -21,6 +18,8 @@ interface SelectProps { wrapperClassName?: string wrapperStyle?: StyleProp + + label?: string } export function Select({ options, @@ -28,9 +27,8 @@ export function Select({ onValueChange, wrapperClassName, wrapperStyle, + label, }: SelectProps) { - const [isOpen, setIsOpen] = useState(false) - const [currentValue, setCurrentValue] = useState(() => { if (!value) { return options[0].value @@ -51,43 +49,37 @@ export function Select({ }) const systemFill = useColor("text") + return ( - <> + + {!!label && } + {/* Trigger */} - setIsOpen(!isOpen)}> + ({ + title: option.label, + selected: option.value === currentValue, + }))} + onPress={(e) => { + const { index } = e.nativeEvent + handleChangeValue(options[index].value) + }} + > {valueToLabelMap.get(currentValue)} - + - - {/* Picker */} - {isOpen && ( - - setIsOpen(false)} - className="absolute inset-0 flex flex-row items-end" - > - - - e.stopPropagation()}> - - {options.map((option, index) => ( - - ))} - - - - - - )} - + + ) } diff --git a/apps/mobile/src/components/ui/form/TextField.tsx b/apps/mobile/src/components/ui/form/TextField.tsx index f53005fe3..0fa45cf2a 100644 --- a/apps/mobile/src/components/ui/form/TextField.tsx +++ b/apps/mobile/src/components/ui/form/TextField.tsx @@ -3,28 +3,36 @@ import { forwardRef } from "react" import type { StyleProp, TextInputProps, ViewStyle } from "react-native" import { StyleSheet, TextInput, View } from "react-native" +import { FormLabel } from "./Label" + interface TextFieldProps { wrapperClassName?: string wrapperStyle?: StyleProp + + label?: string + required?: boolean } export const TextField = forwardRef( - ({ className, style, wrapperClassName, wrapperStyle, ...rest }, ref) => { + ({ className, style, wrapperClassName, wrapperStyle, label, required, ...rest }, ref) => { return ( - - - + <> + {!!label && } + + + + ) }, ) diff --git a/apps/mobile/src/screens/(modal)/rsshub-form.tsx b/apps/mobile/src/screens/(modal)/rsshub-form.tsx index 0e69c7ce2..ae3277be2 100644 --- a/apps/mobile/src/screens/(modal)/rsshub-form.tsx +++ b/apps/mobile/src/screens/(modal)/rsshub-form.tsx @@ -12,7 +12,6 @@ import { z } from "zod" import { HeaderTitleExtra } from "@/src/components/common/HeaderTitleExtra" import { ModalHeaderCloseButton } from "@/src/components/common/ModalSharedComponents" import { FormProvider, useFormContext } from "@/src/components/ui/form/FormProvider" -import { FormLabel } from "@/src/components/ui/form/Label" import { Select } from "@/src/components/ui/form/Select" import { TextField } from "@/src/components/ui/form/TextField" import MarkdownWeb from "@/src/components/ui/typography/MarkdownWeb" @@ -121,7 +120,6 @@ function FormImpl({ route, routePrefix, name }: RsshubFormParams) { return ( - {!parameters?.options && ( ( {parameters.description} + {parameters.description} )} )