2023-08-12 03:22:50 +00:00
|
|
|
import React, { useCallback, useMemo, useState } from "react";
|
|
|
|
import { View } from "react-native";
|
|
|
|
import { Button, Menu, Subheading } from "react-native-paper";
|
|
|
|
import { ITEM_PADDING } from "./constants";
|
2022-11-16 05:17:59 +00:00
|
|
|
|
|
|
|
export interface Item {
|
2023-08-12 03:22:50 +00:00
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
color?: string;
|
2022-11-16 05:17:59 +00:00
|
|
|
}
|
2022-11-01 03:06:25 +00:00
|
|
|
|
2022-12-30 06:39:35 +00:00
|
|
|
function Select({
|
2022-11-01 03:06:25 +00:00
|
|
|
value,
|
|
|
|
onChange,
|
2022-11-15 06:51:15 +00:00
|
|
|
items,
|
2022-11-30 02:15:19 +00:00
|
|
|
label,
|
2022-11-01 03:06:25 +00:00
|
|
|
}: {
|
2023-08-12 03:22:50 +00:00
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
items: Item[];
|
|
|
|
label?: string;
|
2022-11-01 03:06:25 +00:00
|
|
|
}) {
|
2023-08-12 03:22:50 +00:00
|
|
|
const [show, setShow] = useState(false);
|
2022-11-16 05:17:59 +00:00
|
|
|
|
|
|
|
const selected = useMemo(
|
2023-06-27 03:16:59 +00:00
|
|
|
() => items.find((item) => item.value === value) || items[0],
|
2023-08-12 03:22:50 +00:00
|
|
|
[items, value]
|
|
|
|
);
|
2022-11-16 05:17:59 +00:00
|
|
|
|
|
|
|
const handlePress = useCallback(
|
|
|
|
(newValue: string) => {
|
2023-08-12 03:22:50 +00:00
|
|
|
onChange(newValue);
|
|
|
|
setShow(false);
|
2022-11-16 05:17:59 +00:00
|
|
|
},
|
2023-08-12 03:22:50 +00:00
|
|
|
[onChange]
|
|
|
|
);
|
2022-11-01 03:06:25 +00:00
|
|
|
|
|
|
|
return (
|
2022-11-30 02:15:19 +00:00
|
|
|
<View
|
|
|
|
style={{
|
2023-08-12 03:22:50 +00:00
|
|
|
flexDirection: "row",
|
|
|
|
alignItems: "center",
|
2022-12-01 02:51:39 +00:00
|
|
|
paddingLeft: ITEM_PADDING,
|
2023-06-27 03:16:59 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{label && <Subheading style={{ width: 100 }}>{label}</Subheading>}
|
2022-11-30 02:15:19 +00:00
|
|
|
<Menu
|
|
|
|
visible={show}
|
|
|
|
onDismiss={() => setShow(false)}
|
|
|
|
anchor={
|
|
|
|
<Button
|
|
|
|
onPress={() => setShow(true)}
|
|
|
|
style={{
|
2023-08-12 03:22:50 +00:00
|
|
|
alignSelf: "flex-start",
|
2023-06-27 03:16:59 +00:00
|
|
|
}}
|
|
|
|
>
|
2022-11-30 02:15:19 +00:00
|
|
|
{selected?.label}
|
|
|
|
</Button>
|
2023-06-27 03:16:59 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{items.map((item) => (
|
2022-11-30 02:15:19 +00:00
|
|
|
<Menu.Item
|
2023-07-17 06:38:28 +00:00
|
|
|
titleStyle={{ color: item.color }}
|
2022-11-30 02:15:19 +00:00
|
|
|
key={item.value}
|
|
|
|
title={item.label}
|
|
|
|
onPress={() => handlePress(item.value)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Menu>
|
|
|
|
</View>
|
2023-08-12 03:22:50 +00:00
|
|
|
);
|
2022-11-01 03:06:25 +00:00
|
|
|
}
|
2022-12-30 06:39:35 +00:00
|
|
|
|
2023-08-12 03:22:50 +00:00
|
|
|
export default React.memo(Select);
|