@@ -5,25 +5,36 @@ import {CanIUseCard} from "./card";
5
5
import { uniq } from "lodash-es" ;
6
6
7
7
interface AppState {
8
- index : Record < string , string [ ] > ;
8
+ q ?: string ;
9
+ index ?: Record < string , string [ ] > ;
9
10
}
10
11
11
12
class App extends Component < unknown , AppState > {
12
13
constructor ( ) {
13
14
super ( ) ;
15
+ const query = new URLSearchParams ( location . search ) ;
16
+ this . state = { q : ( query . get ( 'q' ) ?. trim ( ) ) || undefined } ;
14
17
fetch ( 'caniuse/data/index.json' ) . then ( resp => resp . json ( ) ) . then ( data => this . setState ( {
15
- index : data
18
+ index : data ,
16
19
} ) ) ;
17
20
}
18
21
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
+
19
32
render ( props : unknown , state : AppState ) {
20
- const query = new URLSearchParams ( location . search ) ;
21
- const name = query . get ( 'q' )
22
33
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 >
24
35
< 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 } /> ` } `;
27
38
}
28
39
29
40
}
0 commit comments