1
1
'use strict' ;
2
2
3
- Vue . component ( ' suggestion' , {
3
+ const suggestion = {
4
4
template : `
5
5
<div v-bind:class="[suggestions.length > 0 ? [suggestionContainerClass, suggestionActivatedClass] : suggestionContainerClass]">
6
- <input :type="type" :name="name" :placeholder="placeholder" v-model="search" :autocomplete="autocomplete " v-on:keyup="keyboardHandle" v-on:search="keyboardHandle" v-focus>
6
+ <input :type="type" :name="name" :placeholder="placeholder" :autocomplete="autocomplete" v-model="search " v-on:keyup="keyboardHandle" v-on:search="keyboardHandle" v-focus>
7
7
<ul v-if="suggestions">
8
8
<li v-for="suggestion in suggestions" v-bind:class="{ selected: suggestion.selected }" v-on:click="chooseSuggestion(suggestion)">{{ suggestion.label }}</li>
9
9
</ul>
10
10
</div>
11
- ` ,
12
- data : ( ) => {
13
- return {
14
- currentSuggestion : null ,
15
- suggestions : [ ] ,
16
- loadTimeout : null
17
- }
18
- } ,
11
+ ` ,
12
+ components : { } ,
13
+ emits : [
14
+ 'loadSuggestion' ,
15
+ 'chooseSuggestion'
16
+ ] ,
19
17
props : {
20
- name : {
18
+ type : {
21
19
type : String ,
22
20
default : 'search'
23
21
} ,
24
- placeholder : {
22
+ name : {
25
23
type : String ,
26
24
default : 'search'
27
25
} ,
28
- type : {
26
+ placeholder : {
29
27
type : String ,
30
- default : 'search'
31
- } ,
32
- search : {
33
- type : String ,
34
- default : ''
28
+ default : 'Enter your search'
35
29
} ,
36
30
autocomplete : {
37
31
type : String ,
38
32
default : 'off'
39
33
} ,
34
+ value : {
35
+ type : String ,
36
+ default : ''
37
+ } ,
40
38
loadSuggestionFromTheNumberOfCharacters : {
41
39
type : Number ,
42
40
default : 3
@@ -45,16 +43,6 @@ Vue.component('suggestion', {
45
43
type : Number ,
46
44
default : 100
47
45
} ,
48
- loadSuggestionCallback : {
49
- type : Function ,
50
- require : true ,
51
- default : null
52
- } ,
53
- chooseSuggestionCallback : {
54
- type : Function ,
55
- require : true ,
56
- default : null
57
- } ,
58
46
suggestionContainerClass : {
59
47
type : String ,
60
48
default : 'vue-suggestion'
@@ -76,16 +64,27 @@ Vue.component('suggestion', {
76
64
default : true
77
65
}
78
66
} ,
67
+ data ( ) {
68
+ return {
69
+ search : '' ,
70
+ currentSuggestion : null ,
71
+ suggestions : [ ] ,
72
+ loadTimeout : null
73
+ }
74
+ } ,
79
75
directives : {
80
76
focus : {
81
77
inserted : function ( el ) {
82
- el . focus ( )
78
+ el . focus ( ) ;
83
79
}
84
80
}
85
81
} ,
86
82
created ( ) {
87
83
window . addEventListener ( 'wheel' , this . mouseHandle ) ;
88
84
} ,
85
+ mounted ( ) {
86
+ this . search = this . value ;
87
+ } ,
89
88
destroyed ( ) {
90
89
window . removeEventListener ( 'wheel' , this . mouseHandle ) ;
91
90
} ,
@@ -97,7 +96,7 @@ Vue.component('suggestion', {
97
96
clearTimeout ( this . loadTimeout ) ;
98
97
99
98
this . loadTimeout = setTimeout ( ( ) => {
100
- this . loadSuggestionCallback ( this . search , ( suggestions ) => {
99
+ this . $emit ( 'loadSuggestion' , this . search , ( suggestions ) => {
101
100
suggestions . forEach ( suggestion => {
102
101
if ( undefined === suggestion . selected ) {
103
102
suggestion . selected = false ;
@@ -122,7 +121,7 @@ Vue.component('suggestion', {
122
121
chooseSuggestion : function ( suggestion ) {
123
122
this . setCurrentSuggestion ( suggestion ) ;
124
123
this . closeSuggestionList ( ) ;
125
- this . chooseSuggestionCallback ( suggestion . label , suggestion . datas ) ;
124
+ this . $emit ( 'chooseSuggestion' , suggestion . label , suggestion . datas ) ;
126
125
} ,
127
126
suggestionListNavigation : function ( direction ) {
128
127
if ( this . currentSuggestion ) {
@@ -206,4 +205,6 @@ Vue.component('suggestion', {
206
205
}
207
206
} ,
208
207
}
209
- } ) ;
208
+ }
209
+
210
+ export { suggestion } ;
0 commit comments