tam_select 1.2.5 → 1.4.1

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.
@@ -0,0 +1,21 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import TamSelect from "tam-select"
3
+
4
+ export default class TamSelectController extends Controller<HTMLSelectElement> {
5
+ instance: TamSelect | null
6
+ readonly optionsValue: Record<string, unknown>
7
+ /** Options passed to TamSelect. Override in a subclass to add app defaults. */
8
+ readonly tamSelectOptions: import("tam-select").TamSelectOptions | Record<string, unknown>
9
+
10
+ connect(): void
11
+ disconnect(): void
12
+ optionsValueChanged(): void
13
+ refresh(): void
14
+ open(): void
15
+ close(): void
16
+ focus(): void
17
+ clear(): void
18
+ selectAll(): void
19
+ deselectAll(): void
20
+ setValue(event: { params: { value: string | string[] | null } }): void
21
+ }
@@ -7,10 +7,39 @@ export default class extends Controller {
7
7
  }
8
8
 
9
9
  connect() {
10
- this.instance = TamSelect.getInstance(this.element) || new TamSelect(this.element, this.optionsValue)
10
+ this.instance = TamSelect.getInstance(this.element) || new TamSelect(this.element, this.tamSelectOptions)
11
+ // Turbo snapshots the page before Stimulus disconnects. Restore the native
12
+ // select first so a cached page never holds a dead, duplicate control.
13
+ this.beforeCache = () => this.teardown()
14
+ // A frame navigation promoted with data-turbo-action="advance" also caches
15
+ // a snapshot, but keeps this page. Rebuild the control once it loads.
16
+ this.afterLoad = () => {
17
+ if (!this.instance && this.element.isConnected) {
18
+ this.instance = TamSelect.getInstance(this.element) || new TamSelect(this.element, this.tamSelectOptions)
19
+ }
20
+ }
21
+ document.addEventListener("turbo:before-cache", this.beforeCache)
22
+ document.addEventListener("turbo:load", this.afterLoad)
11
23
  }
12
24
 
13
25
  disconnect() {
26
+ document.removeEventListener("turbo:before-cache", this.beforeCache)
27
+ document.removeEventListener("turbo:load", this.afterLoad)
28
+ this.teardown()
29
+ }
30
+
31
+ // The options passed to TamSelect. Override in a subclass to add app-wide
32
+ // defaults, a theme, or computed options:
33
+ //
34
+ // import TamSelectController from "tam-select/stimulus"
35
+ // export default class extends TamSelectController {
36
+ // get tamSelectOptions() { return { theme: "select2", ...this.optionsValue } }
37
+ // }
38
+ get tamSelectOptions() {
39
+ return this.optionsValue
40
+ }
41
+
42
+ teardown() {
14
43
  this.instance?.destroy()
15
44
  this.instance = null
16
45
  }
@@ -18,14 +47,38 @@ export default class extends Controller {
18
47
  optionsValueChanged() {
19
48
  if (!this.instance) return
20
49
  this.instance.destroy()
21
- this.instance = new TamSelect(this.element, this.optionsValue)
50
+ this.instance = new TamSelect(this.element, this.tamSelectOptions)
22
51
  }
23
52
 
24
53
  refresh() {
25
54
  this.instance?.refresh()
26
55
  }
27
56
 
57
+ open() {
58
+ this.instance?.open()
59
+ }
60
+
61
+ close() {
62
+ this.instance?.close()
63
+ }
64
+
65
+ focus() {
66
+ this.instance?.focus()
67
+ }
68
+
28
69
  clear() {
29
70
  this.instance?.clear()
30
71
  }
72
+
73
+ selectAll() {
74
+ this.instance?.selectAll()
75
+ }
76
+
77
+ deselectAll() {
78
+ this.instance?.deselectAll()
79
+ }
80
+
81
+ setValue({ params }) {
82
+ this.instance?.setValue(params.value)
83
+ }
31
84
  }
