import React, { useState, useEffect } from "react" import { Modal, IconButton, Portal, List, Button, TextInput, Text, } from "react-native-paper" import { ScrollView, View, StyleSheet } from "react-native" import { FontAwesomeIcon } from "@fortawesome/react-native-fontawesome" import type { IconDefinition, IconName, } from "@fortawesome/fontawesome-svg-core" import { library, findIconDefinition } from "@fortawesome/fontawesome-svg-core" import { fas } from "@fortawesome/free-solid-svg-icons" export interface HabitIconSelectorModalProps { visible?: boolean value: string onDismiss: () => void onSelect: (icon: string) => void onPress: () => void } export const HabitIconSelectorModal: React.FC = ({ visible = false, value, onDismiss, onSelect, onPress, }) => { const [selectedIcon, setSelectedIcon] = useState() const [possibleIcons, setPossibleIcons] = useState([]) const [searchText, setSearchText] = useState(value) useEffect(() => { setPossibleIcons(findIconsInLibrary(searchText)) }, [searchText]) library.add(fas) const handleIconSelect = (icon: string): void => { setSelectedIcon(icon) onSelect(icon) } const findIconsInLibrary = (icon: string): IconDefinition[] => { const iconNames = Object.keys(fas).map((key) => { return fas[key]?.iconName ?? "" }) const matchedValue: string[] = iconNames.filter((name) => { return name.includes(icon) }) return matchedValue.length > 0 ? matchedValue.map((name) => { return findIconDefinition({ prefix: "fas", iconName: name as IconName, }) }) : [] } return ( <> { return setSearchText(text) }} /> {possibleIcons.length > 0 ? ( {possibleIcons.map((icon) => { return ( { return ( ) }} size={30} onPress={() => { handleIconSelect(icon.iconName) }} /> ) })} ) : ( No results found )} ) } const styles = StyleSheet.create({ modalContent: { backgroundColor: "white", padding: 20, margin: 20, borderRadius: 10, }, iconContainer: { flexDirection: "row", flexWrap: "wrap", justifyContent: "space-between", }, scrollView: { maxHeight: 10000, maxWidth: 5000, }, noResults: { marginTop: 20, alignItems: "center", }, })