@bagelink/vue 1.4.184 → 1.5.3

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.
@@ -129,6 +129,7 @@ export interface SearchResult<T> {
129
129
  hasResults: ComputedRef<boolean>
130
130
  isSearching: ComputedRef<boolean>
131
131
  isLoading: ComputedRef<boolean> // New property to track server-side loading state
132
+ searchTerm?: import('vue').Ref<string> // Optional searchTerm ref for simplified usage
132
133
  }
133
134
 
134
135
  /**
@@ -349,12 +350,59 @@ export function searchItems<T>(params: SearchItemParams<T>): T[] {
349
350
  /**
350
351
  * Vue composable for searching items with reactive results
351
352
  * Supports both client-side filtering and server-side search
352
- * @param params Search parameters
353
+ *
354
+ * Usage 1 - Simplified: Pass items directly and get searchTerm ref back
355
+ * ```ts
356
+ * const { results, searchTerm } = useSearch(itemList)
357
+ * ```
358
+ *
359
+ * Usage 2 - Advanced: Pass full params object with custom search term
360
+ * ```ts
361
+ * const searchTerm = ref('')
362
+ * const { results } = useSearch({ searchTerm, items: itemList, keysToSearch: ['name'] })
363
+ * ```
364
+ *
365
+ * @param paramsOrItems Search parameters object or array of items for simplified usage
366
+ * @param options Optional additional search parameters (only used with simplified usage)
353
367
  * @returns Reactive search results and metadata
354
368
  */
369
+ export function useSearch<T>(
370
+ items: MaybeRefOrGetter<T[]>,
371
+ options?: Omit<SearchItemParams<T>, 'items' | 'searchTerm'>
372
+ ): SearchResult<T> & { searchTerm: import('vue').Ref<string> }
355
373
  export function useSearch<T>(
356
374
  params: SearchItemParams<T>
375
+ ): SearchResult<T>
376
+ export function useSearch<T>(
377
+ paramsOrItems: SearchItemParams<T> | MaybeRefOrGetter<T[]>,
378
+ options?: Omit<SearchItemParams<T>, 'items' | 'searchTerm'>
357
379
  ): SearchResult<T> {
380
+ // Normalize inputs - detect if first argument is items array or params object
381
+ let params: SearchItemParams<T>
382
+ let ownedSearchTerm: import('vue').Ref<string> | undefined
383
+
384
+ // Check if first argument is an array or ref (simplified usage)
385
+ // If it's a params object, it won't have these characteristics
386
+ const isArray = Array.isArray(paramsOrItems)
387
+ const isFunction = typeof paramsOrItems === 'function'
388
+ const isObject = typeof paramsOrItems === 'object' && paramsOrItems !== null
389
+ const isRef = isObject && 'value' in paramsOrItems && !('items' in paramsOrItems)
390
+
391
+ const isSimplifiedUsage = isArray || isFunction || isRef
392
+
393
+ if (isSimplifiedUsage) {
394
+ // Simplified usage: useSearch(items, options)
395
+ ownedSearchTerm = ref('')
396
+ params = {
397
+ items: paramsOrItems as MaybeRefOrGetter<T[]>,
398
+ searchTerm: ownedSearchTerm,
399
+ ...options
400
+ }
401
+ } else {
402
+ // Advanced usage: useSearch(params)
403
+ params = paramsOrItems as SearchItemParams<T>
404
+ }
405
+
358
406
  const { searchTerm, minChars = 2, serverSearch, debounceMs = 300 } = params
359
407
 
360
408
  // For tracking server-side loading state
@@ -425,11 +473,18 @@ export function useSearch<T>(
425
473
  return !!term && typeof term === 'string' && term.length >= minChars
426
474
  })
427
475
 
428
- return {
476
+ const result: SearchResult<T> = {
429
477
  results,
430
478
  resultCount,
431
479
  hasResults,
432
480
  isSearching,
433
481
  isLoading: computed(() => isLoading.value),
434
482
  }
483
+
484
+ // Include searchTerm in result if we created it (simplified usage)
485
+ if (ownedSearchTerm) {
486
+ result.searchTerm = ownedSearchTerm
487
+ }
488
+
489
+ return result
435
490
  }