PliWould/components/MeasurementInput.tsx

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-07-01 04:49:41 +02:00
import { dimensions_t, length_t } from "@/lib/product";
2024-07-01 17:05:24 +02:00
import { Length } from "convert";
2024-07-01 04:49:41 +02:00
import { StyleSheet, Text, TextInput, View } from "react-native";
export type t_length_unit = "foot" | "inch"
export type MeasurementInputProps = {
onValueSet?: (d: dimensions_t) => any,
2024-07-01 17:05:24 +02:00
defaultValue: length_t;
label?: string,
2024-07-01 04:49:41 +02:00
}
2024-07-01 17:05:24 +02:00
export function MeasurementInput({onValueSet, defaultValue, label}: MeasurementInputProps) {
1
2024-07-01 04:49:41 +02:00
function doOnValueSet(value : string) {
2024-07-01 17:05:24 +02:00
const iVal = parseFloat(value) || parseInt(value);
2024-07-01 04:49:41 +02:00
onValueSet && onValueSet({
2024-07-01 17:05:24 +02:00
...defaultValue,
l: iVal,
2024-07-01 04:49:41 +02:00
})
}
2024-07-01 17:05:24 +02:00
const sDefValue = new String(defaultValue.l).valueOf()
2024-07-01 04:49:41 +02:00
return (
<View>
<TextInput
clearTextOnFocus={true}
2024-07-01 17:05:24 +02:00
defaultValue={sDefValue}
2024-07-01 04:49:41 +02:00
onChangeText={doOnValueSet}
inputMode='decimal'
2024-07-01 17:05:24 +02:00
style={styles.lengthInput}
aria-label={label || "Enter measurement"}
/>
<Text style={styles.unitHints}>{defaultValue.u}</Text>
2024-07-01 04:49:41 +02:00
</View>
)
}
const styles = StyleSheet.create({
unitHints: {
fontSize: 30,
padding: 10,
},
lengthInput: {
borderWidth: 1,
borderRadius: 4,
borderColor: "grey",
padding: 4,
margin: 4,
fontSize: 30,
width: 200,
},
})