ankiutils 0.0.9 → 0.0.11

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.
@@ -24,7 +24,7 @@
24
24
  disabled = false,
25
25
  clearable = false,
26
26
  multiple = false,
27
- onSelected = () => {}
27
+ onSelected: onSelectedBase = () => {}
28
28
  }: Props = $props();
29
29
 
30
30
  let searchTerm = $state('');
@@ -69,7 +69,7 @@
69
69
  }
70
70
  searchTerm = '';
71
71
  inputElement?.focus();
72
- onSelected(selectedOptions);
72
+ onSelectedBase(selectedOptions);
73
73
  }
74
74
 
75
75
  async function scrollHighlightedIntoView() {
@@ -124,6 +124,13 @@
124
124
  }
125
125
  }
126
126
 
127
+ function onSelected() {
128
+ if (!multiple) {
129
+ selectOptionsComponent?.closeDropdown();
130
+ }
131
+ onSelectedBase(selectedOptions);
132
+ }
133
+
127
134
  $effect(() => {
128
135
  if (isOpen) {
129
136
  document.addEventListener('click', handleClickOutside);
@@ -184,6 +191,7 @@
184
191
  options={filteredOptions}
185
192
  bind:selectedOptions
186
193
  {multiple}
194
+ {highlightedIndex}
187
195
  {onSelected}
188
196
  {onOpenDropdown}
189
197
  {onCloseDropdown}
@@ -3,7 +3,7 @@
3
3
  import { type SelectOption } from './types.js';
4
4
 
5
5
  interface Props {
6
- label: string;
6
+ label?: string;
7
7
  options: SelectOption[];
8
8
  selectedOptions: string[];
9
9
  multiple?: boolean;
@@ -21,7 +21,14 @@
21
21
  let isOpen = $state(false);
22
22
  let containerElement: HTMLDivElement;
23
23
  let selectOptionsComponent: SelectOptions | undefined = $state();
24
-
24
+ let displayLabel = $derived.by(() => {
25
+ if (label) {
26
+ return label;
27
+ } else if (selectedOptions.length) {
28
+ return selectedOptions.join(', ');
29
+ }
30
+ return 'Select';
31
+ });
25
32
  function handleClickOutside(event: MouseEvent) {
26
33
  if (containerElement && !containerElement.contains(event.target as Node)) {
27
34
  selectOptionsComponent?.closeDropdown();
@@ -38,7 +45,7 @@
38
45
 
39
46
  <div class="dropdown" bind:this={containerElement}>
40
47
  <button class="btn" onclick={() => (isOpen = !isOpen)}>
41
- <span>{label}</span>
48
+ <span>{displayLabel}</span>
42
49
  <i class="bi bi-chevron-{isOpen ? 'up' : 'down'}"></i>
43
50
  </button>
44
51
  {#if isOpen}
@@ -1,6 +1,6 @@
1
1
  import { type SelectOption } from './types.ts';
2
2
  interface Props {
3
- label: string;
3
+ label?: string;
4
4
  options: SelectOption[];
5
5
  selectedOptions: string[];
6
6
  multiple?: boolean;
@@ -6,6 +6,7 @@
6
6
  selectedOptions: string[];
7
7
  multiple?: boolean;
8
8
  isOpen?: boolean;
9
+ highlightedIndex?: number,
9
10
  onSelected?: (value: string[]) => void;
10
11
  onOpenDropdown?: () => void;
11
12
  onCloseDropdown?: () => void;
@@ -16,6 +17,7 @@
16
17
  selectedOptions = $bindable<string[]>([]),
17
18
  multiple = false,
18
19
  isOpen = $bindable(false),
20
+ highlightedIndex,
19
21
  onSelected,
20
22
  onOpenDropdown,
21
23
  onCloseDropdown
@@ -50,34 +52,40 @@
50
52
  }
51
53
  </script>
52
54
 
53
- <div class="menu dropdown-content flex-nowrap w-full max-h-46 overflow-auto bg-base-100 rounded-box z-1 p-2 shadow-sm gap-1">
55
+ <ul
56
+ class="menu dropdown-content flex-nowrap w-full overflow-auto bg-base-100 rounded-box z-1 p-2 shadow-sm gap-1"
57
+ >
54
58
  {#if options.length === 0}
55
59
  <div class="text-gray-400">No options found</div>
56
60
  {:else}
57
61
  {#each options as option, index (option.value)}
58
62
  {@const checked = selectedOptions.includes(option.value)}
59
-
60
- {#if multiple}
61
- <input
62
- bind:this={optionElements[index]}
63
- type="checkbox"
64
- class="btn"
65
- class:btn-primary={checked}
66
- {checked}
67
- aria-label={option.label}
68
- onclick={() => selectOption(option)}
69
- />
70
- {:else}
71
- <button
72
- bind:this={optionElements[index]}
73
- type="button"
74
- class="btn"
75
- class:btn-primary={checked}
76
- onclick={() => selectOption(option)}
77
- >
78
- {option.label}
79
- </button>
80
- {/if}
63
+ {@const highlighted = !checked && highlightedIndex === index}
64
+ <li>
65
+ {#if multiple}
66
+ <input
67
+ bind:this={optionElements[index]}
68
+ type="checkbox"
69
+ class="btn"
70
+ class:btn-primary={checked}
71
+ class:btn-outline={highlighted}
72
+ {checked}
73
+ aria-label={option.label}
74
+ onclick={() => selectOption(option)}
75
+ />
76
+ {:else}
77
+ <button
78
+ bind:this={optionElements[index]}
79
+ type="button"
80
+ class="btn"
81
+ class:btn-primary={checked}
82
+ class:btn-outline={highlighted}
83
+ onclick={() => selectOption(option)}
84
+ >
85
+ {option.label}
86
+ </button>
87
+ {/if}
88
+ </li>
81
89
  {/each}
82
90
  {/if}
83
- </div>
91
+ </ul>
@@ -4,6 +4,7 @@ interface Props {
4
4
  selectedOptions: string[];
5
5
  multiple?: boolean;
6
6
  isOpen?: boolean;
7
+ highlightedIndex?: number;
7
8
  onSelected?: (value: string[]) => void;
8
9
  onOpenDropdown?: () => void;
9
10
  onCloseDropdown?: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ankiutils",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "scripts": {
6
6
  "dev": "vite dev",