@@ -0,0 +1,347 @@
1
+ export interface TamSelectItem {
2
+ id: string
3
+ value: string
4
+ label: string
5
+ detail: string | null
6
+ meta: string | null
7
+ image: string | null
8
+ disabled: boolean
9
+ selected: boolean
10
+ option: HTMLOptionElement
11
+ group: string | null
12
+ }
13
+
14
+ export interface TamSelectVisibleEntry {
15
+ type: "item" | "group-header" | "create"
16
+ id: string
17
+ item?: TamSelectItem
18
+ group?: string
19
+ label?: string
20
+ value?: string
21
+ disabled?: boolean
22
+ }
23
+
24
+ export interface TamSelectPagination {
25
+ page?: number
26
+ next_page?: number | null
27
+ has_more?: boolean
28
+ total_pages?: number
29
+ [key: string]: unknown
30
+ }
31
+
32
+ export interface TamSelectTemplateContext {
33
+ tamSelect: TamSelect
34
+ context: "result" | "selection"
35
+ }
36
+
37
+ export interface TamSelectTransportOptions {
38
+ url: URL
39
+ query: string
40
+ page: number
41
+ headers: Record<string, string>
42
+ signal: AbortSignal
43
+ tamSelect: TamSelect
44
+ }
45
+
46
+ export interface TamSelectLanguage {
47
+ placeholder?: string
48
+ searchPlaceholder?: string
49
+ noResults?: string
50
+ create?: string | ((query: string) => string)
51
+ loading?: string
52
+ loadMore?: string
53
+ inputTooShort?: string | ((remaining: number) => string)
54
+ results?: string | ((count: number) => string)
55
+ clear?: string
56
+ remove?: string | ((item: TamSelectItem) => string)
57
+ selectionLimit?: string | ((limit: number) => string)
58
+ selectAll?: string | ((matchCount: number) => string)
59
+ deselectAll?: string | ((selectedMatchCount: number) => string)
60
+ selectionCount?: string | ((count: number, limit: number) => string)
61
+ }
62
+
63
+ export interface TamSelectOptions {
64
+ /** Enable text filtering. Default: `true` */
65
+ searchable?: boolean
66
+ /** Allow typed values to become options. Default: `false` */
67
+ creatable?: boolean
68
+ /** Display the clear button. A single select needs an empty (`value=""`) option to clear to. Default: `true` */
69
+ clearable?: boolean
70
+ /** Keep dropdown open after selection in multi-select. Default: `!select.multiple` */
71
+ closeAfterSelect?: boolean
72
+ /** Placeholder text for the closed control. Default: native prompt or `"Select…"` */
73
+ placeholder?: string
74
+ /** Placeholder for the search input. Default: `"Search…"` */
75
+ searchPlaceholder?: string
76
+ /** Text shown when no results match. Default: `"No results found"` */
77
+ noResultsText?: string
78
+ /** Template for the create action. Default: `` (query) => `Create "${query}"` `` */
79
+ createText?: string | ((query: string) => string)
80
+ /** Text shown while loading remote data. Default: `"Loading…"` */
81
+ loadingText?: string
82
+ /** Text shown at the bottom for infinite scroll. Default: `"Load more"` */
83
+ loadMoreText?: string
84
+ /** Message when input is too short. Default: `(remaining) => ...` */
85
+ inputTooShortText?: string | ((remaining: number) => string)
86
+ /** Remote search endpoint URL. Default: `null` */
87
+ remoteUrl?: string | null
88
+ /** Query parameter name for remote search. Default: `"q"` */
89
+ queryParam?: string
90
+ /** Query parameter name for pagination. Default: `"page"` */
91
+ pageParam?: string
92
+ /** Debounce delay in ms for remote requests. Default: `250` */
93
+ debounce?: number
94
+ /** Minimum characters before remote search fires. Default: `0` */
95
+ minQueryLength?: number
96
+ /** Enable fuzzy (typo-tolerant) matching. Default: `true` */
97
+ fuzzySearch?: boolean
98
+ /** Highlight matching text in results. Default: `true` */
99
+ highlightMatches?: boolean
100
+ /** Rank stronger matches first. Default: `true` */
101
+ sortByRelevance?: boolean
102
+ /** Fields searched in local mode. Default: `["label", "detail", "meta"]` */
103
+ searchFields?: string[]
104
+ /** Function or text for result count announcement. Default: `(count) => ...` */
105
+ resultsText?: string | ((count: number) => string)
106
+ /** Localized labels and messages. Explicit top-level text options take precedence. */
107
+ language?: TamSelectLanguage
108
+ /** Key for value in remote items. Default: `"value"` */
109
+ valueField?: string
110
+ /** Key for label in remote items. Default: `"label"` */
111
+ labelField?: string
112
+ /** Key for image URL in remote items. Default: `"image"` */
113
+ imageField?: string
114
+ /** Dot-separated path to items in remote response. Default: `"items"` */
115
+ itemsPath?: string
116
+ /** Dot-separated path to pagination in remote response. Default: `"pagination"` */
117
+ paginationPath?: string
118
+ /** Custom local matcher `(item, query) => boolean`. Default: `null` */
119
+ matcher?: ((item: TamSelectItem, query: string) => boolean) | null
120
+ /** Visual theme: `"default"`, `"select2"`, or `"auto"`. Default: `"auto"` */
121
+ theme?: "default" | "select2" | "auto"
122
+ /** Override any Tailwind class group. Default: `{}` */
123
+ classes?: Record<string, string>
124
+ /** Custom headers for remote requests. Default: `{}` */
125
+ headers?: Record<string, string>
126
+ /** Smooth dropdown open/close transitions. Default: `true` */
127
+ animations?: boolean
128
+ /** Built-in animation preset. Overrides animationDuration/animationEasing when set. Default: `null` */
129
+ animationPreset?: "default" | "material" | "spring" | "snappy" | "smooth" | "bounce" | "elastic" | "fade" | "pop" | "slide" | "none" | null
130
+ /** Animation duration in ms. Default: `150` */
131
+ animationDuration?: number
132
+ /** CSS easing function for animations. Default: `"ease-out"` */
133
+ animationEasing?: string
134
+ /** Custom content for an empty option list (also for searches with no matches unless `noResultsState` is set). Default: `null` */
135
+ emptyState?: string | ((el: HTMLDivElement) => void) | null
136
+ /** Custom content when no results match. Default: `null` */
137
+ noResultsState?: string | ((el: HTMLDivElement) => void) | null
138
+ /** Custom content during loading. Default: `null` */
139
+ loadingState?: string | ((el: HTMLDivElement) => void) | null
140
+ /** Load option images only when visible via IntersectionObserver. Default: `false` */
141
+ lazyLoadImages?: boolean
142
+ /** Max items selectable in multi-select (0 = unlimited). Default: `0` */
143
+ maximumSelectionLength?: number
144
+ /** Show count-aware Select all / Clear actions for multi-selects. Default: `false` */
145
+ bulkActions?: boolean
146
+ /** Show a live selected count in the multi-select dropdown. Default: `false` */
147
+ showSelectionCount?: boolean
148
+ /** Hide selected results from the option list. Default: `false` */
149
+ hideSelected?: boolean
150
+ /** Bulk selection label or formatter receiving the eligible match count. */
151
+ selectAllText?: string | ((matchCount: number) => string)
152
+ /** Bulk deselection label or formatter receiving the selected match count. */
153
+ deselectAllText?: string | ((selectedMatchCount: number) => string)
154
+ /** Selected count label, receiving the count and limit (0 = unlimited). */
155
+ selectionCountText?: string | ((count: number, limit: number) => string)
156
+ /** Max characters in search input (0 = unlimited). Default: `0` */
157
+ maximumInputLength?: number
158
+ /** Min options before showing search box. Default: `0` */
159
+ minimumResultsForSearch?: number
160
+ /** Auto-select active option when dropdown closes. Default: `false` */
161
+ selectOnClose?: boolean
162
+ /** Characters that trigger tag creation (e.g. `[",", " "]`). Default: `[]` */
163
+ tokenSeparators?: string[]
164
+ /** Custom sort function for filtered results. Default: `null` */
165
+ sorter?: ((items: TamSelectItem[]) => TamSelectItem[]) | null
166
+ /** Render a result. Return a Node for trusted markup or text for escaped output. */
167
+ templateResult?: ((item: TamSelectItem, context: TamSelectTemplateContext) => Node | string | null | false) | null
168
+ /** Render a selected value. Return a Node for trusted markup or text for escaped output. */
169
+ templateSelection?: ((item: TamSelectItem, context: TamSelectTemplateContext) => Node | string | null | false) | null
170
+ /** Custom remote transport. It may return JSON or a Response-like object. */
171
+ transport?: ((options: TamSelectTransportOptions) => unknown | Promise<unknown>) | null
172
+ /** Transform remote JSON before configured paths are read. */
173
+ processResults?: ((data: unknown, context: { query: string; page: number; tamSelect: TamSelect }) => unknown | Promise<unknown>) | null
174
+ /** Extra remote query parameters or a function that returns them. */
175
+ remoteParams?: Record<string, unknown> | ((context: { query: string; page: number; tamSelect: TamSelect }) => Record<string, unknown>)
176
+ /** Cache remote responses by final URL. Default: `false` */
177
+ cacheRemote?: boolean
178
+ /** Element or selector used to portal the dropdown beyond clipping containers. */
179
+ dropdownParent?: Element | string | null
180
+ /** Open-menu width. `"auto"` is at least the control's width and grows to fit the widest option, like a native select; `"trigger"` always matches the control. `"content"` aliases `"auto"`. Default: `"auto"` */
181
+ dropdownWidth?: "trigger" | "auto" | "content"
182
+ /** Widest an auto-width menu grows, in CSS pixels, before long options wrap. Never narrower than the control. Default: `480` */
183
+ maxDropdownWidth?: number
184
+ /** Maximum dropdown height in CSS pixels before results scroll. Default: `360` */
185
+ maxDropdownHeight?: number
186
+ /** Minimum gap between a portaled dropdown and the visible viewport edge. Default: `8` */
187
+ viewportPadding?: number
188
+ /** Control width. `"resolve"` fills the container, or follows an inline width on the native select; `"auto"` (alias `"fit"`) sizes it to the widest option like a native select; any other value is a CSS width. Default: `"resolve"` */
189
+ width?: "resolve" | "auto" | "fit" | (string & {})
190
+ /** Text direction: `"ltr"`, `"rtl"`, or `"auto"`. Default: `"auto"` */
191
+ dir?: "ltr" | "rtl" | "auto"
192
+ /** Enable drag-and-drop reordering of selected tags. Default: `false` */
193
+ draggable?: boolean
194
+ /** Enable virtual scrolling. Default: `false` */
195
+ virtualScroll?: boolean
196
+ /** Auto-enable virtual scroll when items exceed this count. Default: `100` */
197
+ virtualScrollThreshold?: number
198
+ /** Extra items rendered above/below viewport. Default: `5` */
199
+ virtualScrollBuffer?: number
200
+ /** Estimated height per item in pixels for virtual scroll. Default: `32` */
201
+ virtualItemHeight?: number
202
+ /** Wrap long result labels and details across multiple lines. By default, wraps ordinary lists and truncates virtualized lists. `true` disables fixed-height virtualization. */
203
+ wrapOptionText?: boolean
204
+ /** Add native title tooltips to option text and compact selected values. Default: `true` */
205
+ optionTooltip?: boolean
206
+ }
207
+
208
+ export interface TamSelectEventDetail {
209
+ tamSelect: TamSelect
210
+ [key: string]: unknown
211
+ }
212
+
213
+ export interface TamSelectChangeEventDetail extends TamSelectEventDetail {
214
+ value: string | string[]
215
+ items: TamSelectItem[]
216
+ }
217
+
218
+ export interface TamSelectLoadEventDetail extends TamSelectEventDetail {
219
+ items: TamSelectItem[]
220
+ pagination: TamSelectPagination
221
+ }
222
+
223
+ export interface TamSelectErrorEventDetail extends TamSelectEventDetail {
224
+ error: Error
225
+ }
226
+
227
+ export interface TamSelectCreateEventDetail extends TamSelectEventDetail {
228
+ item: TamSelectItem
229
+ }
230
+
231
+ export interface TamSelectDataEventDetail extends TamSelectEventDetail {
232
+ items: (TamSelectItem | undefined)[]
233
+ }
234
+
235
+ export interface TamSelectReorderEventDetail extends TamSelectEventDetail {
236
+ value: string | string[]
237
+ items: TamSelectItem[]
238
+ }
239
+
240
+ export interface TamSelectBulkChangeEventDetail extends TamSelectEventDetail {
241
+ action: "select" | "deselect"
242
+ items: TamSelectItem[]
243
+ value: string[]
244
+ }
245
+
246
+ declare class TamSelect {
247
+ static instances: WeakMap<HTMLSelectElement, TamSelect>
248
+ static getInstance(element: HTMLSelectElement): TamSelect | null
249
+ /** Remove markup left on a select by an instance that no longer exists (e.g. a restored page snapshot). */
250
+ static recoverStaleMarkup(select: HTMLSelectElement): void
251
+
252
+ constructor(select: HTMLSelectElement, options?: TamSelectOptions)
253
+
254
+ /** The underlying `<select>` element */
255
+ readonly select: HTMLSelectElement
256
+ /** The generated wrapper `<div>` */
257
+ readonly wrapper: HTMLDivElement
258
+ /** The control `<div>` containing the selection display */
259
+ readonly control: HTMLDivElement
260
+ /** The dropdown panel containing search and results */
261
+ readonly dropdown: HTMLDivElement
262
+ /** The scrollable listbox containing rendered options */
263
+ readonly results: HTMLDivElement
264
+ /** The search `<input>` (when searchable) */
265
+ readonly input: HTMLInputElement | undefined
266
+ /** The trigger `<button>` (when not searchable) */
267
+ readonly trigger: HTMLButtonElement | undefined
268
+ /** The clear `<button>` */
269
+ readonly clearButton: HTMLButtonElement
270
+ /** Current resolved text direction */
271
+ readonly dir: "ltr" | "rtl"
272
+ /** Current resolved theme */
273
+ readonly theme: "default" | "select2"
274
+ /** Whether the dropdown is open */
275
+ readonly opened: boolean
276
+ /** Whether remote data is loading */
277
+ readonly loading: boolean
278
+ /** Whether the selection limit has been reached */
279
+ readonly selectionLimitReached: boolean
280
+ /** Current search query */
281
+ readonly query: string
282
+ /** All parsed items */
283
+ readonly items: TamSelectItem[]
284
+ /** Currently visible items in the dropdown */
285
+ readonly visibleItems: TamSelectVisibleEntry[]
286
+ /** Currently active (highlighted) option index */
287
+ readonly activeIndex: number
288
+ /** All parsed option groups */
289
+ readonly groupTree: Array<{
290
+ label?: string
291
+ type?: string
292
+ items: TamSelectItem[]
293
+ entries: TamSelectVisibleEntry[]
294
+ }>
295
+ /** Current merged class map */
296
+ readonly classes: Record<string, string>
297
+ /** Current TamSelect options */
298
+ readonly options: TamSelectOptions
299
+
300
+ /** The current selection value (string for single, array for multi) */
301
+ get value(): string | string[]
302
+
303
+ /** Open the dropdown */
304
+ open(loadRemote?: boolean): void
305
+ /** Close the dropdown */
306
+ close(): void
307
+ /** Focus the search input or trigger */
308
+ focus(): void
309
+ /** Remove focus */
310
+ blur(): void
311
+ /** Set the selected value(s) */
312
+ setValue(value: string | string[] | null): void
313
+ /** Clear all selections (a single select selects its empty option; without one this does nothing) */
314
+ clear(): void
315
+ /** Select loaded matches, respecting disabled options, cancellation, and limits. */
316
+ selectAll(): void
317
+ /** Deselect loaded matches, including those hidden by hideSelected. */
318
+ deselectAll(): void
319
+ /** Re-read native options and re-render */
320
+ refresh(): void
321
+ /** Add one or more options programmatically */
322
+ addData(raw: Record<string, unknown> | Record<string, unknown>[]): void
323
+ /** Remove options by value */
324
+ removeData(values: string | string[]): void
325
+ /** Clear cached remote responses */
326
+ clearRemoteCache(): void
327
+ /** Select an item, or the first item with a value. Re-selecting the current value emits no change. */
328
+ selectValue(value: string | TamSelectItem): void
329
+ /** Deselect an item, or the first item with a value */
330
+ deselect(value: string | TamSelectItem): void
331
+ /** Create a new option from a label */
332
+ /** Returns `null` while the select is disabled. */
333
+ createItem(label: string, value?: string): TamSelectItem | null
334
+ /** Remove all generated markup and restore the native select */
335
+ destroy(): void
336
+
337
+ /** Read the selected items */
338
+ selectedItems(): TamSelectItem[]
339
+ /** Refresh the visible items list */
340
+ updateVisibleItems(items: TamSelectItem[], preserveActive?: boolean): void
341
+ /** Toggle item selection */
342
+ toggleItem(item: TamSelectItem): void
343
+ }
344
+
345
+ export declare const ANIMATION_PRESETS: Record<string, { duration: number; easing: string }>
346
+
347
+ export default TamSelect