Skip to content

Commit 1e56d66

Browse files
committed
fix: added extra options
1 parent b5f9071 commit 1e56d66

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/SearchInput.vue

+43-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div v-bind="attrsStyles">
3-
<slot name="search-icon">
3+
<slot v-if="searchIcon" name="search-icon">
44
<div class="search-icon search">
55
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
66
<path
@@ -24,7 +24,7 @@
2424
@blur="hasFocus = false"
2525
@keydown="onKeydown"
2626
/>
27-
<slot v-if="!hasFocus && modelValue.length === 0" name="shortcut-icon">
27+
<slot v-if="shortcutIcon && !hasFocus && modelValue.length === 0" name="shortcut-icon">
2828
<div class="search-icon shortcut" title='Press "/" to search'>
2929
<svg width="16" height="20" viewBox="4 4 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
3030
<path
@@ -36,7 +36,7 @@
3636
</svg>
3737
</div>
3838
</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">
4040
<div class="search-icon clear" @mousedown="clear">
4141
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
4242
<path
@@ -50,7 +50,7 @@
5050
</template>
5151

5252
<script lang="ts">
53-
import { defineComponent, PropType, ref, computed, onBeforeUnmount } from 'vue'
53+
import { defineComponent, PropType, ref, computed, onBeforeUnmount, watch } from 'vue'
5454
import { fieldType, FieldType } from './SearchInput.types'
5555
5656
const filterObject = (obj: { [key: string]: unknown }, properties: (string | number)[], remove = true) => {
@@ -66,6 +66,8 @@ const filterObject = (obj: { [key: string]: unknown }, properties: (string | num
6666
return res
6767
}
6868
69+
const defaultBoolean = (val = true) => ({ type: Boolean, default: val })
70+
6971
export default defineComponent({
7072
inheritAttrs: false,
7173
props: {
@@ -77,7 +79,18 @@ export default defineComponent({
7779
modelValue: {
7880
type: String,
7981
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()
8194
},
8295
emits: ['update:modelValue'],
8396
setup(props, { emit, attrs }) {
@@ -86,7 +99,11 @@ export default defineComponent({
8699
87100
const attrsWithoutStyles = computed(() => filterObject(attrs, ['class', 'style']))
88101
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+
})
90107
91108
const clear = () => {
92109
emit('update:modelValue', '')
@@ -97,10 +114,12 @@ export default defineComponent({
97114
}
98115
99116
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+
}
104123
}
105124
}
106125
@@ -125,10 +144,22 @@ export default defineComponent({
125144
}
126145
}
127146
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+
)
129160
130161
onBeforeUnmount(() => {
131-
window.document.removeEventListener('keydown', onDocumentKeydown)
162+
removeDocumentKeydown()
132163
})
133164
134165
return {

0 commit comments

Comments
 (0)