The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { Button, HStack, Picker, Popover, Spacer, Text, VStack } from "@expo/ui/swift-ui"; |
| 2 | import { |
| 3 | accessibilityLabel, |
| 4 | buttonBorderShape, |
| 5 | buttonStyle, |
| 6 | disabled, |
| 7 | font, |
| 8 | foregroundStyle, |
| 9 | frame, |
| 10 | labelStyle, |
| 11 | padding, |
| 12 | pickerStyle, |
| 13 | presentationBackground, |
| 14 | tag, |
| 15 | tint, |
| 16 | } from "@expo/ui/swift-ui/modifiers"; |
| 17 | import { useGT } from "gt-react-native"; |
| 18 | import { useState } from "react"; |
| 19 | |
| 20 | import { NativeThemeHost } from "./NativeThemeHost"; |
| 21 | import { useControlTheme } from "./useControlTheme"; |
| 22 | import type { NumberFieldProps } from "./NumberField.types"; |
| 23 | |
| 24 | export function NumberField(props: NumberFieldProps) { |
| 25 | const gt = useGT(); |
| 26 | const { colors, fontSize } = useControlTheme(); |
| 27 | const [open, setOpen] = useState(false); |
| 28 | const [draft, setDraft] = useState(props.value); |
| 29 | const values = Array.from({ length: props.max - props.min + 1 }, (_, index) => props.min + index); |
| 30 | if (!values.includes(draft)) values.push(draft); |
| 31 | values.sort((a, b) => a - b); |
| 32 | return ( |
| 33 | <NativeThemeHost matchContents> |
| 34 | <Popover isPresented={open} onIsPresentedChange={setOpen}> |
| 35 | <Popover.Trigger> |
| 36 | <Button |
| 37 | onPress={() => { |
| 38 | if (props.disabled) return; |
| 39 | setDraft(props.value); |
| 40 | setOpen(true); |
| 41 | }} |
| 42 | modifiers={[ |
| 43 | buttonStyle("bordered"), |
| 44 | disabled(props.disabled), |
| 45 | accessibilityLabel( |
| 46 | `${props.label}: ${props.formatValue?.(props.value) ?? props.value}`, |
| 47 | ), |
| 48 | frame({ minWidth: 64, minHeight: 44 }), |
| 49 | foregroundStyle(colors["--color-primary-text"]), |
| 50 | font({ size: fontSize }), |
| 51 | ]} |
| 52 | > |
| 53 | <Text>{String(props.value)}</Text> |
| 54 | </Button> |
| 55 | </Popover.Trigger> |
| 56 | <Popover.Content> |
| 57 | <VStack |
| 58 | modifiers={[ |
| 59 | padding({ all: 12 }), |
| 60 | frame({ width: 240 }), |
| 61 | presentationBackground(colors["--color-sheet-solid"]), |
| 62 | ]} |
| 63 | > |
| 64 | <Picker |
| 65 | label={props.label} |
| 66 | selection={draft} |
| 67 | onSelectionChange={setDraft} |
| 68 | modifiers={[pickerStyle("wheel"), frame({ height: 180 })]} |
| 69 | > |
| 70 | {values.map((value) => ( |
| 71 | <Text |
| 72 | key={value} |
| 73 | modifiers={[tag(value), foregroundStyle(colors["--color-foreground"])]} |
| 74 | > |
| 75 | {props.formatValue?.(value) ?? String(value)} |
| 76 | </Text> |
| 77 | ))} |
| 78 | </Picker> |
| 79 | <HStack> |
| 80 | <Button |
| 81 | label={gt("Cancel")} |
| 82 | systemImage="xmark" |
| 83 | onPress={() => setOpen(false)} |
| 84 | modifiers={[ |
| 85 | labelStyle("iconOnly"), |
| 86 | buttonStyle("bordered"), |
| 87 | buttonBorderShape("circle"), |
| 88 | tint(colors["--color-foreground"]), |
| 89 | ]} |
| 90 | /> |
| 91 | <Spacer /> |
| 92 | <Button |
| 93 | label={gt("Done")} |
| 94 | systemImage="checkmark" |
| 95 | onPress={() => { |
| 96 | setOpen(false); |
| 97 | if (!props.disabled && draft !== props.value) props.onValueChange(draft); |
| 98 | }} |
| 99 | modifiers={[ |
| 100 | labelStyle("iconOnly"), |
| 101 | buttonStyle("borderedProminent"), |
| 102 | buttonBorderShape("circle"), |
| 103 | tint(colors["--color-prominent-action"]), |
| 104 | foregroundStyle(colors["--color-prominent-action-foreground"]), |
| 105 | ]} |
| 106 | /> |
| 107 | </HStack> |
| 108 | </VStack> |
| 109 | </Popover.Content> |
| 110 | </Popover> |
| 111 | </NativeThemeHost> |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | export type { NumberFieldProps } from "./NumberField.types"; |