2024-07-01 04:49:41 +02:00
|
|
|
import { Length } from "convert";
|
|
|
|
import { useState } from "react";
|
2024-07-02 17:34:23 +02:00
|
|
|
import { Button, StyleSheet, Text, TouchableHighlight, View } from "react-native";
|
2024-07-01 04:49:41 +02:00
|
|
|
|
|
|
|
export type UnitChooserProps = {
|
|
|
|
choices: Length[],
|
|
|
|
onChoicePressed: (l: Length) => any,
|
|
|
|
activeColor?: string,
|
|
|
|
defaultColor?: string,
|
2024-07-02 17:34:23 +02:00
|
|
|
defaultValue? : Length,
|
2024-07-01 04:49:41 +02:00
|
|
|
}
|
|
|
|
|
2024-07-02 17:34:23 +02:00
|
|
|
export default function UnitChooser({ choices, onChoicePressed, activeColor, defaultColor, defaultValue }: UnitChooserProps) {
|
|
|
|
const [value, setValue] = useState(defaultValue || choices[0] as Length);
|
2024-07-01 04:49:41 +02:00
|
|
|
|
|
|
|
activeColor = activeColor || "lightblue";
|
2024-07-01 21:23:45 +02:00
|
|
|
defaultColor = defaultColor || "lightgrey";
|
2024-07-01 04:49:41 +02:00
|
|
|
|
|
|
|
function doChoiceClicked(choice: Length) {
|
|
|
|
setValue(choice);
|
|
|
|
onChoicePressed(choice);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.unitChooser}>
|
|
|
|
{choices.map((ci) => {
|
|
|
|
return (
|
2024-07-02 17:34:23 +02:00
|
|
|
<TouchableHighlight
|
2024-07-01 04:49:41 +02:00
|
|
|
onPress={() => doChoiceClicked(ci)}
|
2024-07-02 17:34:23 +02:00
|
|
|
style={value === ci ? styles.active : styles.default }
|
|
|
|
key={ci}
|
|
|
|
>
|
|
|
|
<Text style={value === ci ? styles.textActive : styles.textDefault}>{ci}</Text>
|
|
|
|
</TouchableHighlight>
|
2024-07-01 04:49:41 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
unitChooser: {
|
2024-07-02 17:34:23 +02:00
|
|
|
flexDirection: "row",
|
|
|
|
verticalAlign: "middle",
|
2024-07-01 04:49:41 +02:00
|
|
|
},
|
|
|
|
active: {
|
2024-07-02 17:34:23 +02:00
|
|
|
backgroundColor: "skyblue",
|
|
|
|
padding: 5,
|
|
|
|
borderRadius: 5,
|
2024-07-01 04:49:41 +02:00
|
|
|
},
|
|
|
|
default: {
|
2024-07-02 17:34:23 +02:00
|
|
|
backgroundColor: "lightgray",
|
|
|
|
padding: 5,
|
|
|
|
borderRadius: 5,
|
|
|
|
verticalAlign: "middle",
|
|
|
|
},
|
|
|
|
textActive: {
|
|
|
|
marginTop: 2,
|
|
|
|
marginBottom: 2,
|
|
|
|
marginLeft: 10,
|
|
|
|
marginRight: 10,
|
|
|
|
fontSize: 40,
|
|
|
|
},
|
|
|
|
textDefault: {
|
|
|
|
marginTop: 2,
|
|
|
|
marginBottom: 2,
|
|
|
|
marginLeft: 10,
|
|
|
|
marginRight: 10,
|
|
|
|
fontSize: 40,
|
|
|
|
},
|
|
|
|
unitButton: {
|
|
|
|
},
|
2024-07-01 04:49:41 +02:00
|
|
|
})
|