@beyonk/svelte-mapbox 12.1.2 → 12.1.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.
package/README.md CHANGED
@@ -173,7 +173,7 @@ The Geocoder is an autocompleting place lookup, which returns a lat and lng for
173
173
  </script>
174
174
  ```
175
175
 
176
- The geocoder has five events you can subscribe to: `onloading`, `onresult`, `onresults`, `onclear`, and `onerror` which are [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on)
176
+ The geocoder has six events you can subscribe to: `onloading`, `onresult`, `onresults`, `onclear`, `onerror`, and `onchange` which are [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on)
177
177
 
178
178
  The most important event is `onresult` which is fired when a user selects an autocomplete result.
179
179
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beyonk/svelte-mapbox",
3
- "version": "12.1.2",
3
+ "version": "12.1.3",
4
4
  "devDependencies": {
5
5
  "@beyonk/eslint-config": "^9.1.4",
6
6
  "@eslint/eslintrc": "^3.1.0",
@@ -4,7 +4,11 @@ function load (assets, cb) {
4
4
 
5
5
  if (existing) {
6
6
  if (type === 'script') {
7
- cb()
7
+ if (window.mapboxgl) {
8
+ cb()
9
+ } else {
10
+ existing.addEventListener('load', () => cb(), { once: true })
11
+ }
8
12
  }
9
13
  return
10
14
  }
@@ -1,4 +1,5 @@
1
1
  <script>
2
+ import { untrack } from 'svelte'
2
3
  import geocoderAttachment from './geocoder-attachment.js'
3
4
 
4
5
  let {
@@ -7,7 +8,7 @@
7
8
  version = 'v5.1.0',
8
9
  types = [ 'country', 'region', 'postcode', 'district', 'place', 'locality', 'neighborhood', 'address' ],
9
10
  placeholder = 'Search',
10
- value = null,
11
+ value = $bindable(null),
11
12
  customStylesheetUrl = false,
12
13
  geocoder = $bindable(),
13
14
  onresults,
@@ -16,6 +17,7 @@
16
17
  onerror,
17
18
  onclear,
18
19
  onload,
20
+ onchange,
19
21
  ...rest
20
22
  } = $props()
21
23
 
@@ -27,17 +29,22 @@
27
29
  types: types.join(','),
28
30
  placeholder,
29
31
  customStylesheetUrl,
30
- value
32
+ value: untrack(() => value)
31
33
  }, options))
32
34
 
33
35
  function init (detail) {
34
36
  geocoder = detail.geocoder
35
37
  }
38
+
39
+ function updateValue (detail) {
40
+ value = detail.query
41
+ onchange?.(detail)
42
+ }
36
43
  </script>
37
44
 
38
45
  <div
39
46
  id={fieldId}
40
- {@attach geocoderAttachment(optionsWithDefaults, { onresults, onresult, onloading, onerror, onclear, onload })}
47
+ {@attach geocoderAttachment(optionsWithDefaults, { onresults, onresult, onloading, onerror, onclear, onload, onchange: updateValue })}
41
48
  onready={init}
42
49
  {...rest}
43
50
  ></div>
@@ -1,8 +1,10 @@
1
1
  import { load } from '../asset-loader.js'
2
2
 
3
- export default function geocoderAttachment (options, { onresults, onresult, onloading, onerror, onclear, onready }) {
3
+ export default function geocoderAttachment (options, { onresults, onresult, onloading, onerror, onclear, onready, onchange }) {
4
4
  return (element) => {
5
5
  let geocoderInstance
6
+ let inputElement
7
+ let handleChange
6
8
 
7
9
  const resources = [
8
10
  { type: 'script', value: `//api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/${options.version}/mapbox-gl-geocoder.min.js`, id: 'byk-gc-js' }
@@ -40,9 +42,18 @@ export default function geocoderAttachment (options, { onresults, onresult, onlo
40
42
  geocoderInstance.on('load', (ev) => {
41
43
  onready?.({ ...ev, geocoder: geocoderInstance })
42
44
  })
45
+
46
+ inputElement = element.querySelector('input')
47
+ handleChange = (ev) => {
48
+ onchange({ query: ev.target.value })
49
+ }
50
+ inputElement.addEventListener('change', handleChange)
43
51
  })
44
52
 
45
53
  return () => {
54
+ if (inputElement) {
55
+ inputElement.removeEventListener('change', handleChange)
56
+ }
46
57
  geocoderInstance && geocoderInstance.remove && geocoderInstance.remove()
47
58
  }
48
59
  }