PliWould/components/UnitChooser.tsx

74 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-07-01 04:49:41 +02:00
import { Length } from "convert";
import { useState } from "react";
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,
defaultValue? : Length,
2024-07-01 04:49:41 +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";
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 (
<TouchableHighlight
2024-07-01 04:49:41 +02:00
onPress={() => doChoiceClicked(ci)}
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: {
flexDirection: "row",
verticalAlign: "middle",
2024-07-01 04:49:41 +02:00
},
active: {
backgroundColor: "skyblue",
padding: 5,
borderRadius: 5,
2024-07-01 04:49:41 +02:00
},
default: {
backgroundColor: "lightgray",
padding: 5,
borderRadius: 5,
verticalAlign: "middle",
},
textActive: {
marginTop: 2,
marginBottom: 2,
marginLeft: 10,
marginRight: 10,
fontSize: 25,
},
textDefault: {
marginTop: 2,
marginBottom: 2,
marginLeft: 10,
marginRight: 10,
fontSize: 25,
},
unitButton: {
},
2024-07-01 04:49:41 +02:00
})