1
1
<template >
2
2
<div v-bind =" attrsStyles" >
3
- <slot name =" search-icon" >
3
+ <slot v-if = " searchIcon " name =" search-icon" >
4
4
<div class =" search-icon search" >
5
5
<svg width =" 20" height =" 20" viewBox =" 0 0 24 24" fill =" none" xmlns =" http://www.w3.org/2000/svg" >
6
6
<path
24
24
@blur =" hasFocus = false"
25
25
@keydown =" onKeydown"
26
26
/>
27
- <slot v-if =" !hasFocus && modelValue.length === 0" name =" shortcut-icon" >
27
+ <slot v-if =" shortcutIcon && !hasFocus && modelValue.length === 0" name =" shortcut-icon" >
28
28
<div class =" search-icon shortcut" title =' Press "/" to search' >
29
29
<svg width =" 16" height =" 20" viewBox =" 4 4 14 14" fill =" none" xmlns =" http://www.w3.org/2000/svg" >
30
30
<path
36
36
</svg >
37
37
</div >
38
38
</slot >
39
- <slot v-if =" modelValue.length > 0" name =" clear-icon" :clear =" clear" >
39
+ <slot v-if =" clearIcon && modelValue.length > 0" name =" clear-icon" :clear =" clear" >
40
40
<div class =" search-icon clear" @mousedown =" clear" >
41
41
<svg width =" 18" height =" 18" viewBox =" 0 0 24 24" fill =" none" xmlns =" http://www.w3.org/2000/svg" >
42
42
<path
50
50
</template >
51
51
52
52
<script lang="ts">
53
- import { defineComponent , PropType , ref , computed , onBeforeUnmount } from ' vue'
53
+ import { defineComponent , PropType , ref , computed , onBeforeUnmount , watch } from ' vue'
54
54
import { fieldType , FieldType } from ' ./SearchInput.types'
55
55
56
56
const filterObject = (obj : { [key : string ]: unknown }, properties : (string | number )[], remove = true ) => {
@@ -66,6 +66,8 @@ const filterObject = (obj: { [key: string]: unknown }, properties: (string | num
66
66
return res
67
67
}
68
68
69
+ const defaultBoolean = (val = true ) => ({ type: Boolean , default: val })
70
+
69
71
export default defineComponent ({
70
72
inheritAttrs: false ,
71
73
props: {
@@ -77,7 +79,18 @@ export default defineComponent({
77
79
modelValue: {
78
80
type: String ,
79
81
default: ' '
80
- }
82
+ },
83
+ wrapperClass: {
84
+ type: String ,
85
+ default: ' search-input-wrapper'
86
+ },
87
+ searchIcon: defaultBoolean (),
88
+ shortcutIcon: defaultBoolean (),
89
+ clearIcon: defaultBoolean (),
90
+ escapeEnabled: defaultBoolean (),
91
+ clearOnEsc: defaultBoolean (),
92
+ blurOnEsc: defaultBoolean (),
93
+ selectOnFocus: defaultBoolean ()
81
94
},
82
95
emits: [' update:modelValue' ],
83
96
setup(props , { emit , attrs }) {
@@ -86,7 +99,11 @@ export default defineComponent({
86
99
87
100
const attrsWithoutStyles = computed (() => filterObject (attrs , [' class' , ' style' ]))
88
101
89
- const attrsStyles = computed (() => filterObject (attrs , [' class' , ' style' ], false ))
102
+ const attrsStyles = computed (() => {
103
+ const res = filterObject (attrs , [' class' , ' style' ], false )
104
+ if (! res .class ) res .class = props .wrapperClass
105
+ return res
106
+ })
90
107
91
108
const clear = () => {
92
109
emit (' update:modelValue' , ' ' )
@@ -97,10 +114,12 @@ export default defineComponent({
97
114
}
98
115
99
116
const onKeydown = (e : KeyboardEvent ) => {
100
- if (e .key === ' Escape' ) {
101
- clear ()
102
- const el = inputRef .value as HTMLInputElement
103
- el .blur ()
117
+ if (props .escapeEnabled && e .key === ' Escape' ) {
118
+ props .clearOnEsc && clear ()
119
+ if (props .blurOnEsc ) {
120
+ const el = inputRef .value as HTMLInputElement
121
+ el .blur ()
122
+ }
104
123
}
105
124
}
106
125
@@ -125,10 +144,22 @@ export default defineComponent({
125
144
}
126
145
}
127
146
128
- window .document .addEventListener (' keydown' , onDocumentKeydown )
147
+ const removeDocumentKeydown = () => window .document .removeEventListener (' keydown' , onDocumentKeydown )
148
+
149
+ watch (
150
+ () => props .shortcutIcon ,
151
+ (nV ) => {
152
+ if (nV ) {
153
+ window .document .addEventListener (' keydown' , onDocumentKeydown )
154
+ } else {
155
+ removeDocumentKeydown ()
156
+ }
157
+ },
158
+ { immediate: true }
159
+ )
129
160
130
161
onBeforeUnmount (() => {
131
- window . document . removeEventListener ( ' keydown ' , onDocumentKeydown )
162
+ removeDocumentKeydown ( )
132
163
})
133
164
134
165
return {
0 commit comments