@@ -5,25 +5,36 @@ import {CanIUseCard} from "./card";
55import { uniq } from "lodash-es" ;
66
77interface AppState {
8- index : Record < string , string [ ] > ;
8+ q ?: string ;
9+ index ?: Record < string , string [ ] > ;
910}
1011
1112class 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