Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 2x 4x | import { useState, useEffect, useRef } from "react"; import { useAtomValue } from "jotai"; import { useParams, NavLink } from "react-router-dom"; import { brandStoresState } from "../../state/brandStoreState"; import type { Store } from "../../types/shared"; type Props = { nativeNavLink?: boolean; }; // TODO: refactor this and use Downshift to have proper a11y function StoreSelector({ nativeNavLink }: Props): React.JSX.Element { const { id } = useParams(); const selectorRef = useRef<HTMLDivElement>(null); const brandStoresList = useAtomValue(brandStoresState); const [showStoreSelector, setShowStoreSelector] = useState<boolean>(false); const [searchValue, setSearchValue] = useState(""); const [filteredBrandStores, setFilteredBrandstores] = useState<Store[]>(brandStoresList); // close the store list when clicking outside of this component useEffect(() => { const clickOutsideListener = (e: MouseEvent) => { if (!selectorRef.current?.contains(e.target as HTMLElement)) { setShowStoreSelector(false); } }; window.addEventListener("click", clickOutsideListener); return () => { window.removeEventListener("click", clickOutsideListener); }; }, []); const getStoreName = (id: string | undefined) => { if (!id) { return; } const targetStore = brandStoresList.find((store) => store.id === id); if (targetStore) { return targetStore.name; } return "Select a store"; }; useEffect(() => { setFilteredBrandstores(brandStoresList); }, [brandStoresList]); return ( <div className="store-selector" ref={selectorRef}> <button className="store-selector__button u-no-margin--bottom" onClick={() => { setShowStoreSelector(!showStoreSelector); }} > {id !== undefined ? getStoreName(id) : "Select a store"} </button> {showStoreSelector && ( <div className="store-selector__panel"> <div className="p-search-box u-no-margin--bottom"> <label htmlFor="search-stores" className="u-off-screen"> Search stores </label> <input type="search" className="p-search-box__input" id="search-stores" name="search-stores" placeholder="Search" value={searchValue} onInput={(e) => { const value = (e.target as HTMLInputElement).value; setSearchValue(value); if (value.length > 0) { setFilteredBrandstores( brandStoresList.filter((store) => { const storeName = store.name.toLowerCase(); return storeName.includes(value.toLowerCase()); }), ); } else { setFilteredBrandstores(brandStoresList); } }} /> <button type="reset" className="p-search-box__reset" onClick={() => { setSearchValue(""); setFilteredBrandstores(brandStoresList); }} > <i className="p-icon--close">Close</i> </button> <button type="submit" className="p-search-box__button"> <i className="p-icon--search">Search</i> </button> </div> <ul className="store-selector__list" style={{ listStyle: "none" }}> {filteredBrandStores.map((store: Store) => ( <li key={store.id} className="store-selector__item"> {nativeNavLink ? ( <a href={`/admin/${store.id}/snaps`}>{store.name}</a> ) : ( <NavLink to={`/admin/${store.id}/snaps`} onClick={() => { setShowStoreSelector(false); }} > {store.name} </NavLink> )} </li> ))} </ul> </div> )} </div> ); } export default StoreSelector; |