Skip to content

Commit 2398b6f

Browse files
committed
fix: fixed select on ficus, added hideShortcutIconOnBlur prop
1 parent 9b2c9c0 commit 2398b6f

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/SearchInput.vue

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
@blur="hasFocus = false"
1515
@keydown="onKeydown"
1616
/>
17-
<slot v-if="shortcutIcon && !hasFocus && modelValue.length === 0" name="shortcut-icon">
17+
<slot v-if="showShortcutIcon" name="shortcut-icon">
1818
<i class="search-icon shortcut" title='Press "/" to search'></i>
1919
</slot>
20-
<slot v-if="clearIcon && modelValue.length > 0" name="clear-icon" :clear="clear">
20+
<slot v-if="showClearIcon" name="clear-icon" :clear="clear">
2121
<i class="search-icon clear" @mousedown="clear"></i>
2222
</slot>
2323
</div>
@@ -61,6 +61,7 @@ export default defineComponent({
6161
searchIcon: defaultBoolean(),
6262
shortcutIcon: defaultBoolean(),
6363
clearIcon: defaultBoolean(),
64+
hideShortcutIconOnBlur: defaultBoolean(),
6465
clearOnEsc: defaultBoolean(),
6566
blurOnEsc: defaultBoolean(),
6667
selectOnFocus: defaultBoolean(),
@@ -82,6 +83,14 @@ export default defineComponent({
8283
return res
8384
})
8485
86+
const showClearIcon = computed(() => !!(props.clearIcon && props.modelValue.length > 0))
87+
88+
const showShortcutIcon = computed(() => {
89+
if (props.shortcutIcon && !hasFocus.value && !props.hideShortcutIconOnBlur) return true
90+
if (props.shortcutIcon && !hasFocus.value && props.modelValue.length === 0) return true
91+
return false
92+
})
93+
8594
const clear = () => {
8695
emit('update:modelValue', '')
8796
}
@@ -117,7 +126,7 @@ export default defineComponent({
117126
})
118127
const elToFocus = allVisibleSearchInputs.length > 1 ? allVisibleSearchInputs[0] : inputRef.value
119128
elToFocus?.focus()
120-
elToFocus?.select()
129+
if (props.selectOnFocus) elToFocus?.select()
121130
}
122131
}
123132
@@ -146,7 +155,9 @@ export default defineComponent({
146155
onInput,
147156
onKeydown,
148157
attrsStyles,
149-
attrsWithoutStyles
158+
attrsWithoutStyles,
159+
showClearIcon,
160+
showShortcutIcon
150161
}
151162
}
152163
})

tests/searchInput.spec.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('SearchInput.vue', () => {
108108
expect(wrapper.emitted()['update:modelValue'][0]).toEqual([''])
109109
})
110110

111-
it('esc key clears the value', async () => {
111+
it('clears the value with the "esc" key', async () => {
112112
const wrapper = createWrapper({
113113
props: {
114114
modelValue: 'test'
@@ -187,4 +187,16 @@ describe('SearchInput.vue', () => {
187187

188188
expect(inputs[0].element).toBe(document.activeElement)
189189
})
190+
191+
it('should render a shortcut icon when the hideShortcutIconOnBlur prop is false', async () => {
192+
const wrapper = createWrapper({
193+
props: {
194+
hideShortcutIconOnBlur: false
195+
}
196+
})
197+
198+
const i = await wrapper.find('i.search-icon.shortcut')
199+
200+
expect(i).toBeTruthy()
201+
})
190202
})

0 commit comments

Comments
 (0)