From 2f19c2ccc44ee5265c242f6fc37d1cf16baad4e7 Mon Sep 17 00:00:00 2001 From: nguvictor Date: Sun, 22 Oct 2023 21:23:40 -0400 Subject: [PATCH 01/11] initial prototype --- src/app/components/PairSelector.tsx | 184 +++++++++++++++++++++++----- 1 file changed, 150 insertions(+), 34 deletions(-) diff --git a/src/app/components/PairSelector.tsx b/src/app/components/PairSelector.tsx index 1a81fe14..77b18766 100644 --- a/src/app/components/PairSelector.tsx +++ b/src/app/components/PairSelector.tsx @@ -1,6 +1,84 @@ import { useAppSelector, useAppDispatch } from "../hooks"; import { selectPairAddress } from "../redux/pairSelectorSlice"; +import { useEffect, useRef, useState } from "react"; +/* +const SearchableDropdown = ({ + options, + label, + id, + selectedVal, + handleChange, +}) => { + const [query, setQuery] = useState(""); + const [isOpen, setIsOpen] = useState(false); + const inputRef = useRef(null); + + useEffect(() => { + document.addEventListener("click", toggle); + return () => document.removeEventListener("click", toggle); + }, []); + + const selectOption = (option) => { + setQuery(() => ""); + handleChange(option[label]); + setIsOpen((isOpen) => !isOpen); + }; + + function toggle(e) { + setIsOpen(e && e.target === inputRef.current); + } + + const getDisplayValue = () => { + if (query) return query; + if (selectedVal) return selectedVal; + + return ""; + }; + + const filter = (options) => { + return options.filter( + (option) => option[label].toLowerCase().indexOf(query.toLowerCase()) > -1 + ); + }; + + return ( +
+
+
+ { + setQuery(e.target.value); + handleChange(null); + }} + onClick={toggle} + /> +
+
+
+ +
+ {filter(options).map((option, index) => { + return ( +
selectOption(option)} + className={`option ${option[label] === selectedVal ? "selected" : "" + }`} + key={`${id}-${index}`} + > + {option[label]} +
+ ); + })} +
+
+ ); +}; +*/ function displayName(name?: string) { return name?.replace("/", " - "); } @@ -11,44 +89,82 @@ export function PairSelector() { const selectPair = (pairAddress: string) => { dispatch(selectPairAddress(pairAddress)); }; + const options = [ + { id: 1, name: "Graspus graspus" }, + { id: 2, name: "Grus rubicundus" }, + { id: 3, name: "Speothos vanaticus" }, + ]; + const label = "name"; + const id = "pairAddress"; + const selectedVal = "A"; + const handleChange = (val) => setValue(val); + + const [query, setQuery] = useState(""); + const [isOpen, setIsOpen] = useState(false); + + const inputRef = useRef(null); + + useEffect(() => { + document.addEventListener("click", toggle); + return () => document.removeEventListener("click", toggle); + }, []); + + const selectOption = (option) => { + setQuery(() => ""); + handleChange(option[label]); + setIsOpen((isOpen) => !isOpen); + }; + + function toggle(e) { + setIsOpen(e && e.target === inputRef.current); + } + + const getDisplayValue = () => { + if (query) return query; + if (selectedVal) return selectedVal; + + return ""; + }; + + const filter = (options) => { + return options.filter( + (option) => option[label].toLowerCase().indexOf(query.toLowerCase()) > -1 + ); + }; return (
- - + {option[label]} +
+ ); + })} + ); } From 0e9552df45414cdfd8222bda4550b9596468ab96 Mon Sep 17 00:00:00 2001 From: Victor Date: Sun, 22 Oct 2023 22:04:31 -0400 Subject: [PATCH 02/11] switching is working --- src/app/components/PairSelector.tsx | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/app/components/PairSelector.tsx b/src/app/components/PairSelector.tsx index 77b18766..cbb4ae1b 100644 --- a/src/app/components/PairSelector.tsx +++ b/src/app/components/PairSelector.tsx @@ -89,15 +89,11 @@ export function PairSelector() { const selectPair = (pairAddress: string) => { dispatch(selectPairAddress(pairAddress)); }; - const options = [ - { id: 1, name: "Graspus graspus" }, - { id: 2, name: "Grus rubicundus" }, - { id: 3, name: "Speothos vanaticus" }, - ]; - const label = "name"; + console.log(pairSelector.pairsList); + const options = pairSelector.pairsList; const id = "pairAddress"; - const selectedVal = "A"; - const handleChange = (val) => setValue(val); + const selectedVal = pairSelector.pairsList[0] ? pairSelector.pairsList[0].name : "Loading"; + const handleChange = (val) => {console.log(val);selectPair(val)}; const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); @@ -111,7 +107,7 @@ export function PairSelector() { const selectOption = (option) => { setQuery(() => ""); - handleChange(option[label]); + handleChange(option["address"]); setIsOpen((isOpen) => !isOpen); }; @@ -128,12 +124,12 @@ export function PairSelector() { const filter = (options) => { return options.filter( - (option) => option[label].toLowerCase().indexOf(query.toLowerCase()) > -1 + (option) => option["name"].toLowerCase().indexOf(query.toLowerCase()) > -1 ); }; return ( -
+
-
+
-
+
{filter(options).map((option, index) => { return (
selectOption(option)} - className={`option ${option[label] === selectedVal ? "selected" : "" - }`} + className={`option ${ + option["name"] === selectedVal ? "selected" : "" + }`} key={`${id}-${index}`} > - {option[label]} + {option["name"]}
); })} From e667e28eb949f1d86398d428ef1f9c19baeb2944 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 23 Oct 2023 18:22:05 -0400 Subject: [PATCH 03/11] style adjustments --- src/app/components/PairSelector.tsx | 95 ++++++++++++++++++----------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/src/app/components/PairSelector.tsx b/src/app/components/PairSelector.tsx index cbb4ae1b..f356336a 100644 --- a/src/app/components/PairSelector.tsx +++ b/src/app/components/PairSelector.tsx @@ -89,11 +89,15 @@ export function PairSelector() { const selectPair = (pairAddress: string) => { dispatch(selectPairAddress(pairAddress)); }; - console.log(pairSelector.pairsList); const options = pairSelector.pairsList; - const id = "pairAddress"; - const selectedVal = pairSelector.pairsList[0] ? pairSelector.pairsList[0].name : "Loading"; - const handleChange = (val) => {console.log(val);selectPair(val)}; + const id = "pairOption"; + const selectedVal = pairSelector.pairsList[0] + ? pairSelector.pairsList[0].name + : "Loading"; + const handleChange = (val) => { + console.log(val); + selectPair(val); + }; const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); @@ -130,37 +134,58 @@ export function PairSelector() { return (
-
-
- { - setQuery(e.target.value); - handleChange(null); - }} - onClick={toggle} - /> -
-
-
- -
- {filter(options).map((option, index) => { - return ( -
selectOption(option)} - className={`option ${ - option["name"] === selectedVal ? "selected" : "" - }`} - key={`${id}-${index}`} - > - {option["name"]} -
- ); - })} +
+ +
    + {filter(options).map((option, index) => { + return ( +
  • selectOption(option)} + className={ + `${option["name"] === selectedVal ? "selected" : ""}` + + " font-bold !pl-0" + } + key={`${id}-${index}`} + > + +
  • + ); + })} +
); From 8d656ba2678611d34a5c2ef889383a45422f54c4 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 24 Oct 2023 00:28:15 -0400 Subject: [PATCH 04/11] styling updates --- src/app/components/PairSelector.tsx | 40 +++++++++++++++++------------ 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/app/components/PairSelector.tsx b/src/app/components/PairSelector.tsx index f356336a..c6431077 100644 --- a/src/app/components/PairSelector.tsx +++ b/src/app/components/PairSelector.tsx @@ -91,37 +91,42 @@ export function PairSelector() { }; const options = pairSelector.pairsList; const id = "pairOption"; - const selectedVal = pairSelector.pairsList[0] + let selectedVal = pairSelector.pairsList[0] ? pairSelector.pairsList[0].name : "Loading"; const handleChange = (val) => { - console.log(val); selectPair(val); }; const [query, setQuery] = useState(""); - const [isOpen, setIsOpen] = useState(false); + const [isOpen, setIsOpen] = useState(true); const inputRef = useRef(null); - + const dropdownRef = useRef(null); + /* useEffect(() => { document.addEventListener("click", toggle); return () => document.removeEventListener("click", toggle); }, []); - +*/ const selectOption = (option) => { setQuery(() => ""); handleChange(option["address"]); + console.log(option); + selectedVal = option["name"]; setIsOpen((isOpen) => !isOpen); }; function toggle(e) { + console.log(inputRef.current); setIsOpen(e && e.target === inputRef.current); } const getDisplayValue = () => { - if (query) return query; - if (selectedVal) return selectedVal; + console.log("query %s", query); + console.log("sel %s", selectedVal); + if (query) return displayName(query); + if (selectedVal) return displayName(selectedVal); return ""; }; @@ -134,9 +139,9 @@ export function PairSelector() { return (
-
-