Skip to content

Commit c8f7fe0

Browse files
committed
improved search param
1 parent 574d197 commit c8f7fe0

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/views/develop/caniuse/caniuse.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,36 @@ import {CanIUseCard} from "./card";
55
import {uniq} from "lodash-es";
66

77
interface AppState {
8-
index: Record<string, string[]>;
8+
q?: string;
9+
index?: Record<string, string[]>;
910
}
1011

1112
class App extends Component<unknown, AppState> {
1213
constructor() {
1314
super();
15+
const query = new URLSearchParams(location.search);
16+
this.state = {q: (query.get('q')?.trim()) || undefined};
1417
fetch('caniuse/data/index.json').then(resp => resp.json()).then(data => this.setState({
15-
index: data
18+
index: data,
1619
}));
1720
}
1821

22+
onSearchChange = (e: Event) => {
23+
const q = (e.target as HTMLInputElement).value?.trim();
24+
const url = new URL(location.href);
25+
if (url.searchParams) {
26+
url.searchParams.set('q', q);
27+
history.pushState(null, '', url);
28+
}
29+
this.setState({q});
30+
};
31+
1932
render(props: unknown, state: AppState) {
20-
const query = new URLSearchParams(location.search);
21-
const name = query.get('q')
2233
return html`
23-
<h1>Can I use <input type="search" class=""/>?</h1>
34+
<h1>Can I use <input type="search" class="" oninput=${this.onSearchChange}/>?</h1>
2435
<hr/>
25-
${state.index && html`
26-
<${CanIUseSearch} index=${state.index} name=${name}/>`}`;
36+
${state.index && state.q && html`
37+
<${CanIUseSearch} index=${state.index} name=${state.q}/>`}`;
2738
}
2839

2940
}

0 commit comments

Comments
 (0)