import {useState, useEffect} from "react" import {View, Platform, Pressable, TextInput, ViewStyle, TextStyle} from "react-native" import {Text} from "@/components/ignite" import {useAppTheme} from "@/contexts/ThemeContext" import {ThemedStyle} from "@/theme" import showAlert from "@/utils/AlertUtils" type NumberSettingProps = { label: string value: number min?: number max?: number step?: number placeholder?: string onValueChange: (value: number) => void containerStyle?: ViewStyle isFirst?: boolean isLast?: boolean } const NumberSetting: React.FC = ({ label, value, min, max, step = 1, placeholder = "Enter number...", onValueChange, containerStyle, isFirst, isLast, }) => { const {theme, themed} = useAppTheme() const groupedStyle: ViewStyle | undefined = isFirst !== undefined || isLast !== undefined ? { borderTopLeftRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1, borderTopRightRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1, borderBottomLeftRadius: isLast ? theme.spacing.s4 : theme.spacing.s1, borderBottomRightRadius: isLast ? theme.spacing.s4 : theme.spacing.s1, marginBottom: isLast ? 0 : theme.spacing.s2, } : undefined const [localValue, setLocalValue] = useState(value.toString()) const [_isEditing, setIsEditing] = useState(false) // Update local value when prop changes useEffect(() => { setLocalValue(value.toString()) }, [value]) const validateAndUpdate = (text: string) => { // Allow empty string for editing if (text === "") { setLocalValue(text) return } // Check if it's a valid number const numValue = parseFloat(text) if (isNaN(numValue)) { return // Don't update if not a valid number } // Apply min/max constraints let finalValue = numValue if (min !== undefined && numValue < min) { finalValue = min } if (max !== undefined && numValue > max) { finalValue = max } // Apply step constraint if (step !== 1) { finalValue = Math.round(finalValue / step) * step } setLocalValue(finalValue.toString()) onValueChange(finalValue) } const handleSubmit = () => { if (localValue === "") { // Reset to current value if empty setLocalValue(value.toString()) setIsEditing(false) return } const numValue = parseFloat(localValue) if (isNaN(numValue)) { showAlert("Invalid Input", "Please enter a valid number.") setLocalValue(value.toString()) setIsEditing(false) return } validateAndUpdate(localValue) setIsEditing(false) } const handleBlur = () => { handleSubmit() } const handleFocus = () => { setIsEditing(true) } const increment = () => { const newValue = value + step validateAndUpdate(newValue.toString()) } const decrement = () => { const newValue = value - step validateAndUpdate(newValue.toString()) } return ( {label} - = max}> + {(min !== undefined || max !== undefined) && ( {min !== undefined && max !== undefined ? `Range: ${min} - ${max}` : min !== undefined ? `Min: ${min}` : `Max: ${max}`} )} ) } const $container: ThemedStyle = ({colors, spacing}) => ({ backgroundColor: colors.primary_foreground, borderRadius: spacing.s4, paddingVertical: spacing.s4, paddingHorizontal: spacing.s4, width: "100%", }) const $label: ThemedStyle = ({colors, spacing}) => ({ fontSize: 14, fontWeight: "600", color: colors.text, marginBottom: spacing.s2, }) const $inputContainer: ThemedStyle = ({spacing}) => ({ flexDirection: "row", alignItems: "center", gap: spacing.s2, }) const $input: ThemedStyle = ({colors, spacing}) => ({ flex: 1, fontSize: 16, color: colors.text, backgroundColor: colors.primary_foreground, borderWidth: 1, borderColor: colors.border, borderRadius: 6, paddingHorizontal: spacing.s3, paddingVertical: spacing.s2, textAlign: "center", minHeight: Platform.OS === "ios" ? 44 : 48, }) const $decrementButton: ThemedStyle = ({colors}) => ({ backgroundColor: colors.primary_foreground, borderWidth: 1, borderColor: colors.border, borderRadius: 6, width: 44, height: 44, justifyContent: "center", alignItems: "center", }) const $incrementButton: ThemedStyle = ({colors}) => ({ backgroundColor: colors.primary_foreground, borderWidth: 1, borderColor: colors.border, borderRadius: 6, width: 44, height: 44, justifyContent: "center", alignItems: "center", }) const $buttonText: ThemedStyle = ({colors}) => ({ fontSize: 18, fontWeight: "600", color: colors.text, }) const $constraintsText: ThemedStyle = ({colors, spacing}) => ({ fontSize: 12, color: colors.textDim, marginTop: spacing.s2, textAlign: "center", }) export default NumberSetting