Skip to content

Commit f755800

Browse files
authored
Merge pull request #657 from AnthonyANI/keep-search-setting
Added keepSearch setting to avoid clearing search input upon close
2 parents 4524781 + 9993381 commit f755800

File tree

8 files changed

+126
-6
lines changed

8 files changed

+126
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ new SlimSelect({
140140
alwaysOpen: false, // Keep dropdown always open
141141
showSearch: true, // Show search input
142142
focusSearch: true, // Auto focus search on open
143+
keepSearch: false, // Keep search input value when dropdown closes
143144
ariaLabel: 'Combobox', // ARIA label for accessibility
144145
searchPlaceholder: 'Search', // Search input placeholder
145146
searchText: 'No Results', // Text when no results found

src/docs/app.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export default defineComponent({
102102
{ text: 'keepOrder', value: 'settings#keepOrder' },
103103
{ text: 'showSearch', value: 'settings#showSearch' },
104104
{ text: 'focusSearch', value: 'settings#focusSearch' },
105+
{ text: 'keepSearch', value: 'settings#keepSearch' },
105106
{ text: 'searchText', value: 'settings#searchText' },
106107
{ text: 'searchPlaceholder', value: 'settings#searchPlaceholder' },
107108
{ text: 'searchHighlight', value: 'settings#searchHighlight' },

src/docs/pages/settings/index.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import OpenPosition from './open_position.vue'
2525
import Placeholder from './placeholder.vue'
2626
import ShowSearch from './show_search.vue'
2727
import FocusSearch from './focus_search.vue'
28+
import KeepSearch from './keep_search.vue'
2829
import SearchText from './search_text.vue'
2930
import SearchPlaceholder from './search_placeholder.vue'
3031
import SearchHighlight from './search_highlight.vue'
@@ -55,6 +56,7 @@ export default defineComponent({
5556
KeepOrder,
5657
ShowSearch,
5758
FocusSearch,
59+
KeepSearch,
5860
SearchText,
5961
SearchPlaceholder,
6062
SearchHighlight,
@@ -106,21 +108,22 @@ export default defineComponent({
106108
<AdSlot ad-slot="1270131515" />
107109

108110
<FocusSearch />
109-
<SearchText />
111+
<KeepSearch />
110112
<AdSlot ad-slot="1270131515" />
111113

114+
<SearchText />
112115
<SearchPlaceholder />
113-
<SearchHighlight />
114116
<AdSlot ad-slot="1270131515" />
115117

118+
<SearchHighlight />
116119
<CloseOnSelect />
117-
<ShowTooltip />
118120
<AdSlot ad-slot="1270131515" />
119121

122+
<ShowTooltip />
120123
<Closable />
121-
<HideSelected />
122124
<AdSlot ad-slot="1270131515" />
123125

126+
<HideSelected />
124127
<MaxValuesShown />
125128
</div>
126129
</template>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<script lang="ts">
2+
import { defineComponent } from 'vue'
3+
import ShikiStyle from '../../components/shiki_style.vue'
4+
5+
import SlimSelect from '@/slim-select'
6+
7+
export default defineComponent({
8+
name: 'KeepSearch',
9+
mounted() {
10+
// Single
11+
new SlimSelect({
12+
select: this.$refs.keepSearchSingle as HTMLSelectElement,
13+
settings: {
14+
keepSearch: true
15+
}
16+
})
17+
18+
// Multiple
19+
new SlimSelect({
20+
select: this.$refs.keepSearchMulti as HTMLSelectElement,
21+
settings: {
22+
keepSearch: true
23+
}
24+
})
25+
},
26+
components: {
27+
ShikiStyle
28+
}
29+
})
30+
</script>
31+
32+
<template>
33+
<div id="keepSearch" class="content">
34+
<h2 class="header">keepSearch</h2>
35+
<p>
36+
The keepSearch setting controls whether the search input text is preserved when the dropdown closes. When enabled,
37+
any text entered in the search field will remain when you reopen the dropdown, allowing you to continue your
38+
previous search.
39+
</p>
40+
<p>
41+
By default, the search field is cleared each time the dropdown closes. Enabling this setting is useful when users
42+
need to maintain their last search and results, such as when picking multiple selections in a multi-select
43+
dropdown.
44+
</p>
45+
46+
<div class="row" style="padding: 0 0 var(--spacing-half) 0">
47+
<select ref="keepSearchSingle">
48+
<option value="dog">Dog</option>
49+
<option value="cat">Cat</option>
50+
<option value="bird">Bird</option>
51+
<option value="fish">Fish</option>
52+
<option value="hamster">Hamster</option>
53+
<option value="rabbit">Rabbit</option>
54+
</select>
55+
56+
<select ref="keepSearchMulti" multiple>
57+
<option value="dog">Dog</option>
58+
<option value="cat">Cat</option>
59+
<option value="bird">Bird</option>
60+
<option value="fish">Fish</option>
61+
<option value="hamster">Hamster</option>
62+
<option value="rabbit">Rabbit</option>
63+
</select>
64+
</div>
65+
66+
<ShikiStyle language="javascript">
67+
<pre>
68+
new SlimSelect({
69+
select: '#selectElement',
70+
settings: {
71+
keepSearch: true
72+
}
73+
})
74+
</pre>
75+
</ShikiStyle>
76+
</div>
77+
</template>

src/slim-select/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,41 @@ describe('SlimSelect Module', () => {
503503
expect(slim.render.content.search.input.value).toBe('')
504504
})
505505

506+
test('should keep search input when keepSearch is true', async () => {
507+
// Create new instance with keepSearch enabled
508+
const select = document.createElement('select')
509+
select.innerHTML = `
510+
<option value="a">A</option>
511+
<option value="b">B</option>
512+
<option value="c">C</option>
513+
`
514+
document.body.appendChild(select)
515+
516+
const slimWithKeepSearch = new SlimSelect({
517+
select: select,
518+
settings: {
519+
keepSearch: true
520+
}
521+
})
522+
523+
// Open dropdown
524+
slimWithKeepSearch.open()
525+
526+
// Search for something
527+
slimWithKeepSearch.search('test')
528+
expect(slimWithKeepSearch.render.content.search.input.value).toBe('test')
529+
530+
// Close dropdown
531+
slimWithKeepSearch.close()
532+
533+
// Search input should NOT be cleared when keepSearch is true
534+
expect(slimWithKeepSearch.render.content.search.input.value).toBe('test')
535+
536+
// Cleanup
537+
slimWithKeepSearch.destroy()
538+
document.body.removeChild(select)
539+
})
540+
506541
test('should persist search results in store across close/open cycles', () => {
507542
// Search for something
508543
slim.search('te')

src/slim-select/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ export default class SlimSelect {
419419
// Tell render to close
420420
this.render.close()
421421

422-
// Clear search only if not empty
423-
if (this.render.content.search.input.value !== '') {
422+
// Clear search only if not empty and keepSearch is false
423+
if (!this.settings.keepSearch && this.render.content.search.input.value !== '') {
424424
this.search('') // Clear search
425425
}
426426

src/slim-select/settings.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaultSettings: { [key: string]: any } = {
1717
alwaysOpen: false,
1818
showSearch: true,
1919
focusSearch: true,
20+
keepSearch: false,
2021
ariaLabel: 'Combobox',
2122
searchPlaceholder: 'Search...',
2223
searchText: 'No Results',

src/slim-select/settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default class Settings {
1616
public alwaysOpen: boolean
1717
public showSearch: boolean
1818
public focusSearch: boolean
19+
public keepSearch: boolean
1920
public ariaLabel: string
2021
public searchPlaceholder: string
2122
public searchText: string
@@ -50,6 +51,7 @@ export default class Settings {
5051
this.alwaysOpen = settings.alwaysOpen !== undefined ? settings.alwaysOpen : false
5152
this.showSearch = settings.showSearch !== undefined ? settings.showSearch : true
5253
this.focusSearch = settings.focusSearch !== undefined ? settings.focusSearch : true
54+
this.keepSearch = settings.keepSearch !== undefined ? settings.keepSearch : false
5355
this.ariaLabel = settings.ariaLabel || 'Combobox'
5456
this.searchPlaceholder = settings.searchPlaceholder || 'Search...'
5557
this.searchText = settings.searchText || 'No Results'

0 commit comments

Comments
 (0)