228 lines
7.2 KiB
TypeScript
228 lines
7.2 KiB
TypeScript
import React, { useEffect, useMemo, useState } from "react";
|
|
import {
|
|
Modal,
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
} from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import { moderateScale, verticalScale } from "react-native-size-matters";
|
|
|
|
import { Colors } from "@/theme";
|
|
|
|
export interface SearchAttributeOption {
|
|
attribute_key: string;
|
|
label: string;
|
|
}
|
|
|
|
interface SearchAttributeModalProps {
|
|
visible: boolean;
|
|
fields: SearchAttributeOption[];
|
|
selected: string[];
|
|
onApply: (attributeKeys: string[]) => void;
|
|
onClose: () => void;
|
|
}
|
|
|
|
export function SearchAttributeModal({
|
|
visible,
|
|
fields,
|
|
selected,
|
|
onApply,
|
|
onClose,
|
|
}: SearchAttributeModalProps) {
|
|
const validKeys = useMemo(() => new Set(fields.map((item) => item.attribute_key)), [fields]);
|
|
const [draft, setDraft] = useState<string[]>([]);
|
|
|
|
useEffect(() => {
|
|
if (!visible) return;
|
|
setDraft(selected.filter((key) => validKeys.has(key)));
|
|
}, [selected, validKeys, visible]);
|
|
|
|
const selectedSet = useMemo(() => new Set(draft), [draft]);
|
|
|
|
const toggle = (key: string) => {
|
|
setDraft((current) =>
|
|
current.includes(key)
|
|
? current.filter((item) => item !== key)
|
|
: [...current, key],
|
|
);
|
|
};
|
|
|
|
return (
|
|
<Modal visible={visible} animationType="slide" transparent onRequestClose={onClose}>
|
|
<View style={styles.overlay}>
|
|
<Pressable style={styles.scrim} onPress={onClose} />
|
|
<View style={styles.sheet}>
|
|
<View style={styles.handle} />
|
|
<View style={styles.header}>
|
|
<View style={styles.headerText}>
|
|
<Text style={styles.title}>Search in</Text>
|
|
<Text style={styles.subtitle}>
|
|
Choose which searchable attributes should participate in the text search.
|
|
Leave all unselected to search everywhere.
|
|
</Text>
|
|
</View>
|
|
<Pressable onPress={onClose} hitSlop={10} style={styles.closeButton}>
|
|
<Ionicons name="close" size={22} color={Colors.textPrimary} />
|
|
</Pressable>
|
|
</View>
|
|
|
|
<ScrollView
|
|
showsVerticalScrollIndicator={false}
|
|
contentContainerStyle={styles.content}
|
|
>
|
|
<Pressable
|
|
onPress={() => setDraft([])}
|
|
style={[styles.option, draft.length === 0 && styles.optionActive]}
|
|
>
|
|
<View style={styles.optionCopy}>
|
|
<Text style={[styles.optionTitle, draft.length === 0 && styles.optionTitleActive]}>
|
|
All searchable attributes
|
|
</Text>
|
|
<Text style={styles.optionText}>Search across all visible searchable attributes in this module.</Text>
|
|
</View>
|
|
{draft.length === 0 ? (
|
|
<Ionicons name="checkmark-circle" size={21} color={Colors.primary} />
|
|
) : null}
|
|
</Pressable>
|
|
|
|
{fields.map((field) => {
|
|
const active = selectedSet.has(field.attribute_key);
|
|
return (
|
|
<Pressable
|
|
key={field.attribute_key}
|
|
onPress={() => toggle(field.attribute_key)}
|
|
style={[styles.option, active && styles.optionActive]}
|
|
>
|
|
<View style={styles.optionCopy}>
|
|
<Text style={[styles.optionTitle, active && styles.optionTitleActive]}>
|
|
{field.label}
|
|
</Text>
|
|
</View>
|
|
<Ionicons
|
|
name={active ? "checkbox" : "square-outline"}
|
|
size={21}
|
|
color={active ? Colors.primary : Colors.textSecondary}
|
|
/>
|
|
</Pressable>
|
|
);
|
|
})}
|
|
</ScrollView>
|
|
|
|
<View style={styles.footer}>
|
|
<Pressable onPress={() => setDraft([])} style={styles.secondaryButton}>
|
|
<Text style={styles.secondaryText}>Reset to all</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
onPress={() => {
|
|
onApply(draft);
|
|
onClose();
|
|
}}
|
|
style={styles.primaryButton}
|
|
>
|
|
<Text style={styles.primaryText}>Apply search fields</Text>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
overlay: { flex: 1, justifyContent: "flex-end" },
|
|
scrim: { ...StyleSheet.absoluteFillObject, backgroundColor: "rgba(15,23,42,0.36)" },
|
|
sheet: {
|
|
maxHeight: "82%",
|
|
borderTopLeftRadius: moderateScale(22),
|
|
borderTopRightRadius: moderateScale(22),
|
|
backgroundColor: Colors.surface,
|
|
overflow: "hidden",
|
|
},
|
|
handle: {
|
|
alignSelf: "center",
|
|
width: moderateScale(42),
|
|
height: verticalScale(4),
|
|
marginTop: verticalScale(8),
|
|
borderRadius: 99,
|
|
backgroundColor: "#CBD5E1",
|
|
},
|
|
header: {
|
|
flexDirection: "row",
|
|
alignItems: "flex-start",
|
|
gap: moderateScale(12),
|
|
paddingHorizontal: moderateScale(20),
|
|
paddingTop: verticalScale(14),
|
|
paddingBottom: verticalScale(10),
|
|
},
|
|
headerText: { flex: 1 },
|
|
title: { fontSize: moderateScale(18), fontWeight: "800", color: Colors.textPrimary },
|
|
subtitle: {
|
|
marginTop: verticalScale(4),
|
|
fontSize: moderateScale(12),
|
|
lineHeight: moderateScale(17),
|
|
color: Colors.textSecondary,
|
|
},
|
|
closeButton: {
|
|
width: moderateScale(34),
|
|
height: moderateScale(34),
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
content: {
|
|
gap: verticalScale(8),
|
|
paddingHorizontal: moderateScale(20),
|
|
paddingVertical: verticalScale(10),
|
|
},
|
|
option: {
|
|
minHeight: verticalScale(54),
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: moderateScale(12),
|
|
paddingHorizontal: moderateScale(14),
|
|
paddingVertical: verticalScale(10),
|
|
borderWidth: 1,
|
|
borderColor: "#E2E8F0",
|
|
borderRadius: moderateScale(14),
|
|
backgroundColor: "#FFFFFF",
|
|
},
|
|
optionActive: { borderColor: "#A7D7A9", backgroundColor: "#F1F8F1" },
|
|
optionCopy: { flex: 1 },
|
|
optionTitle: { fontSize: moderateScale(13.5), fontWeight: "700", color: Colors.textPrimary },
|
|
optionTitleActive: { color: Colors.primaryDark },
|
|
optionText: { marginTop: 2, fontSize: moderateScale(11.5), color: Colors.textSecondary },
|
|
footer: {
|
|
flexDirection: "row",
|
|
gap: moderateScale(10),
|
|
paddingHorizontal: moderateScale(20),
|
|
paddingTop: verticalScale(12),
|
|
paddingBottom: verticalScale(16),
|
|
borderTopWidth: 1,
|
|
borderTopColor: "#E2E8F0",
|
|
backgroundColor: "#FFFFFF",
|
|
},
|
|
secondaryButton: {
|
|
minWidth: moderateScale(104),
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
paddingHorizontal: moderateScale(14),
|
|
paddingVertical: verticalScale(11),
|
|
borderRadius: moderateScale(12),
|
|
borderWidth: 1,
|
|
borderColor: "#CBD5E1",
|
|
},
|
|
secondaryText: { fontSize: moderateScale(12.5), fontWeight: "700", color: Colors.textPrimary },
|
|
primaryButton: {
|
|
flex: 1,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
paddingHorizontal: moderateScale(14),
|
|
paddingVertical: verticalScale(11),
|
|
borderRadius: moderateScale(12),
|
|
backgroundColor: Colors.primary,
|
|
},
|
|
primaryText: { fontSize: moderateScale(12.5), fontWeight: "800", color: "#FFFFFF" },
|
|
});
|