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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 99x 99x 99x 99x 98x 2x 2x 44x 44x 3x 3x 10x 3x 3x 3x 3x 7x 7x 10x 10x 7x 10x 7x 7x 99x 12x 3x 21x 1x 17x 17x | import { Button } from "@wintermuted/ui-react";
import React, { useEffect, useId, useRef } from "react";
import FontAwesome from "react-fontawesome";
import "./GlobalPlaceSearch.scss";
export interface GlobalPlaceSearchProps {
query: string;
onQueryChange: (query: string) => void;
open: boolean;
onOpenChange: (open: boolean) => void;
hint: React.ReactNode;
scope: React.ReactNode;
status: React.ReactNode;
loadStatus?: React.ReactNode;
children?: React.ReactNode;
moreAction?: React.ReactNode;
title?: string;
inputLabel?: string;
searchLabel?: string;
resultsLabel?: string;
closeLabel?: string;
}
export function GlobalPlaceSearch({
query,
onQueryChange,
open,
onOpenChange,
hint,
scope,
status,
loadStatus,
children,
moreAction,
title = "All places",
inputLabel = "Search all places",
searchLabel = "All places",
resultsLabel = "Global Places search results",
closeLabel = "Close Places search",
}: GlobalPlaceSearchProps) {
const root = useRef<HTMLDivElement>(null);
const input = useRef<HTMLInputElement>(null);
const id = useId();
useEffect(() => {
if (!open) return;
function dismiss(event: PointerEvent) {
Eif (event.target instanceof Node && !root.current?.contains(event.target))
onOpenChange(false);
}
document.addEventListener("pointerdown", dismiss);
return () => document.removeEventListener("pointerdown", dismiss);
}, [open, onOpenChange]);
function close() {
input.current?.focus();
onOpenChange(false);
}
function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
if (event.key === "Escape" && open) {
event.preventDefault();
event.stopPropagation();
close();
return;
}
Iif (!open || !["ArrowDown", "ArrowUp"].includes(event.key)) return;
const links = Array.from(
root.current?.querySelectorAll<HTMLAnchorElement>(
".transit-global-search-results a",
) || [],
);
const index =
event.target instanceof HTMLAnchorElement
? links.indexOf(event.target)
: -1;
Iif (event.target !== input.current && index < 0) return;
const next =
event.key === "ArrowDown"
? links[Math.min(index + 1, links.length - 1)]
: links[index - 1] || input.current;
if (next) {
event.preventDefault();
next.focus();
}
}
return (
<div
ref={root}
className="transit-global-place-search"
onKeyDownCapture={handleKeyDown}
onBlur={(event) => {
if (!event.currentTarget.contains(event.relatedTarget))
onOpenChange(false);
}}
>
<form
role="search"
aria-label={searchLabel}
onSubmit={(event) => {
event.preventDefault();
onOpenChange(true);
}}
>
<label htmlFor={`${id}-input`} className="wm-sr-only">
{inputLabel}
</label>
<FontAwesome
name="search"
className="transit-global-search-icon"
aria-hidden="true"
/>
<input
ref={input}
id={`${id}-input`}
type="search"
className="wm-input"
autoComplete="off"
placeholder={inputLabel}
value={query}
aria-controls={open ? `${id}-results` : undefined}
aria-describedby={`${id}-hint`}
onFocus={() => onOpenChange(true)}
onClick={() => onOpenChange(true)}
onChange={(event) => {
onQueryChange(event.target.value);
onOpenChange(true);
}}
/>
<span id={`${id}-hint`} className="wm-sr-only">
{hint}
</span>
</form>
{open && (
<section
id={`${id}-results`}
className="transit-global-search-popover"
aria-label={resultsLabel}
>
<div className="transit-global-search-heading">
<h2>{title}</h2>
{loadStatus}
<Button
size="sm"
variant="ghost"
aria-label={closeLabel}
onClick={close}
>
<FontAwesome name="times" aria-hidden="true" />
</Button>
</div>
<p className="transit-global-search-scope">{scope}</p>
<p role="status" className="transit-global-search-count">
{status}
</p>
{children}
{moreAction}
</section>
)}
</div>
);
}
|