PliWould/components/MeasurementInput.tsx

62 lines
1.6 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";
import { useState } from "react";
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) {
const [mValue, setMValue] = useState(defaultValue)
const defValue = Number.isNaN(defaultValue.l) ? 0 : defaultValue.l
2024-07-01 04:49:41 +02:00
function doOnValueSet(value : string) {
setMValue(mValue);
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
})
}
const sDefValue = new String(defValue).valueOf()
2024-07-01 17:05:24 +02:00
2024-07-01 04:49:41 +02:00
return (
<View style={styles.inputWrapper}>
2024-07-01 04:49:41 +02:00
<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}>{mValue.u}</Text>
2024-07-01 04:49:41 +02:00
</View>
)
}
const styles = StyleSheet.create({
inputWrapper: {
alignItems: "flex-start",
flexDirection: "row"
},
2024-07-01 04:49:41 +02:00
unitHints: {
padding: 10,
},
lengthInput: {
borderWidth: 1,
borderRadius: 4,
borderColor: "grey",
padding: 4,
margin: 4,
fontSize: 25,
width: 100,
2024-07-01 04:49:41 +02:00
},
})