From c410f7aad7fbf788b76fcf6ecc853d186c0a0ab4 Mon Sep 17 00:00:00 2001 From: Wes Weitzel Date: Tue, 12 Dec 2023 17:56:13 -0800 Subject: [PATCH] Player and team search (#85) --- src/components/Select.tsx | 36 ++++++++++++--- src/components/ThemeSelect.tsx | 4 +- src/hooks/usePrevious.ts | 9 ++++ src/lib/api/goals.ts | 1 + src/lib/api/players.ts | 33 ++++++++++++++ src/lib/api/teams.ts | 1 + src/pages/Goals.tsx | 80 ++++++++++++++++++++-------------- 7 files changed, 123 insertions(+), 41 deletions(-) create mode 100644 src/hooks/usePrevious.ts create mode 100644 src/lib/api/players.ts diff --git a/src/components/Select.tsx b/src/components/Select.tsx index aa2adce..121deb0 100644 --- a/src/components/Select.tsx +++ b/src/components/Select.tsx @@ -1,18 +1,23 @@ import 'bootstrap/js/dist/dropdown'; -import {useState} from 'react'; +import {useEffect, useState} from 'react'; +import {usePrevious} from '../hooks/usePrevious'; interface Option { displayName: string; - value: number | string; + key: any; + value: any; + image?: string; } interface SelectProps { label?: string; options?: Option[]; - value?: number | string; + value?: any; onChange: (value: any) => void; showSearchInput?: boolean; + onSearchChange?: (value: string) => void; showAllOption?: boolean; + onFirstInteraction?: () => void; } function Select({ @@ -21,13 +26,32 @@ function Select({ value, onChange, showSearchInput = false, + onSearchChange = () => {}, showAllOption = true, + onFirstInteraction = () => {}, }: SelectProps) { const currentOption = options?.find((option) => option.value == value); const [searchText, setSearchText] = useState(''); + const prevSearchText = usePrevious(searchText); + const [hadFirstInteraction, setHadFirstInteraction] = useState(false); const dropdownId = `dropdown-${Math.random().toString(36).substring(7)}`; + useEffect(() => { + const id = setTimeout(() => { + if (searchText || (prevSearchText && prevSearchText.length > 0)) { + onSearchChange(searchText); + } + }, 250); + return () => clearTimeout(id); + }, [searchText]); + + useEffect(() => { + if (hadFirstInteraction) { + onFirstInteraction(); + } + }, [hadFirstInteraction]); + return (