@getmicdrop/svelte-components 5.6.0 → 5.6.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.
- package/dist/components/Layout/__tests__/AppShell.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/AppShell.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/AppShell.test.js +95 -0
- package/dist/components/Layout/__tests__/ContentSection.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/ContentSection.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/ContentSection.test.js +112 -0
- package/dist/components/Layout/__tests__/PageContainer.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/PageContainer.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/PageContainer.test.js +133 -0
- package/dist/components/Layout/__tests__/Responsive.test.d.ts +2 -0
- package/dist/components/Layout/__tests__/Responsive.test.d.ts.map +1 -0
- package/dist/components/Layout/__tests__/Responsive.test.js +123 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +41 -40
- package/dist/patterns/navigation/BottomNav.svelte +18 -8
- package/dist/patterns/navigation/BottomNav.svelte.d.ts.map +1 -1
- package/dist/primitives/Accordion/AccordionItemWrapper.test.svelte +107 -107
- package/dist/primitives/Accordion/AccordionItemWrapper.test.svelte.d.ts +2 -2
- package/dist/primitives/Accordion/AccordionItemWrapper.test.svelte.d.ts.map +1 -1
- package/dist/primitives/Checkbox/Checkbox.svelte +3 -3
- package/dist/primitives/Dropdown/DropdownDivider.svelte +9 -0
- package/dist/primitives/Dropdown/DropdownDivider.svelte.d.ts +7 -0
- package/dist/primitives/Dropdown/DropdownDivider.svelte.d.ts.map +1 -0
- package/dist/primitives/Helper/Helper.svelte +33 -0
- package/dist/primitives/Helper/Helper.svelte.d.ts +18 -0
- package/dist/primitives/Helper/Helper.svelte.d.ts.map +1 -0
- package/dist/primitives/Input/Input.svelte +416 -417
- package/dist/primitives/Input/Input.svelte.d.ts +2 -4
- package/dist/primitives/Input/Input.svelte.d.ts.map +1 -1
- package/dist/primitives/Modal/Modal.svelte +157 -158
- package/dist/primitives/Modal/Modal.svelte.d.ts +6 -8
- package/dist/primitives/Modal/Modal.svelte.d.ts.map +1 -1
- package/dist/primitives/NumberInput/NumberInput.svelte +105 -106
- package/dist/primitives/NumberInput/NumberInput.svelte.d.ts +0 -2
- package/dist/primitives/NumberInput/NumberInput.svelte.d.ts.map +1 -1
- package/dist/primitives/Toggle.svelte +70 -71
- package/dist/primitives/Toggle.svelte.d.ts +2 -4
- package/dist/primitives/Toggle.svelte.d.ts.map +1 -1
- package/dist/primitives/Tooltip/Tooltip.svelte +83 -0
- package/dist/primitives/Tooltip/Tooltip.svelte.d.ts +15 -0
- package/dist/primitives/Tooltip/Tooltip.svelte.d.ts.map +1 -0
- package/dist/primitives/index.d.ts +3 -0
- package/dist/primitives/index.js +7 -0
- package/dist/recipes/ImageUploader/ImageUploader.spec.js +6 -5
- package/dist/recipes/inputs/MultiSelect.svelte +277 -256
- package/dist/recipes/inputs/MultiSelect.svelte.d.ts +54 -21
- package/dist/recipes/inputs/MultiSelect.svelte.d.ts.map +1 -1
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.spec.js +9 -4
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte +332 -327
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte.d.ts +12 -1
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte.d.ts.map +1 -1
- package/dist/stores/auth.js +8 -0
- package/dist/stores/auth.svelte.d.ts +39 -0
- package/dist/stores/auth.svelte.d.ts.map +1 -0
- package/dist/stores/auth.svelte.js +60 -0
- package/dist/stores/formDataStore.d.ts.map +1 -1
- package/dist/stores/formDataStore.js +8 -0
- package/dist/stores/formDataStore.svelte.d.ts +47 -0
- package/dist/stores/formDataStore.svelte.d.ts.map +1 -0
- package/dist/stores/formDataStore.svelte.js +84 -0
- package/dist/stores/formSave.d.ts.map +1 -1
- package/dist/stores/formSave.js +8 -0
- package/dist/stores/formSave.svelte.d.ts +33 -0
- package/dist/stores/formSave.svelte.d.ts.map +1 -0
- package/dist/stores/formSave.svelte.js +113 -0
- package/dist/stores/navigation.d.ts.map +1 -1
- package/dist/stores/navigation.js +8 -0
- package/dist/stores/navigation.svelte.d.ts +35 -0
- package/dist/stores/navigation.svelte.d.ts.map +1 -0
- package/dist/stores/navigation.svelte.js +69 -0
- package/dist/telemetry.server.spec.js +11 -8
- package/dist/telemetry.spec.js +75 -50
- package/dist/utils/imageValidation.spec.js +62 -59
- package/dist/utils/logger.d.ts +19 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +47 -0
- package/package.json +296 -292
|
@@ -1,337 +1,342 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { SearchOutline } from "../../../primitives/Icons";
|
|
3
|
-
import * as GMaps from '@googlemaps/js-api-loader';
|
|
4
|
-
import { PUBLIC_GOOGLE_MAPS_API_KEY } from '../../../config.js';
|
|
5
|
-
import { typography } from '../../../tokens/typography';
|
|
6
|
-
const { Loader } = GMaps;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let
|
|
40
|
-
let
|
|
41
|
-
let
|
|
42
|
-
let
|
|
43
|
-
let
|
|
44
|
-
let
|
|
45
|
-
let
|
|
46
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { SearchOutline } from "../../../primitives/Icons";
|
|
3
|
+
import * as GMaps from '@googlemaps/js-api-loader';
|
|
4
|
+
import { PUBLIC_GOOGLE_MAPS_API_KEY } from '../../../config.js';
|
|
5
|
+
import { typography } from '../../../tokens/typography';
|
|
6
|
+
const { Loader } = GMaps;
|
|
7
|
+
/** Google Places API address component */ interface AddressComponent { longText: string; shortText: string; types: string[]; } /** Google Places API response data */ interface PlaceData { formattedAddress?: string; addressComponents?: AddressComponent[]; text?: string; [key: string]: unknown; } /** Google Places autocomplete suggestion */ interface AutocompleteSuggestion { placePrediction: { types: string[]; text: { toString(): string }; toPlace(): PlaceObject; }; } /** Google Places place object */ interface PlaceObject { fetchFields(options: { fields: string[] }): Promise<void>; toJSON(): PlaceData; } /** Google Places API interface */ interface PlacesApiInterface { AutocompleteSessionToken: new () => AutocompleteSessionToken; AutocompleteSuggestion: { fetchAutocompleteSuggestions(request: AutocompleteRequest): Promise<{ suggestions: AutocompleteSuggestion[] }>; }; } /** Google Places autocomplete session token */ interface AutocompleteSessionToken {} /** Autocomplete request parameters */ interface AutocompleteRequest { input: string; language: string; region: string; sessionToken: AutocompleteSessionToken | string; } /** Search result item */ interface SearchResult { to_place: PlaceObject; text: string; originalText?: string; }
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
fetchFields?: string[];
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
language?: string;
|
|
13
|
+
region?: string;
|
|
14
|
+
autocomplete?: string;
|
|
15
|
+
initialValue?: string;
|
|
16
|
+
onResponse?: (data: PlaceData) => void;
|
|
17
|
+
onError?: (error: string) => void;
|
|
18
|
+
mode?: 'full' | 'cityState';
|
|
19
|
+
animateFocus?: boolean;
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let {
|
|
24
|
+
fetchFields = ['formattedAddress', 'addressComponents'],
|
|
25
|
+
placeholder = $bindable('Search for location...'),
|
|
26
|
+
language = 'en-US',
|
|
27
|
+
region = 'US',
|
|
28
|
+
autocomplete = 'off',
|
|
29
|
+
initialValue = '',
|
|
30
|
+
onResponse = () => {},
|
|
31
|
+
onError = () => {},
|
|
32
|
+
mode = 'full',
|
|
33
|
+
animateFocus = true,
|
|
34
|
+
disabled = false,
|
|
35
|
+
}: Props = $props();
|
|
36
|
+
|
|
37
|
+
let shouldAnimate = $derived(animateFocus && !disabled);
|
|
38
|
+
|
|
39
|
+
let inputRef: HTMLInputElement;
|
|
40
|
+
let containerRef: HTMLDivElement;
|
|
41
|
+
let currentSuggestion = $state(-1);
|
|
42
|
+
let title = $state('');
|
|
43
|
+
let results = $state<SearchResult[]>([]);
|
|
44
|
+
let token: AutocompleteSessionToken | undefined;
|
|
45
|
+
let loader: GMaps.Loader;
|
|
46
|
+
let placesApi: Partial<PlacesApiInterface> = {};
|
|
47
|
+
|
|
47
48
|
let request = $state<{
|
|
48
49
|
input: string;
|
|
49
50
|
language: string;
|
|
50
51
|
region: string;
|
|
51
|
-
sessionToken:
|
|
52
|
+
sessionToken: AutocompleteSessionToken | string;
|
|
52
53
|
}>({
|
|
53
54
|
input: (() => initialValue)(),
|
|
54
55
|
language: (() => language)(),
|
|
55
56
|
region: (() => region)(),
|
|
56
57
|
sessionToken: '',
|
|
57
58
|
});
|
|
58
|
-
|
|
59
|
-
let isInitialized = $state(false);
|
|
60
|
-
|
|
61
|
-
$effect(() => {
|
|
62
|
-
if (initialValue && !isInitialized) {
|
|
63
|
-
request.input = initialValue;
|
|
64
|
-
title = initialValue;
|
|
65
|
-
isInitialized = true;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
$effect(() => {
|
|
70
|
-
if (mode === 'cityState' && placeholder === 'Search for location...') {
|
|
71
|
-
placeholder = 'Search for city, state...';
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const reset = () => {
|
|
76
|
-
currentSuggestion = -1;
|
|
77
|
-
results = [];
|
|
78
|
-
refreshToken(request).catch(() => {});
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const makeAcRequest = async (
|
|
82
|
-
event: Event & { currentTarget: HTMLInputElement }
|
|
83
|
-
): Promise<void> => {
|
|
84
|
-
const target = event.currentTarget as HTMLInputElement;
|
|
85
|
-
if (target?.value === '') {
|
|
86
|
-
title = '';
|
|
87
|
-
request.input = '';
|
|
88
|
-
results = [];
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
title = target.value;
|
|
93
|
-
request.input = target.value;
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
const { suggestions } =
|
|
97
|
-
await placesApi.AutocompleteSuggestion
|
|
98
|
-
request
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
if (mode === 'cityState') {
|
|
102
|
-
results = suggestions
|
|
103
|
-
.filter((s:
|
|
104
|
-
const types = s.placePrediction.types || [];
|
|
105
|
-
|
|
106
|
-
// Accept cities (locality) and city-level neighborhoods (sublocality)
|
|
107
|
-
// like Van Nuys, North Hollywood - but not states or counties
|
|
108
|
-
return types.some(
|
|
109
|
-
(type: string) =>
|
|
110
|
-
type === 'locality' ||
|
|
111
|
-
type === 'sublocality' ||
|
|
112
|
-
type === 'sublocality_level_1'
|
|
113
|
-
);
|
|
114
|
-
})
|
|
115
|
-
.map((s:
|
|
116
|
-
let displayText = s.placePrediction.text.toString();
|
|
117
|
-
|
|
118
|
-
// Format as "City, State" (first two parts)
|
|
119
|
-
const parts = displayText.split(', ');
|
|
120
|
-
if (parts.length >= 2) {
|
|
121
|
-
displayText = parts.slice(0, 2).join(', ');
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
to_place: s.placePrediction.toPlace(),
|
|
126
|
-
text: displayText,
|
|
127
|
-
originalText: s.placePrediction.text.toString(),
|
|
128
|
-
};
|
|
129
|
-
});
|
|
130
|
-
} else {
|
|
131
|
-
results = suggestions.map((s:
|
|
132
|
-
to_place: s.placePrediction.toPlace(),
|
|
133
|
-
text: s.placePrediction.text.toString(),
|
|
134
|
-
}));
|
|
135
|
-
}
|
|
136
|
-
} catch (e:
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
//
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
<
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
class="
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
59
|
+
|
|
60
|
+
let isInitialized = $state(false);
|
|
61
|
+
|
|
62
|
+
$effect(() => {
|
|
63
|
+
if (initialValue && !isInitialized) {
|
|
64
|
+
request.input = initialValue;
|
|
65
|
+
title = initialValue;
|
|
66
|
+
isInitialized = true;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
$effect(() => {
|
|
71
|
+
if (mode === 'cityState' && placeholder === 'Search for location...') {
|
|
72
|
+
placeholder = 'Search for city, state...';
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const reset = () => {
|
|
77
|
+
currentSuggestion = -1;
|
|
78
|
+
results = [];
|
|
79
|
+
refreshToken(request).catch(() => {});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const makeAcRequest = async (
|
|
83
|
+
event: Event & { currentTarget: HTMLInputElement }
|
|
84
|
+
): Promise<void> => {
|
|
85
|
+
const target = event.currentTarget as HTMLInputElement;
|
|
86
|
+
if (target?.value === '') {
|
|
87
|
+
title = '';
|
|
88
|
+
request.input = '';
|
|
89
|
+
results = [];
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
title = target.value;
|
|
94
|
+
request.input = target.value;
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const { suggestions } =
|
|
98
|
+
await placesApi.AutocompleteSuggestion!.fetchAutocompleteSuggestions(
|
|
99
|
+
request
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
if (mode === 'cityState') {
|
|
103
|
+
results = suggestions
|
|
104
|
+
.filter((s: AutocompleteSuggestion) => {
|
|
105
|
+
const types = s.placePrediction.types || [];
|
|
106
|
+
|
|
107
|
+
// Accept cities (locality) and city-level neighborhoods (sublocality)
|
|
108
|
+
// like Van Nuys, North Hollywood - but not states or counties
|
|
109
|
+
return types.some(
|
|
110
|
+
(type: string) =>
|
|
111
|
+
type === 'locality' ||
|
|
112
|
+
type === 'sublocality' ||
|
|
113
|
+
type === 'sublocality_level_1'
|
|
114
|
+
);
|
|
115
|
+
})
|
|
116
|
+
.map((s: AutocompleteSuggestion) => {
|
|
117
|
+
let displayText = s.placePrediction.text.toString();
|
|
118
|
+
|
|
119
|
+
// Format as "City, State" (first two parts)
|
|
120
|
+
const parts = displayText.split(', ');
|
|
121
|
+
if (parts.length >= 2) {
|
|
122
|
+
displayText = parts.slice(0, 2).join(', ');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
to_place: s.placePrediction.toPlace(),
|
|
127
|
+
text: displayText,
|
|
128
|
+
originalText: s.placePrediction.text.toString(),
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
results = suggestions.map((s: AutocompleteSuggestion) => ({
|
|
133
|
+
to_place: s.placePrediction.toPlace(),
|
|
134
|
+
text: s.placePrediction.text.toString(),
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
} catch (e: unknown) {
|
|
138
|
+
const error = e as { name?: string; message?: string };
|
|
139
|
+
onError(
|
|
140
|
+
(error.name || 'An error occurred') +
|
|
141
|
+
' - ' +
|
|
142
|
+
(error.message || 'see console for details.')
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const onPlaceSelected = async (
|
|
148
|
+
place: PlaceObject,
|
|
149
|
+
originalText?: string
|
|
150
|
+
): Promise<void> => {
|
|
151
|
+
results = [];
|
|
152
|
+
currentSuggestion = -1;
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
await place.fetchFields({
|
|
156
|
+
fields: fetchFields,
|
|
157
|
+
});
|
|
158
|
+
let placeData = place.toJSON();
|
|
159
|
+
request.input = originalText || placeData.text || '';
|
|
160
|
+
title = originalText || placeData.text || '';
|
|
161
|
+
onResponse(placeData);
|
|
162
|
+
} catch (e: unknown) {
|
|
163
|
+
const error = e as { name?: string; message?: string };
|
|
164
|
+
onError(
|
|
165
|
+
(error.name || 'An error occurred') +
|
|
166
|
+
' - ' +
|
|
167
|
+
(error.message || 'error fetching place details')
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
refreshToken(request).catch(() => {});
|
|
173
|
+
}, 100);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const refreshToken = async (req: AutocompleteRequest) => {
|
|
177
|
+
try {
|
|
178
|
+
token = new placesApi.AutocompleteSessionToken!();
|
|
179
|
+
req.sessionToken = token;
|
|
180
|
+
return req;
|
|
181
|
+
} catch (e: unknown) {
|
|
182
|
+
const error = e as { name?: string; message?: string };
|
|
183
|
+
onError(
|
|
184
|
+
(error.name || 'An error occurred') +
|
|
185
|
+
' - ' +
|
|
186
|
+
(error.message || 'error fetch token')
|
|
187
|
+
);
|
|
188
|
+
return req;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
function onKeyDown(e: KeyboardEvent) {
|
|
193
|
+
if (results.length === 0) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (e.key === 'ArrowDown') {
|
|
198
|
+
e.preventDefault();
|
|
199
|
+
if (currentSuggestion === -1) {
|
|
200
|
+
currentSuggestion = 0;
|
|
201
|
+
} else {
|
|
202
|
+
currentSuggestion =
|
|
203
|
+
currentSuggestion < results.length - 1 ? currentSuggestion + 1 : 0;
|
|
204
|
+
}
|
|
205
|
+
} else if (e.key === 'ArrowUp') {
|
|
206
|
+
e.preventDefault();
|
|
207
|
+
if (currentSuggestion === -1) {
|
|
208
|
+
currentSuggestion = results.length - 1;
|
|
209
|
+
} else {
|
|
210
|
+
currentSuggestion =
|
|
211
|
+
currentSuggestion > 0 ? currentSuggestion - 1 : results.length - 1;
|
|
212
|
+
}
|
|
213
|
+
} else if (e.key === 'Enter') {
|
|
214
|
+
e.preventDefault();
|
|
215
|
+
if (currentSuggestion >= 0 && currentSuggestion < results.length) {
|
|
216
|
+
onPlaceSelected(
|
|
217
|
+
results[currentSuggestion].to_place,
|
|
218
|
+
results[currentSuggestion].text
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
} else if (e.key === 'Escape') {
|
|
222
|
+
e.preventDefault();
|
|
223
|
+
reset();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function handleClickOutside(event: MouseEvent) {
|
|
228
|
+
if (containerRef && !containerRef.contains(event.target as Node)) {
|
|
229
|
+
reset();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
$effect(() => {
|
|
234
|
+
if (initialValue) {
|
|
235
|
+
request.input = initialValue;
|
|
236
|
+
title = initialValue;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Add click outside listener
|
|
240
|
+
document.addEventListener('click', handleClickOutside);
|
|
241
|
+
|
|
242
|
+
// Initialize Google Maps API
|
|
243
|
+
const initGoogleMaps = async () => {
|
|
244
|
+
if (!PUBLIC_GOOGLE_MAPS_API_KEY) {
|
|
245
|
+
onError('Google Maps API key is required');
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
loader = new Loader({
|
|
251
|
+
apiKey: PUBLIC_GOOGLE_MAPS_API_KEY,
|
|
252
|
+
version: 'weekly',
|
|
253
|
+
libraries: ['places'],
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const { AutocompleteSessionToken, AutocompleteSuggestion } =
|
|
257
|
+
await loader.importLibrary('places');
|
|
258
|
+
placesApi.AutocompleteSessionToken = AutocompleteSessionToken;
|
|
259
|
+
placesApi.AutocompleteSuggestion = AutocompleteSuggestion;
|
|
260
|
+
token = new placesApi.AutocompleteSessionToken!();
|
|
261
|
+
request.sessionToken = token;
|
|
262
|
+
} catch (e: unknown) {
|
|
263
|
+
const error = e as { name?: string; message?: string };
|
|
264
|
+
onError(
|
|
265
|
+
(error.name || 'An error occurred') +
|
|
266
|
+
' - ' +
|
|
267
|
+
(error.message || 'Error loading Google Maps API')
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
initGoogleMaps();
|
|
273
|
+
|
|
274
|
+
return () => {
|
|
275
|
+
document.removeEventListener('click', handleClickOutside);
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
</script>
|
|
279
|
+
|
|
280
|
+
<div class="w-full">
|
|
281
|
+
<div
|
|
282
|
+
bind:this={containerRef}
|
|
283
|
+
class="relative rounded-lg {shouldAnimate ? 'transition-transform duration-200 focus-within:scale-[1.01]' : ''}"
|
|
284
|
+
>
|
|
285
|
+
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 z-10">
|
|
286
|
+
<SearchOutline class={`w-5 h-5 ${typography.iconMuted}`} />
|
|
287
|
+
</div>
|
|
288
|
+
|
|
289
|
+
<input
|
|
290
|
+
bind:this={inputRef}
|
|
291
|
+
type="text"
|
|
292
|
+
name="location"
|
|
293
|
+
class="block w-full h-10 pl-10 pr-2.5 py-2.5 bg-gray-50 border border-gray-300 rounded-lg {typography.label}
|
|
294
|
+
focus:ring-blue-500 focus:border-blue-500 focus:outline-none
|
|
295
|
+
hover:border-blue-500
|
|
296
|
+
disabled:opacity-50 disabled:cursor-not-allowed
|
|
297
|
+
placeholder:text-gray-500
|
|
298
|
+
dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400
|
|
299
|
+
dark:focus:ring-blue-500 dark:focus:border-blue-500
|
|
300
|
+
dark:hover:border-blue-500"
|
|
301
|
+
{placeholder}
|
|
302
|
+
autocomplete={autocomplete as 'off' | 'on'}
|
|
303
|
+
{disabled}
|
|
304
|
+
aria-controls="options"
|
|
305
|
+
bind:value={title}
|
|
306
|
+
oninput={makeAcRequest}
|
|
307
|
+
onkeydown={onKeyDown}
|
|
308
|
+
/>
|
|
309
|
+
|
|
310
|
+
{#if results.length > 0}
|
|
311
|
+
<div class="absolute inset-y-0 right-0 hidden md:flex items-center p-1.5 gap-1">
|
|
312
|
+
<kbd class="inline-flex items-center justify-center px-1 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">Esc</kbd>
|
|
313
|
+
<kbd class="inline-flex items-center justify-center w-6 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">⇑</kbd>
|
|
314
|
+
<kbd class="inline-flex items-center justify-center w-6 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">⇓</kbd>
|
|
315
|
+
</div>
|
|
316
|
+
|
|
317
|
+
<ul
|
|
318
|
+
class="absolute top-full left-0 right-0 z-50 mt-1 py-1 bg-white border border-gray-200 rounded-lg shadow-lg max-h-60 overflow-y-auto
|
|
319
|
+
dark:bg-gray-800 dark:border-gray-600"
|
|
320
|
+
id="options"
|
|
321
|
+
>
|
|
322
|
+
{#each results as place, i}
|
|
323
|
+
<li
|
|
324
|
+
class="cursor-pointer transition-colors duration-100 {typography.body}
|
|
325
|
+
{i === currentSuggestion ? 'bg-gray-100 dark:bg-gray-700' : 'hover:bg-gray-100 dark:hover:bg-gray-700'}"
|
|
326
|
+
id="option-{i + 1}"
|
|
327
|
+
>
|
|
328
|
+
<button
|
|
329
|
+
type="button"
|
|
330
|
+
class="block w-full text-left px-4 py-3 bg-transparent border-none cursor-pointer {typography.sm}
|
|
331
|
+
focus:outline-none focus:bg-gray-100 dark:focus:bg-gray-700"
|
|
332
|
+
tabindex={i + 1}
|
|
333
|
+
onclick={() => onPlaceSelected(place.to_place, place.text)}
|
|
334
|
+
>
|
|
335
|
+
<span class="font-medium">{place.text}</span>
|
|
336
|
+
</button>
|
|
337
|
+
</li>
|
|
338
|
+
{/each}
|
|
339
|
+
</ul>
|
|
340
|
+
{/if}
|
|
341
|
+
</div>
|
|
342
|
+
</div>
|