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