PliWould/components/AreaInput.tsx

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-07-01 04:49:41 +02:00
import { MeasurementInput } from "./MeasurementInput";
import { area_t, dimensions_t } from "@/lib/product";
import { Length } from "convert";
2024-07-01 04:49:41 +02:00
import { useState } from "react";
import { StyleSheet, Text, View } from "react-native";
2024-07-01 04:49:41 +02:00
export type AreaInputProps = {
onMeasurementSet?: (area : dimensions_t) => any,
2024-07-01 17:05:24 +02:00
defaultValue?: area_t,
lengthLabel?: string,
widthLabel?: string,
units?: Length,
2024-07-01 04:49:41 +02:00
}
export function AreaInput({onMeasurementSet, lengthLabel, widthLabel, defaultValue, units} : AreaInputProps) {
2024-07-01 04:49:41 +02:00
defaultValue = defaultValue || {l: 0, w: 0, u: "ft"}
units = units || "ft"
2024-07-01 17:05:24 +02:00
const [area, setArea] = useState(defaultValue)
2024-07-01 04:49:41 +02:00
function doOnLengthSet(measurement : dimensions_t) {
setArea({
...area,
l: measurement.l
});
2024-07-01 17:05:24 +02:00
onMeasurementSet && onMeasurementSet({
...area,
l: measurement.l
});
2024-07-01 04:49:41 +02:00
}
function doOnWidthSet(measurement : dimensions_t) {
setArea({
...area,
w: measurement.l
});
2024-07-01 17:05:24 +02:00
onMeasurementSet && onMeasurementSet({
...area,
w: measurement.l
});
2024-07-01 04:49:41 +02:00
}
return (
<View style={styles.areaInputWrapper}>
2024-07-01 04:49:41 +02:00
<MeasurementInput
defaultValue={{l: area.l, u: area.u}}
2024-07-01 04:49:41 +02:00
onValueSet={doOnLengthSet}
2024-07-01 17:05:24 +02:00
label={lengthLabel}
units={units}
2024-07-01 04:49:41 +02:00
/>
<Text style={{fontSize: 30,}} > x </Text>
2024-07-01 04:49:41 +02:00
<MeasurementInput
defaultValue={{l: area.w, u: area.u}}
2024-07-01 04:49:41 +02:00
onValueSet={doOnWidthSet}
2024-07-01 17:05:24 +02:00
label={widthLabel}
units={units}
2024-07-01 04:49:41 +02:00
/>
</View>
)
}
const styles = StyleSheet.create({
areaInputWrapper: {
flexDirection: "row",
verticalAlign: "middle",
}
})