@vue-interface/searchable-select-field 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Justin Kimbrell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Searchable Select Field
2
+
3
+ The `searchable-select-field` component provides flexible and customizable searchable input field with customizable sizes, states, and colors.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm i @vue-interface/searchable-select-field
9
+ ```
10
+
11
+ ```bash
12
+ yarn add @vue-interface/searchable-select-field
13
+ ```
14
+
15
+ ```bash
16
+ npm i @vue-interface/searchable-select-field
17
+ ```
18
+
19
+ ```bash
20
+ bun i @vue-interface/searchable-select-field
21
+ ```
22
+
23
+ ## Basic Usage
24
+
25
+ ```vue
26
+ <SearchableSelectField v-model="selected" :options="languages"></SearchableSelectField>
27
+ <SearchableSelectField :options="options" placeholder="Type to search..." label="Placeholder"></SearchableSelectField>
28
+ <SearchableSelectField :options="options" label="Descriptive Text Field" help-text="Some helpful text goes here."></SearchableSelectField>
29
+ <SearchableSelectField :options="options" label="Readonly" placeholder="Type something here..." readonly></SearchableSelectField>
30
+ <SearchableSelectField :options="options" label="Disabled" disabled></SearchableSelectField>
31
+ <SearchableSelectField :options="options" placeholder="Disabled" label="Disabled (Placeholder)" disabled></SearchableSelectField>
32
+ ```
33
+
34
+ ```ts
35
+ import SearchableSelectField from '../src/SearchableSelectField.vue';
36
+
37
+ const value = ref()
38
+ const selected = ref()
39
+ const options = [
40
+ 'Option 1',
41
+ 'Option 2',
42
+ 'Option 3',
43
+ 'Option 4',
44
+ 'Option 5',
45
+ 'Option 6',
46
+ 'Option 7',
47
+ 'Option 8',
48
+ 'Option 9',
49
+ 'Option 10'
50
+ ];
51
+ ```
52
+
53
+ For more comprehensive documentation and examples, please visit the [online documentation](https://vue-interface.github.io/packages/searchable-select-field/).
54
+
package/index.css ADDED
@@ -0,0 +1,4 @@
1
+ @import "tailwindcss";
2
+ @import "@vue-interface/form-control/css";
3
+
4
+ @import "./src/css/searchable-select-field.css";
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@vue-interface/searchable-select-field",
3
+ "version": "1.0.0",
4
+ "description": "A Vue searchable select field component.",
5
+ "type": "module",
6
+ "main": "./dist/searchable-select-field.umd.cjs",
7
+ "module": "./dist/searchable-select-field.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "source": "./index.ts",
12
+ "types": "./dist/index.d.ts",
13
+ "style": "./index.css",
14
+ "import": "./dist/searchable-select-field.js",
15
+ "require": "./dist/searchable-select-field.umd.cjs"
16
+ }
17
+ },
18
+ "browserslist": "last 2 versions, > 0.5%, ie >= 11",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/vue-interface/vue-interface.github.io"
22
+ },
23
+ "keywords": [
24
+ "Searchable",
25
+ "Select",
26
+ "Input",
27
+ "Field",
28
+ "Vue",
29
+ "Component",
30
+ "Tailwind"
31
+ ],
32
+ "author": "Justin Kimbrell",
33
+ "license": "MIT",
34
+ "bugs": {
35
+ "url": "https://github.com/vue-interface/vue-interface.github.io"
36
+ },
37
+ "homepage": "https://vue-interface.github.io/packages/searchable-select-field",
38
+ "readme": "README.md",
39
+ "files": [
40
+ "src",
41
+ "dist",
42
+ "index.css",
43
+ "README.md",
44
+ "LICENSE"
45
+ ],
46
+ "peerDependencies": {
47
+ "@heroicons/vue": "2.2.0",
48
+ "fuse.js": "^7.1.0",
49
+ "vue": "^3.3.4",
50
+ "@vue-interface/activity-indicator": "3.0.7",
51
+ "@vue-interface/form-control": "2.0.18",
52
+ "@vue-interface/input-field": "2.0.19",
53
+ "@vue-interface/sizeable": "2.0.0"
54
+ },
55
+ "scripts": {
56
+ "dev": "vite",
57
+ "build": "vue-tsc && vite build",
58
+ "preview": "vite preview"
59
+ }
60
+ }
@@ -0,0 +1,308 @@
1
+ <script setup lang="ts" generic="T, Value">
2
+ import { ChevronDownIcon, XMarkIcon } from '@heroicons/vue/24/outline';
3
+ import { ActivityIndicator, Pulse } from '@vue-interface/activity-indicator';
4
+ import type { FormControlEvents, FormControlProps, FormControlSlots } from '@vue-interface/form-control';
5
+ import { useFormControl } from '@vue-interface/form-control';
6
+ import { InputField } from '@vue-interface/input-field';
7
+ import Fuse, { IFuseOptions } from 'fuse.js';
8
+ import { InputHTMLAttributes, computed, nextTick, ref, useTemplateRef, watch, watchEffect } from 'vue';
9
+
10
+ const props = withDefaults(defineProps<SearchableSelectFieldProps<T,Value>>(), {
11
+ formControlClass: 'form-control',
12
+ labelClass: 'form-label',
13
+ size: 'form-control-md',
14
+ clearable: true,
15
+ options: () => []
16
+ });
17
+
18
+ const model = defineModel<T>();
19
+ const isInteractive = computed(() => !props.disabled && !props.readonly);
20
+
21
+ defineSlots<FormControlSlots<SearchableSelectFieldSizePrefix,T> & {
22
+ default(props: { option: T; display?: (option: T) => string }): any;
23
+ }>();
24
+
25
+ const emit = defineEmits<FormControlEvents>();
26
+
27
+ const {
28
+ controlAttributes,
29
+ formGroupClasses,
30
+ listeners
31
+ } = useFormControl<InputHTMLAttributes, SearchableSelectFieldSizePrefix, T|undefined, T>({ model, props, emit });
32
+
33
+ watchEffect(() => {
34
+ if(props.value !== undefined) {
35
+ model.value = props.value;
36
+ }
37
+ });
38
+
39
+ const input = ref<string>();
40
+ const showOptions = ref(false);
41
+ const active = ref<number>();
42
+ const buttons = useTemplateRef<HTMLButtonElement[]>('buttons');
43
+ const optionsEl = useTemplateRef<HTMLDivElement>('optionsEl');
44
+
45
+ const keys = computed(() => {
46
+ return typeof props.options === 'object' && props.options?.[0]
47
+ ? Object.keys(props.options?.[0])
48
+ : ['$'];
49
+ });
50
+
51
+ let fuse: Fuse<T> = createFuse();
52
+
53
+ function createFuse() {
54
+ return new Fuse(props.options ?? [], props.fuseOptions ?? {
55
+ includeScore: true,
56
+ threshold: .45,
57
+ keys: keys.value
58
+ });
59
+ }
60
+
61
+ const filtered = computed<T[]>(() => {
62
+ if(!input.value) {
63
+ return props.options ?? [];
64
+ }
65
+
66
+ const matches = fuse.search(input.value).map(({ item }) => item);
67
+
68
+ if(props.allowCustom && !matches.length) {
69
+ return props.options;
70
+ }
71
+
72
+ return matches;
73
+ });
74
+
75
+ watch(() => props.options, () => {
76
+ fuse = createFuse();
77
+ });
78
+
79
+ function scrollIntoView(child?: HTMLElement) {
80
+ const parent = optionsEl.value;
81
+
82
+ if(!parent || !child) {
83
+ return;
84
+ }
85
+
86
+ const parentRect = parent.getBoundingClientRect();
87
+ const childRect = child.getBoundingClientRect();
88
+
89
+ const childTop = childRect.top - parentRect.top + parent.scrollTop;
90
+
91
+ parent.scrollTop = childTop;
92
+ };
93
+
94
+ watch([input, active], ([input, active]) => {
95
+ if(input) {
96
+ buttons.value?.[0]?.scrollIntoView({
97
+ block: 'nearest',
98
+ inline: 'nearest'
99
+ });
100
+ }
101
+ else if(active !== undefined) {
102
+ buttons.value?.[active]?.scrollIntoView({
103
+ block: 'nearest',
104
+ inline: 'nearest'
105
+ });
106
+ }
107
+ });
108
+
109
+ watch(optionsEl, (value) => {
110
+ if(!value || active.value === undefined) {
111
+ return;
112
+ }
113
+
114
+ nextTick(() => {
115
+ scrollIntoView(buttons.value?.[active.value as number]);
116
+ });
117
+ });
118
+
119
+ function select(option?: T) {
120
+ model.value = option;
121
+ active.value = option && props.options.includes(option)
122
+ ? props.options.indexOf(option)
123
+ : undefined;
124
+ input.value = undefined;
125
+ showOptions.value = false;
126
+ }
127
+
128
+ function onInput(e: Event) {
129
+ if (!isInteractive.value) return;
130
+
131
+ showOptions.value = true;
132
+ active.value = undefined;
133
+ input.value = (e.target as HTMLInputElement)?.value;
134
+
135
+ if(!input.value && !props.allowCustom) {
136
+ model.value = undefined;
137
+ }
138
+
139
+ if(props.allowCustom) {
140
+ model.value = input.value as T;
141
+ }
142
+ }
143
+
144
+ function onKeypressEnter() {
145
+ if (!isInteractive.value) return;
146
+
147
+ if(!showOptions.value) {
148
+ showOptions.value = true;
149
+ return;
150
+ }
151
+
152
+ if(props.allowCustom && active.value === undefined) {
153
+ select(input.value as T ?? model.value);
154
+ }
155
+ else if(active.value === undefined) {
156
+ select(filtered.value[0]);
157
+ }
158
+ else if(filtered.value[active.value]) {
159
+ select(filtered.value[active.value]);
160
+ }
161
+ else {
162
+ select(undefined);
163
+ }
164
+ }
165
+
166
+ function onKeydownUp() {
167
+ if (!isInteractive.value) return;
168
+
169
+ showOptions.value = true;
170
+
171
+ if(!active.value) {
172
+ active.value = filtered.value.length - 1;
173
+ }
174
+ else {
175
+ active.value--;
176
+ }
177
+ }
178
+
179
+ function onKeydownDown() {
180
+ if (!isInteractive.value) return;
181
+
182
+ showOptions.value = true;
183
+
184
+ if(active.value === undefined || active.value === filtered.value.length - 1) {
185
+ active.value = 0;
186
+ }
187
+ else {
188
+ active.value++;
189
+ }
190
+ }
191
+
192
+ function onBlur() {
193
+ showOptions.value = false;
194
+ input.value = undefined;
195
+ }
196
+
197
+ function onClickOption(option: T) {
198
+ if (!isInteractive.value) return;
199
+ select(option);
200
+ }
201
+
202
+ function clear() {
203
+ if (!isInteractive.value) return;
204
+ input.value = undefined;
205
+ model.value = undefined;
206
+ }
207
+
208
+ const canClear = computed(() => {
209
+ return props.clearable && (!!input.value || !!model.value) && isInteractive.value;
210
+ });
211
+ </script>
212
+ <script lang="ts">
213
+ export type SearchableSelectFieldSizePrefix = 'form-control';
214
+
215
+ export type SearchableSelectFieldProps<ModelValue, Value> = FormControlProps<
216
+ InputHTMLAttributes,
217
+ SearchableSelectFieldSizePrefix,
218
+ ModelValue,
219
+ Value
220
+ > & {
221
+ options?: ModelValue[];
222
+ fuseOptions?: IFuseOptions<ModelValue>;
223
+ display?: (option: ModelValue) => string;
224
+ allowCustom?: boolean;
225
+ clearable?: boolean;
226
+ };
227
+ </script>
228
+
229
+
230
+ <template>
231
+ <div class="relative [&_.form-control]:pr-8">
232
+ <InputField
233
+ ref="field"
234
+ class="searchable-select-field-input"
235
+ :class="{ 'has-clear-button': canClear, formGroupClasses }"
236
+ :size="size"
237
+ v-bind="{ ...$attrs, controlAttributes, listeners }"
238
+ :name="name"
239
+ :label="label"
240
+ :model-value="input ?? (model && props?.display ? props?.display?.(model) : model)"
241
+ :disabled="disabled"
242
+ :readonly="readonly"
243
+ :color="color"
244
+ :error="error"
245
+ :errors="errors"
246
+ :feedback="feedback"
247
+ :valid="valid"
248
+ :invalid="invalid"
249
+ @click="isInteractive && (showOptions = true)"
250
+ @focus="isInteractive && (showOptions = true)"
251
+ @blur="onBlur"
252
+ @keypress.enter.prevent="onKeypressEnter"
253
+ @keydown.up.prevent="onKeydownUp"
254
+ @keydown.down.prevent="onKeydownDown"
255
+ @keyup.escape="showOptions = false"
256
+ @input="onInput">
257
+ <template #icon v-if="$slots.icon">
258
+ <slot name="icon" />
259
+ </template>
260
+ <template #activity>
261
+ <slot
262
+ name="activity"
263
+ v-bind="{ disabled, options, invalid, valid }">
264
+ <ActivityIndicator
265
+ v-if="!disabled && !options"
266
+ :type="Pulse"
267
+ size="activity-indicator-sm" />
268
+ <button
269
+ v-else-if="canClear"
270
+ type="button"
271
+ class="searchable-select-field-clear-button"
272
+ @click.stop="clear">
273
+ <XMarkIcon class="size-[1.25em]" />
274
+ </button>
275
+ <ChevronDownIcon
276
+ v-else-if="!invalid && !valid"
277
+ class="size-[1.25em]" />
278
+ </slot>
279
+ </template>
280
+ </InputField>
281
+ <div
282
+ v-if="showOptions && filtered.length"
283
+ ref="optionsEl"
284
+ tabindex="-1"
285
+ class="searchable-select-field-dropdown"
286
+ :class="size"
287
+ @mousedown.prevent.stop>
288
+ <button
289
+ v-for="(option, i) in filtered"
290
+ ref="buttons"
291
+ :key="i"
292
+ type="button"
293
+ tabindex="-1"
294
+ :title="display?.(option) ?? String(option)"
295
+ :class="{
296
+ ['bg-neutral-200 dark:bg-neutral-700']: active === i
297
+ }"
298
+ @mousedown.prevent
299
+ @click="onClickOption(option)">
300
+ <slot v-bind="{ option, display }">
301
+ <div class="truncate">
302
+ {{ display?.(option) ?? option }}
303
+ </div>
304
+ </slot>
305
+ </button>
306
+ </div>
307
+ </div>
308
+ </template>
@@ -0,0 +1,129 @@
1
+ @import "tailwindcss";
2
+
3
+ @theme {
4
+ /* Searchable Select Field Dropdown */
5
+ --searchable-select-field-dropdown-display: flex;
6
+ --searchable-select-field-dropdown-flex-direction: column;
7
+ --searchable-select-field-dropdown-position: absolute;
8
+ --searchable-select-field-dropdown-z-index: 10;
9
+ --searchable-select-field-dropdown-width: 100%;
10
+ --searchable-select-field-dropdown-max-height: 18rem;
11
+ --searchable-select-field-dropdown-margin-top: 0.25rem;
12
+ --searchable-select-field-dropdown-overflow: auto;
13
+ --searchable-select-field-dropdown-border-width: 1px;
14
+ --searchable-select-field-dropdown-border-radius: 0.25rem;
15
+ --searchable-select-field-dropdown-border-color: var(--color-neutral-300, oklch(87% 0 0));
16
+ --searchable-select-field-dropdown-background-color: var(--color-white, #fff);
17
+ --searchable-select-field-dropdown-color: var(--color-black, #000);
18
+ --searchable-select-field-dropdown-box-shadow-style: 0 10px 15px -3px, 0 4px 6px -4px;
19
+ --searchable-select-field-dropdown-box-shadow-color: rgb(0 0 0 / 0.1), rgb(0 0 0 / 0.1);
20
+
21
+ --searchable-select-field-dropdown-dark-border-color: var(--color-neutral-600, oklch(43.9% 0 0));
22
+ --searchable-select-field-dropdown-dark-background-color: var(--color-neutral-900, oklch(20.5% 0 0));
23
+ --searchable-select-field-dropdown-dark-color: var(--color-white, #fff);
24
+
25
+ --searchable-select-field-dropdown-text-align: left;
26
+ --searchable-select-field-dropdown-padding: 0.5em 1em;
27
+ --searchable-select-field-dropdown-border-bottom-width: 1px;
28
+ --searchable-select-field-dropdown-hover-background-color: var(--color-neutral-200, oklch(92.2% 0 0));
29
+ --searchable-select-field-dropdown-dark-hover-background-color: var(--color-neutral-700, oklch(37.1% 0 0));
30
+
31
+ /* Searchable Select Field Input */
32
+ --searchable-select-field-input-position: relative;
33
+ --searchable-select-field-input-activity-indicator-position: absolute;
34
+ --searchable-select-field-input-activity-indicator-top: 50%;
35
+ --searchable-select-field-input-activity-indicator-right: 0;
36
+ --searchable-select-field-input-activity-indicator-transform: translate(-1rem, -50%);
37
+
38
+ /* Searchable Select-field Clear Button */
39
+ --searchable-select-field-clear-button-display: flex;
40
+ --searchable-select-field-clear-button-cursor: pointer;
41
+ --searchable-select-field-clear-button-color: var(--color-black, #000);
42
+ --searchable-select-field-clear-button-hover-color: var(--color-neutral-600, oklch(43.9% 0 0));
43
+
44
+ --searchable-select-field-clear-button-dark-color: var(--color-white, #fff);
45
+ --searchable-select-field-clear-button-dark-hover-color: var(--color-neutral-400, oklch(70.8% 0 0));
46
+ }
47
+
48
+ @utility searchable-select-field-dropdown {
49
+ display: var(--searchable-select-field-dropdown-display);
50
+ flex-direction: var(--searchable-select-field-dropdown-flex-direction);
51
+ position: var(--searchable-select-field-dropdown-position);
52
+ z-index: var(--searchable-select-field-dropdown-z-index);
53
+ width: var(--searchable-select-field-dropdown-width);
54
+ max-height: var(--searchable-select-field-dropdown-max-height);
55
+ margin-top: var(--searchable-select-field-dropdown-margin-top);
56
+ overflow: var(--searchable-select-field-dropdown-overflow);
57
+ border-width: var(--searchable-select-field-dropdown-border-width);
58
+ border-radius: var(--searchable-select-field-dropdown-border-radius);
59
+ border-color: var(--searchable-select-field-dropdown-border-color);
60
+ background-color: var(--searchable-select-field-dropdown-background-color);
61
+ color: var(--searchable-select-field-dropdown-color);
62
+ box-shadow: var(--searchable-select-field-dropdown-box-shadow-style) var(--searchable-select-field-dropdown-box-shadow-color);
63
+
64
+ @variant dark {
65
+ border-color: var(--searchable-select-field-dropdown-dark-border-color);
66
+ background-color: var(--searchable-select-field-dropdown-dark-background-color);
67
+ color: var(--searchable-select-field-dropdown-dark-color);
68
+ }
69
+
70
+ & > button {
71
+ text-align: var(--searchable-select-field-dropdown-text-align);
72
+ padding: var(--searchable-select-field-dropdown-padding);
73
+ border-color: var(--searchable-select-field-dropdown-border-color);
74
+
75
+ &:not(:last-child) {
76
+ border-bottom-width: var(--searchable-select-field-dropdown-border-bottom-width);
77
+ }
78
+
79
+ &:hover {
80
+ background-color: var(--searchable-select-field-dropdown-hover-background-color);
81
+ }
82
+
83
+ @variant dark {
84
+ border-color: var(--searchable-select-field-dropdown-dark-border-color);
85
+
86
+ &:hover {
87
+ background-color: var(--searchable-select-field-dropdown-dark-hover-background-color);
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ @utility searchable-select-field-input {
94
+ position: var(--searchable-select-field-input-position);
95
+
96
+ & .form-control-inner {
97
+ position: var(--searchable-select-field-input-position);
98
+ }
99
+
100
+ & .form-control-activity-indicator {
101
+ position: var(--searchable-select-field-input-activity-indicator-position);
102
+ top: var(--searchable-select-field-input-activity-indicator-top);
103
+ right: var(--searchable-select-field-input-activity-indicator-right);
104
+ transform: var(--searchable-select-field-input-activity-indicator-transform);
105
+ }
106
+
107
+ &.has-clear-button .form-control.is-valid,
108
+ &.has-clear-button .form-control.is-invalid {
109
+ background-image: none;
110
+ }
111
+ }
112
+
113
+ @utility searchable-select-field-clear-button {
114
+ display: var(--searchable-select-field-clear-button-display);
115
+ cursor: var(--searchable-select-field-clear-button-cursor);
116
+ color: var(--searchable-select-field-clear-button-color);
117
+
118
+ &:hover {
119
+ color: var(--searchable-select-field-clear-button-hover-color);
120
+ }
121
+
122
+ @variant dark {
123
+ color: var(--searchable-select-field-clear-button-dark-color);
124
+
125
+ &:hover {
126
+ color: var(--searchable-select-field-clear-button-dark-hover-color);
127
+ }
128
+ }
129
+ }