Skip to content

Commit df692dd

Browse files
committed
feat: control input
1 parent a405b18 commit df692dd

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

src/App.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Box } from "@chakra-ui/react";
2-
import { useState } from "react";
2+
import { useEffect, useState } from "react";
33
import "./app.css";
44
import Header from "./components/header";
55
import { Map } from "./components/map";
@@ -9,7 +9,26 @@ import { Panel } from "./components/panel";
99
import { Toaster } from "./components/ui/toaster";
1010

1111
function App() {
12-
const [href, setHref] = useState("");
12+
const [href, setHref] = useState(
13+
new URLSearchParams(location.search).get("href") ?? ""
14+
);
15+
16+
useEffect(() => {
17+
if (new URLSearchParams(location.search).get("href") != href) {
18+
history.pushState(null, "", "?href=" + href);
19+
}
20+
}, [href]);
21+
22+
useEffect(() => {
23+
function handlePopState() {
24+
setHref(new URLSearchParams(location.search).get("href") ?? "");
25+
}
26+
27+
window.addEventListener("popstate", handlePopState);
28+
return () => {
29+
window.removeEventListener("popstate", handlePopState);
30+
};
31+
}, []);
1332

1433
return (
1534
<LayersProvider>
@@ -18,7 +37,7 @@ function App() {
1837
</Box>
1938
<Box zIndex={1}>
2039
<Overlay>
21-
<Header setHref={setHref}></Header>
40+
<Header href={href} setHref={setHref}></Header>
2241
<Panel href={href}></Panel>
2342
</Overlay>
2443
</Box>

src/components/header.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Menu,
77
Portal,
88
} from "@chakra-ui/react";
9-
import { type Dispatch, type SetStateAction } from "react";
9+
import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
1010
import { LuSearch } from "react-icons/lu";
1111

1212
const EXAMPLES = [
@@ -22,14 +22,36 @@ const EXAMPLES = [
2222
];
2323

2424
export default function Header({
25+
href,
2526
setHref,
2627
}: {
28+
href: string;
2729
setHref: Dispatch<SetStateAction<string>>;
2830
}) {
31+
const [value, setValue] = useState(href);
32+
33+
useEffect(() => {
34+
setValue(href);
35+
}, [href]);
36+
2937
return (
3038
<HStack py={4} pointerEvents={"auto"}>
31-
<InputGroup w="full" startElement={<LuSearch></LuSearch>}>
32-
<Input flex={1} variant={"subtle"} rounded={4}></Input>
39+
<InputGroup
40+
w="full"
41+
startElement={<LuSearch></LuSearch>}
42+
as={"form"}
43+
onSubmit={(e) => {
44+
e.preventDefault();
45+
setHref(value);
46+
}}
47+
>
48+
<Input
49+
flex={1}
50+
variant={"subtle"}
51+
rounded={4}
52+
value={value}
53+
onChange={(e) => setValue(e.currentTarget.value)}
54+
></Input>
3355
</InputGroup>
3456
<Menu.Root onSelect={(details) => setHref(details.value)}>
3557
<Menu.Trigger asChild>

src/components/json.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function RawJsonDialogButton({
2323
<Dialog.Root scrollBehavior={"inside"} size={"xl"}>
2424
<Dialog.Trigger>
2525
<Tooltip content="Show raw JSON in a dialog">
26-
<IconButton {...rest}>
26+
<IconButton {...rest} as={"div"}>
2727
<LuSearchCode></LuSearchCode>
2828
</IconButton>
2929
</Tooltip>

0 commit comments

Comments
 (0)