|
| 1 | +import { View, Text, TextInput, StyleSheet } from "react-native"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import type { FieldRendererProps } from "@ybyra/react"; |
| 4 | +import { useTheme } from "../theme/context"; |
| 5 | +import type { Theme } from "../theme/default"; |
| 6 | +import { ds } from "../support/ds"; |
| 7 | + |
| 8 | +export function CurrencyField({ domain, name, value, config, proxy, errors, onChange, onBlur, onFocus }: FieldRendererProps) { |
| 9 | + const { t } = useTranslation(); |
| 10 | + const theme = useTheme(); |
| 11 | + const styles = createStyles(theme); |
| 12 | + if (proxy.hidden) return null; |
| 13 | + |
| 14 | + const fieldLabel = t(`${domain}.fields.${name}`, { defaultValue: name }); |
| 15 | + const prefix = (config.attrs.prefix as string) ?? ""; |
| 16 | + |
| 17 | + return ( |
| 18 | + <View style={styles.container} {...ds(`CurrencyField:${name}`)}> |
| 19 | + <Text style={styles.label}>{fieldLabel}</Text> |
| 20 | + <View style={styles.inputWrapper}> |
| 21 | + {prefix ? <Text style={styles.prefix}>{prefix}</Text> : null} |
| 22 | + <TextInput |
| 23 | + style={[styles.input, prefix ? styles.inputWithPrefix : null, proxy.disabled && styles.inputDisabled, errors.length > 0 && styles.inputError]} |
| 24 | + value={value !== undefined && value !== null ? String(value) : ""} |
| 25 | + onChangeText={(text) => { |
| 26 | + const num = Number(text); |
| 27 | + onChange(isNaN(num) ? text : num); |
| 28 | + }} |
| 29 | + onBlur={onBlur} |
| 30 | + onFocus={onFocus} |
| 31 | + editable={!proxy.disabled} |
| 32 | + keyboardType="decimal-pad" |
| 33 | + placeholderTextColor={theme.colors.mutedForeground} |
| 34 | + /> |
| 35 | + </View> |
| 36 | + <View style={styles.errorSlot}> |
| 37 | + {errors.map((error, i) => ( |
| 38 | + <Text key={i} style={styles.error}>{error}</Text> |
| 39 | + ))} |
| 40 | + </View> |
| 41 | + </View> |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +const createStyles = (theme: Theme) => StyleSheet.create({ |
| 46 | + container: { |
| 47 | + paddingHorizontal: theme.spacing.xs, |
| 48 | + }, |
| 49 | + label: { |
| 50 | + fontSize: theme.fontSize.sm, |
| 51 | + fontWeight: theme.fontWeight.semibold, |
| 52 | + marginBottom: theme.spacing.xs, |
| 53 | + color: theme.colors.foreground, |
| 54 | + }, |
| 55 | + inputWrapper: { |
| 56 | + flexDirection: "row", |
| 57 | + alignItems: "center", |
| 58 | + }, |
| 59 | + prefix: { |
| 60 | + position: "absolute", |
| 61 | + left: theme.spacing.md, |
| 62 | + fontSize: theme.fontSize.md, |
| 63 | + color: theme.colors.mutedForeground, |
| 64 | + zIndex: 1, |
| 65 | + }, |
| 66 | + input: { |
| 67 | + flex: 1, |
| 68 | + borderWidth: 1, |
| 69 | + borderColor: theme.colors.input, |
| 70 | + borderRadius: theme.borderRadius.md, |
| 71 | + paddingHorizontal: theme.spacing.md, |
| 72 | + paddingVertical: 10, |
| 73 | + fontSize: theme.fontSize.md, |
| 74 | + backgroundColor: theme.colors.card, |
| 75 | + color: theme.colors.cardForeground, |
| 76 | + }, |
| 77 | + inputWithPrefix: { |
| 78 | + paddingLeft: 36, |
| 79 | + }, |
| 80 | + inputDisabled: { |
| 81 | + backgroundColor: theme.colors.muted, |
| 82 | + color: theme.colors.mutedForeground, |
| 83 | + }, |
| 84 | + inputError: { |
| 85 | + borderColor: theme.colors.destructive, |
| 86 | + }, |
| 87 | + errorSlot: { |
| 88 | + minHeight: 20, |
| 89 | + marginTop: 2, |
| 90 | + }, |
| 91 | + error: { |
| 92 | + fontSize: theme.fontSize.xs, |
| 93 | + color: theme.colors.destructive, |
| 94 | + }, |
| 95 | +}); |
0 commit comments