PliWould/components/MeasurementInput.tsx

67 lines
1.7 KiB
TypeScript
Raw Normal View History

import { dimensions_t, length_t } from "@/lib/dimensions_t";
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,
units?: Length,
2024-07-01 04:49:41 +02:00
}
export function MeasurementInput({onValueSet, defaultValue, label, units}: MeasurementInputProps) {
const [mValue, setMValue] = useState(defaultValue)
const defValue = Number.isNaN(defaultValue.l) ? 0 : defaultValue.l
units = units || "ft";
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}>{units}</Text>
2024-07-01 04:49:41 +02:00
</View>
)
}
const styles = StyleSheet.create({
inputWrapper: {
alignItems: "flex-start",
flexDirection: "row",
verticalAlign: "middle"
},
2024-07-01 04:49:41 +02:00
unitHints: {
padding: 10,
fontSize: 20,
verticalAlign: "middle",
2024-07-01 04:49:41 +02:00
},
lengthInput: {
borderWidth: 1,
borderRadius: 4,
borderColor: "grey",
padding: 4,
margin: 4,
fontSize: 25,
2024-07-01 04:49:41 +02:00
},
})