tadd select component
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import ScanQRCode from "@/components/ScanQRCode";
|
import ScanQRCode from "@/components/ScanQRCode";
|
||||||
|
import Select from "@/components/Select";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import {
|
import {
|
||||||
Platform,
|
Platform,
|
||||||
@@ -21,13 +22,32 @@ export default function Sensor() {
|
|||||||
const handleScanPress = () => {
|
const handleScanPress = () => {
|
||||||
setScanModalVisible(true);
|
setScanModalVisible(true);
|
||||||
};
|
};
|
||||||
|
const [selectedValue, setSelectedValue] = useState<
|
||||||
|
string | number | undefined
|
||||||
|
>(undefined);
|
||||||
|
|
||||||
|
const options = [
|
||||||
|
{ label: "Apple", value: "apple" },
|
||||||
|
{ label: "Banana", value: "banana" },
|
||||||
|
{ label: "Cherry", value: "cherry", disabled: true },
|
||||||
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView style={{ flex: 1 }}>
|
<SafeAreaView style={{ flex: 1 }}>
|
||||||
<ScrollView contentContainerStyle={styles.scrollContent}>
|
<ScrollView contentContainerStyle={styles.scrollContent}>
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Text style={styles.titleText}>Cảm biến trên tàu</Text>
|
<Text style={styles.titleText}>Cảm biến trên tàu</Text>
|
||||||
|
<Select
|
||||||
|
style={{ width: "80%", marginBottom: 20 }}
|
||||||
|
options={options}
|
||||||
|
value={selectedValue}
|
||||||
|
onChange={(val) => {
|
||||||
|
setSelectedValue(val);
|
||||||
|
console.log("Value: ", val);
|
||||||
|
}}
|
||||||
|
placeholder="Select a fruit"
|
||||||
|
allowClear
|
||||||
|
/>
|
||||||
<Pressable style={styles.scanButton} onPress={handleScanPress}>
|
<Pressable style={styles.scanButton} onPress={handleScanPress}>
|
||||||
<Text style={styles.scanButtonText}>📱 Scan QR Code</Text>
|
<Text style={styles.scanButtonText}>📱 Scan QR Code</Text>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { ThemedText } from "@/components/themed-text";
|
import { ThemedText } from "@/components/themed-text";
|
||||||
import { ThemedView } from "@/components/themed-view";
|
import { ThemedView } from "@/components/themed-view";
|
||||||
import { showToastError } from "@/config";
|
|
||||||
import { TOKEN } from "@/constants";
|
import { TOKEN } from "@/constants";
|
||||||
import { login } from "@/controller/AuthController";
|
import { login } from "@/controller/AuthController";
|
||||||
|
import { showErrorToast } from "@/services/toast_service";
|
||||||
import {
|
import {
|
||||||
getStorageItem,
|
getStorageItem,
|
||||||
removeStorageItem,
|
removeStorageItem,
|
||||||
@@ -57,7 +57,7 @@ export default function LoginScreen() {
|
|||||||
const handleLogin = async () => {
|
const handleLogin = async () => {
|
||||||
// Validate input
|
// Validate input
|
||||||
if (!username.trim() || !password.trim()) {
|
if (!username.trim() || !password.trim()) {
|
||||||
showToastError("Lỗi", "Vui lòng nhập tài khoản và mật khẩu");
|
showErrorToast("Vui lòng nhập tài khoản và mật khẩu");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,8 +81,7 @@ export default function LoginScreen() {
|
|||||||
router.replace("/(tabs)");
|
router.replace("/(tabs)");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToastError(
|
showErrorToast(
|
||||||
"Lỗi",
|
|
||||||
error instanceof Error ? error.message : "Đăng nhập thất bại"
|
error instanceof Error ? error.message : "Đăng nhập thất bại"
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ interface StartButtonProps {
|
|||||||
onPress?: () => void;
|
onPress?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface a {
|
||||||
|
fishingLogs?: Model.FishingLogInfo[] | null;
|
||||||
|
onCallback?: (fishingLogs: Model.FishingLogInfo[]) => void;
|
||||||
|
isEditing?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
const ButtonCreateNewHaulOrTrip: React.FC<StartButtonProps> = ({
|
const ButtonCreateNewHaulOrTrip: React.FC<StartButtonProps> = ({
|
||||||
gpsData,
|
gpsData,
|
||||||
onPress,
|
onPress,
|
||||||
|
|||||||
272
components/Select.tsx
Normal file
272
components/Select.tsx
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
import { AntDesign } from "@expo/vector-icons";
|
||||||
|
import React, { useEffect, useState } from "react";
|
||||||
|
import {
|
||||||
|
ActivityIndicator,
|
||||||
|
ScrollView,
|
||||||
|
StyleProp,
|
||||||
|
StyleSheet,
|
||||||
|
Text,
|
||||||
|
TextInput,
|
||||||
|
TouchableOpacity,
|
||||||
|
View,
|
||||||
|
ViewStyle,
|
||||||
|
} from "react-native";
|
||||||
|
|
||||||
|
export interface SelectOption {
|
||||||
|
label: string;
|
||||||
|
value: string | number;
|
||||||
|
disabled?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SelectProps {
|
||||||
|
value?: string | number;
|
||||||
|
defaultValue?: string | number;
|
||||||
|
options: SelectOption[];
|
||||||
|
onChange?: (value: string | number | undefined) => void;
|
||||||
|
placeholder?: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
loading?: boolean;
|
||||||
|
allowClear?: boolean;
|
||||||
|
showSearch?: boolean;
|
||||||
|
mode?: "single" | "multiple"; // multiple not implemented yet
|
||||||
|
style?: StyleProp<ViewStyle>;
|
||||||
|
size?: "small" | "middle" | "large";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Select
|
||||||
|
* A Select component inspired by Ant Design, adapted for React Native.
|
||||||
|
* Supports single selection, search, clear, loading, disabled states.
|
||||||
|
*/
|
||||||
|
const Select: React.FC<SelectProps> = ({
|
||||||
|
value,
|
||||||
|
defaultValue,
|
||||||
|
options,
|
||||||
|
onChange,
|
||||||
|
placeholder = "Select an option",
|
||||||
|
disabled = false,
|
||||||
|
loading = false,
|
||||||
|
allowClear = false,
|
||||||
|
showSearch = false,
|
||||||
|
mode = "single",
|
||||||
|
style,
|
||||||
|
size = "middle",
|
||||||
|
}) => {
|
||||||
|
const [selectedValue, setSelectedValue] = useState<
|
||||||
|
string | number | undefined
|
||||||
|
>(value ?? defaultValue);
|
||||||
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
const [searchText, setSearchText] = useState("");
|
||||||
|
const [containerHeight, setContainerHeight] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setSelectedValue(value);
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
|
const filteredOptions = showSearch
|
||||||
|
? options.filter((opt) =>
|
||||||
|
opt.label.toLowerCase().includes(searchText.toLowerCase())
|
||||||
|
)
|
||||||
|
: options;
|
||||||
|
|
||||||
|
const selectedOption = options.find((opt) => opt.value === selectedValue);
|
||||||
|
|
||||||
|
const handleSelect = (val: string | number) => {
|
||||||
|
setSelectedValue(val);
|
||||||
|
onChange?.(val);
|
||||||
|
setIsOpen(false);
|
||||||
|
setSearchText("");
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleClear = () => {
|
||||||
|
setSelectedValue(undefined);
|
||||||
|
onChange?.(undefined);
|
||||||
|
};
|
||||||
|
|
||||||
|
const sizeMap = {
|
||||||
|
small: { height: 32, fontSize: 14, paddingHorizontal: 10 },
|
||||||
|
middle: { height: 40, fontSize: 16, paddingHorizontal: 14 },
|
||||||
|
large: { height: 48, fontSize: 18, paddingHorizontal: 18 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const sz = sizeMap[size];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={styles.wrapper}>
|
||||||
|
<TouchableOpacity
|
||||||
|
style={[
|
||||||
|
styles.container,
|
||||||
|
{
|
||||||
|
height: sz.height,
|
||||||
|
paddingHorizontal: sz.paddingHorizontal,
|
||||||
|
opacity: disabled ? 0.6 : 1,
|
||||||
|
},
|
||||||
|
style,
|
||||||
|
]}
|
||||||
|
onPress={() => !disabled && !loading && setIsOpen(!isOpen)}
|
||||||
|
disabled={disabled || loading}
|
||||||
|
activeOpacity={0.8}
|
||||||
|
onLayout={(e) => setContainerHeight(e.nativeEvent.layout.height)}
|
||||||
|
>
|
||||||
|
<View style={styles.content}>
|
||||||
|
{loading ? (
|
||||||
|
<ActivityIndicator size="small" color="#4ecdc4" />
|
||||||
|
) : (
|
||||||
|
<Text
|
||||||
|
style={[
|
||||||
|
styles.text,
|
||||||
|
{
|
||||||
|
fontSize: sz.fontSize,
|
||||||
|
color: selectedValue ? "#111" : "#999",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
numberOfLines={1}
|
||||||
|
>
|
||||||
|
{selectedOption?.label || placeholder}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
<View style={styles.suffix}>
|
||||||
|
{allowClear && selectedValue && !loading ? (
|
||||||
|
<TouchableOpacity onPress={handleClear} style={styles.icon}>
|
||||||
|
<AntDesign name="close" size={16} color="#999" />
|
||||||
|
</TouchableOpacity>
|
||||||
|
) : null}
|
||||||
|
<AntDesign
|
||||||
|
name={isOpen ? "up" : "down"}
|
||||||
|
size={14}
|
||||||
|
color="#999"
|
||||||
|
style={styles.arrow}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</TouchableOpacity>
|
||||||
|
|
||||||
|
{isOpen && (
|
||||||
|
<View style={[styles.dropdown, { top: containerHeight }]}>
|
||||||
|
{showSearch && (
|
||||||
|
<TextInput
|
||||||
|
style={styles.searchInput}
|
||||||
|
placeholder="Search..."
|
||||||
|
value={searchText}
|
||||||
|
onChangeText={setSearchText}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<ScrollView style={styles.list}>
|
||||||
|
{filteredOptions.map((item) => (
|
||||||
|
<TouchableOpacity
|
||||||
|
key={item.value}
|
||||||
|
style={[
|
||||||
|
styles.option,
|
||||||
|
item.disabled && styles.optionDisabled,
|
||||||
|
selectedValue === item.value && styles.optionSelected,
|
||||||
|
]}
|
||||||
|
onPress={() => !item.disabled && handleSelect(item.value)}
|
||||||
|
disabled={item.disabled}
|
||||||
|
>
|
||||||
|
<Text
|
||||||
|
style={[
|
||||||
|
styles.optionText,
|
||||||
|
item.disabled && styles.optionTextDisabled,
|
||||||
|
selectedValue === item.value && styles.optionTextSelected,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</Text>
|
||||||
|
{selectedValue === item.value && (
|
||||||
|
<AntDesign name="check" size={16} color="#4ecdc4" />
|
||||||
|
)}
|
||||||
|
</TouchableOpacity>
|
||||||
|
))}
|
||||||
|
</ScrollView>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
wrapper: {
|
||||||
|
position: "relative",
|
||||||
|
},
|
||||||
|
container: {
|
||||||
|
borderWidth: 1,
|
||||||
|
borderColor: "#e6e6e6",
|
||||||
|
borderRadius: 8,
|
||||||
|
backgroundColor: "#fff",
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
},
|
||||||
|
content: {
|
||||||
|
flex: 1,
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
color: "#111",
|
||||||
|
},
|
||||||
|
suffix: {
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
},
|
||||||
|
icon: {
|
||||||
|
marginRight: 8,
|
||||||
|
},
|
||||||
|
arrow: {
|
||||||
|
marginLeft: 4,
|
||||||
|
},
|
||||||
|
dropdown: {
|
||||||
|
position: "absolute",
|
||||||
|
left: 0,
|
||||||
|
right: 0,
|
||||||
|
backgroundColor: "#fff",
|
||||||
|
borderWidth: 1,
|
||||||
|
borderColor: "#e6e6e6",
|
||||||
|
borderTopWidth: 0,
|
||||||
|
borderRadius: 10,
|
||||||
|
borderBottomLeftRadius: 8,
|
||||||
|
borderBottomRightRadius: 8,
|
||||||
|
shadowColor: "#000",
|
||||||
|
shadowOpacity: 0.1,
|
||||||
|
shadowRadius: 4,
|
||||||
|
shadowOffset: { width: 0, height: 2 },
|
||||||
|
elevation: 5,
|
||||||
|
zIndex: 1000,
|
||||||
|
},
|
||||||
|
searchInput: {
|
||||||
|
borderWidth: 1,
|
||||||
|
borderColor: "#e6e6e6",
|
||||||
|
borderRadius: 4,
|
||||||
|
padding: 8,
|
||||||
|
margin: 8,
|
||||||
|
},
|
||||||
|
list: {
|
||||||
|
maxHeight: 200,
|
||||||
|
},
|
||||||
|
option: {
|
||||||
|
padding: 12,
|
||||||
|
borderBottomWidth: 1,
|
||||||
|
borderBottomColor: "#f0f0f0",
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "center",
|
||||||
|
},
|
||||||
|
optionDisabled: {
|
||||||
|
opacity: 0.5,
|
||||||
|
},
|
||||||
|
optionSelected: {
|
||||||
|
backgroundColor: "#f6ffed",
|
||||||
|
},
|
||||||
|
optionText: {
|
||||||
|
fontSize: 16,
|
||||||
|
color: "#111",
|
||||||
|
},
|
||||||
|
optionTextDisabled: {
|
||||||
|
color: "#999",
|
||||||
|
},
|
||||||
|
optionTextSelected: {
|
||||||
|
color: "#4ecdc4",
|
||||||
|
fontWeight: "600",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Select;
|
||||||
@@ -45,16 +45,6 @@ export const FishCardForm: React.FC<FishCardFormProps> = ({
|
|||||||
onUpdateCatchItem,
|
onUpdateCatchItem,
|
||||||
}) => {
|
}) => {
|
||||||
const { fishSpecies } = useFishes();
|
const { fishSpecies } = useFishes();
|
||||||
const [fishNameOptions, setFishNameOptions] = React.useState<string[]>([]);
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
console.log("Length: ", fishSpecies?.length);
|
|
||||||
|
|
||||||
if (Array.isArray(fishSpecies)) {
|
|
||||||
const names = fishSpecies.map((item) => item.name).filter(Boolean);
|
|
||||||
setFishNameOptions(names);
|
|
||||||
}
|
|
||||||
}, [fishSpecies]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -66,15 +56,14 @@ export const FishCardForm: React.FC<FishCardFormProps> = ({
|
|||||||
{isEditing ? (
|
{isEditing ? (
|
||||||
<FishSelectDropdown
|
<FishSelectDropdown
|
||||||
options={fishSpecies || []}
|
options={fishSpecies || []}
|
||||||
selectedValue={null}
|
selectedFishId={selectedFishIndex}
|
||||||
isOpen={selectedFishIndex === index}
|
isOpen={selectedFishIndex === index}
|
||||||
onToggle={() =>
|
onToggle={() =>
|
||||||
setSelectedFishIndex(selectedFishIndex === index ? null : index)
|
setSelectedFishIndex(selectedFishIndex === index ? null : index)
|
||||||
}
|
}
|
||||||
onSelect={(value: Model.FishSpeciesResponse) => {
|
onSelect={(value: Model.FishSpeciesResponse) => {
|
||||||
onUpdateCatchItem(index, "fish_name", value.name);
|
onUpdateCatchItem(index, "fish_name", value.name);
|
||||||
// setSelectedFishIndex(null);
|
setSelectedFishIndex(value.id);
|
||||||
|
|
||||||
console.log("Fish Selected: ", fish);
|
console.log("Fish Selected: ", fish);
|
||||||
}}
|
}}
|
||||||
zIndex={1000 - index}
|
zIndex={1000 - index}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import styles from "../../style/NetDetailModal.styles";
|
|||||||
|
|
||||||
interface FishSelectDropdownProps {
|
interface FishSelectDropdownProps {
|
||||||
options: Model.FishSpeciesResponse[];
|
options: Model.FishSpeciesResponse[];
|
||||||
selectedValue: Model.FishSpeciesResponse | null;
|
selectedFishId: number | null;
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
onToggle: () => void;
|
onToggle: () => void;
|
||||||
onSelect: (value: Model.FishSpeciesResponse) => void;
|
onSelect: (value: Model.FishSpeciesResponse) => void;
|
||||||
@@ -15,7 +15,7 @@ interface FishSelectDropdownProps {
|
|||||||
|
|
||||||
export const FishSelectDropdown: React.FC<FishSelectDropdownProps> = ({
|
export const FishSelectDropdown: React.FC<FishSelectDropdownProps> = ({
|
||||||
options,
|
options,
|
||||||
selectedValue,
|
selectedFishId,
|
||||||
isOpen,
|
isOpen,
|
||||||
onToggle,
|
onToggle,
|
||||||
onSelect,
|
onSelect,
|
||||||
@@ -23,12 +23,17 @@ export const FishSelectDropdown: React.FC<FishSelectDropdownProps> = ({
|
|||||||
styleOverride,
|
styleOverride,
|
||||||
}) => {
|
}) => {
|
||||||
const dropdownStyle = styleOverride || styles.optionsList;
|
const dropdownStyle = styleOverride || styles.optionsList;
|
||||||
|
const findFishNameById = (id: number | null) => {
|
||||||
|
const fish = options.find((item) => item.id === id);
|
||||||
|
return fish?.name || "Chọn cá";
|
||||||
|
};
|
||||||
|
const [selectedFish, setSelectedFish] =
|
||||||
|
React.useState<Model.FishSpeciesResponse | null>(null);
|
||||||
return (
|
return (
|
||||||
<View style={{ zIndex }}>
|
<View style={{ zIndex }}>
|
||||||
<TouchableOpacity style={styles.selectButton} onPress={onToggle}>
|
<TouchableOpacity style={styles.selectButton} onPress={onToggle}>
|
||||||
<Text style={styles.selectButtonText}>
|
<Text style={styles.selectButtonText}>
|
||||||
{selectedValue ? selectedValue.name : "Chọn cá"}
|
{findFishNameById(selectedFishId)}
|
||||||
</Text>
|
</Text>
|
||||||
<IconSymbol
|
<IconSymbol
|
||||||
name={isOpen ? "chevron.up" : "chevron.down"}
|
name={isOpen ? "chevron.up" : "chevron.down"}
|
||||||
@@ -44,7 +49,9 @@ export const FishSelectDropdown: React.FC<FishSelectDropdownProps> = ({
|
|||||||
style={styles.optionItem}
|
style={styles.optionItem}
|
||||||
onPress={() => onSelect(option)}
|
onPress={() => onSelect(option)}
|
||||||
>
|
>
|
||||||
<Text style={styles.optionText}>{option.name}</Text>
|
<Text style={styles.optionText}>
|
||||||
|
{findFishNameById(option.id)}
|
||||||
|
</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
))}
|
))}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { queryFish } from "@/controller/FishController";
|
|||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
|
|
||||||
type Fish = {
|
type Fish = {
|
||||||
fishSpecies: Model.FishSpeciesResponse | null;
|
fishSpecies: Model.FishSpeciesResponse[] | null;
|
||||||
getFishSpecies: () => Promise<void>;
|
getFishSpecies: () => Promise<void>;
|
||||||
error: string | null;
|
error: string | null;
|
||||||
loading?: boolean;
|
loading?: boolean;
|
||||||
@@ -13,7 +13,7 @@ export const useFishes = create<Fish>((set) => ({
|
|||||||
getFishSpecies: async () => {
|
getFishSpecies: async () => {
|
||||||
try {
|
try {
|
||||||
const response = await queryFish();
|
const response = await queryFish();
|
||||||
console.log("Fish fetching API");
|
console.log("Fish fetching API: ", response.data.length);
|
||||||
|
|
||||||
set({ fishSpecies: response.data, loading: false });
|
set({ fishSpecies: response.data, loading: false });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user