@@ -19,8 +19,7 @@ interface Bloodhound<T> {
19
19
local : T [ ] ;
20
20
}
21
21
22
- function typeSearch ( el : HTMLInputElement ) {
23
- const jqueryEl = $ ( el ) ;
22
+ function typeSearch ( jqueryEl : JQuery , search ?: string ) {
24
23
const opts : Twitter . Typeahead . Options = {
25
24
highlight : true ,
26
25
minLength : 0
@@ -47,13 +46,30 @@ function typeSearch(el: HTMLInputElement) {
47
46
if ( k . keyCode === 13 ) { // Enter key
48
47
const selectables = jqueryEl . siblings ( ".tt-menu" ) . find ( ".tt-selectable" ) ;
49
48
$ ( selectables [ 0 ] ) . trigger ( "click" ) ;
49
+ } else {
50
+ updateSearch ( jqueryEl . val ( ) ) ;
50
51
}
51
52
} ) ;
52
53
54
+ if ( search ) {
55
+ jqueryEl . typeahead ( 'val' , search ) . typeahead ( 'open' ) ;
56
+ }
57
+
53
58
function navigate ( record : MinifiedSearchRecord ) {
54
59
window . location . href = `https://www.npmjs.org/package/@types/${ record . t } ` ;
55
60
}
56
61
62
+ function updateSearch ( newValue : string ) {
63
+ if ( ! URLSearchParams ) {
64
+ return ;
65
+ }
66
+
67
+ const params = new URLSearchParams ( window . location . search ) ;
68
+ params . set ( 'search' , newValue ) ;
69
+
70
+ history . pushState ( null , '' , `${ window . location . pathname } ?${ params } ` ) ;
71
+ }
72
+
57
73
function createDataSource ( ) : Bloodhound < MinifiedSearchRecord > {
58
74
let query = "" ;
59
75
return new Bloodhound < MinifiedSearchRecord > ( {
@@ -83,3 +99,18 @@ function typeSearch(el: HTMLInputElement) {
83
99
} ) ;
84
100
}
85
101
}
102
+
103
+ $ ( ( ) => {
104
+ const params = window . location . search
105
+ . substring ( 1 )
106
+ . split ( '&' )
107
+ . reduce < Record < string , string > > ( ( params , pair ) => {
108
+ const [ key , value ] = pair . split ( '=' ) ;
109
+ params [ key ] = value ;
110
+ return params ;
111
+ } , { } ) ;
112
+ const jqueryEl = $ ( "#demo" ) ;
113
+
114
+ typeSearch ( jqueryEl , params . search ) ;
115
+ jqueryEl . focus ( ) ;
116
+ } ) ;
0 commit comments