@dk-address/web 0.1.2 → 0.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.
@@ -18,6 +18,11 @@ export interface DkAddressElement extends HTMLElement {
18
18
  fetchDetails: boolean;
19
19
  /** The id of a saved address, filled in when the field connects and when its form resets. */
20
20
  defaultAddressId: string | null;
21
+ /**
22
+ * The picked address's id, or `""` before a pick and after the text is edited. Set it, or the
23
+ * `address-id` attribute, to fill the field from a saved id; `""` empties the field.
24
+ */
25
+ addressId: string;
21
26
  /** The name a hidden input submits the picked address's id under. */
22
27
  idName: string | null;
23
28
  /** The name the text submits under in a form. */
@@ -49,6 +54,11 @@ export interface DkAddressElement extends HTMLElement {
49
54
  export type AddressSelectEvent = CustomEvent<Selection>;
50
55
  /** `statechange` on `<dk-address>`: the field's state changed. It doesn't bubble. */
51
56
  export type StateChangeEvent = CustomEvent<AutosuggestState>;
57
+ /** `addressidchange` on `<dk-address>`: `addressId` changed, to `value`. It bubbles. */
58
+ export type AddressIdChangeEvent = CustomEvent<{
59
+ value: string;
60
+ selection: Selection | null;
61
+ }>;
52
62
  declare global {
53
63
  interface HTMLElementTagNameMap {
54
64
  "dk-address": DkAddressElement;
@@ -56,5 +66,6 @@ declare global {
56
66
  interface HTMLElementEventMap {
57
67
  addressselect: AddressSelectEvent;
58
68
  statechange: StateChangeEvent;
69
+ addressidchange: AddressIdChangeEvent;
59
70
  }
60
71
  }
package/dist/elements.js CHANGED
@@ -3,6 +3,22 @@ import { createAddressAutosuggest, } from "@dk-address/core";
3
3
  import { autocomplete } from "@ui-sdk/core/autocomplete";
4
4
  import { createParts, wrappedButton, wrappedChild } from "@ui-sdk/web";
5
5
  const bindings = new WeakMap();
6
+ /** `addressId`s set before the element connected, for when it does. */
7
+ const pendingIds = new WeakMap();
8
+ /** Fill the field from `id`, or empty it for `""`, unless it already shows or is fetching that id. */
9
+ function setAddressId(binding, id) {
10
+ const { state, store } = binding;
11
+ if (!id) {
12
+ binding.wantedId = "";
13
+ if (state.value || state.selection)
14
+ store.clear();
15
+ return;
16
+ }
17
+ if (id === binding.wantedId || id === state.selection?.suggestion.id)
18
+ return;
19
+ binding.wantedId = id;
20
+ void store.selectById(id);
21
+ }
6
22
  /** Our attributes, beside the ones ui-sdk's Autocomplete takes (name, required, disabled and so on). */
7
23
  const kinds = {
8
24
  kind: "string",
@@ -188,9 +204,10 @@ function bind(el) {
188
204
  store,
189
205
  state: store.getState(),
190
206
  byKey: new Map(),
191
- highlighted: null,
192
207
  pickHandled: false,
193
208
  caretRevision: null,
209
+ id: "",
210
+ wantedId: "",
194
211
  stop: () => { },
195
212
  };
196
213
  const pick = (key) => {
@@ -206,6 +223,15 @@ function bind(el) {
206
223
  binding.state = state;
207
224
  apply(el, binding);
208
225
  el.dispatchEvent(new CustomEvent("statechange", { detail: state }));
226
+ const id = state.selection?.suggestion.id ?? "";
227
+ if (id !== binding.id) {
228
+ binding.id = id;
229
+ binding.wantedId = id;
230
+ el.dispatchEvent(new CustomEvent("addressidchange", {
231
+ detail: { value: id, selection: state.selection },
232
+ bubbles: true,
233
+ }));
234
+ }
209
235
  });
210
236
  el.addEventListener("valuechange", (event) => {
211
237
  const { detail } = event;
@@ -235,11 +261,14 @@ function bind(el) {
235
261
  return;
236
262
  if (detail.reason === "item-press") {
237
263
  // When a pick's label equals the current text, ui-sdk skips the text change and the
238
- // pick only shows up here, so fall back to the highlighted suggestion.
239
- const highlighted = binding.highlighted;
264
+ // pick only shows up here. A tap highlights nothing, so read the item it landed on.
265
+ const target = detail.event?.target;
266
+ const item = (target instanceof Element ? target.closest("dk-address-item") : null) ??
267
+ el.querySelector("dk-address-item[data-highlighted]");
268
+ const key = item?.getAttribute("value");
240
269
  queueMicrotask(() => {
241
270
  if (!binding.pickHandled)
242
- pick(highlighted);
271
+ pick(key);
243
272
  binding.pickHandled = false;
244
273
  });
245
274
  event.preventDefault();
@@ -247,10 +276,6 @@ function bind(el) {
247
276
  }
248
277
  store.setOpen(detail.value);
249
278
  }, { signal });
250
- el.addEventListener("highlight", (event) => {
251
- const { value } = event.detail;
252
- binding.highlighted = typeof value === "string" ? value : null;
253
- }, { signal });
254
279
  // ui-sdk reports no reset while the text is controlled, so follow the form ourselves.
255
280
  el.ownerDocument.addEventListener("reset", (event) => {
256
281
  const form = el.querySelector("dk-address-input input")?.form;
@@ -260,11 +285,17 @@ function bind(el) {
260
285
  setTimeout(() => {
261
286
  store.clear();
262
287
  if (el.defaultAddressId)
263
- void store.selectById(el.defaultAddressId);
288
+ setAddressId(binding, el.defaultAddressId);
264
289
  });
265
290
  }, { signal });
266
- const observer = new MutationObserver(() => store.setOptions(optionsOf(el)));
267
- observer.observe(el, { attributeFilter: ownAttributes });
291
+ const observer = new MutationObserver((records) => {
292
+ const names = new Set(records.map((record) => record.attributeName));
293
+ if (names.delete("address-id"))
294
+ setAddressId(binding, el.getAttribute("address-id") ?? "");
295
+ if (names.size > 0)
296
+ store.setOptions(optionsOf(el));
297
+ });
298
+ observer.observe(el, { attributeFilter: [...ownAttributes, "address-id"] });
268
299
  binding.stop = () => {
269
300
  unsubscribe();
270
301
  listeners.abort();
@@ -272,8 +303,16 @@ function bind(el) {
272
303
  store.destroy();
273
304
  };
274
305
  apply(el, binding);
275
- if (el.defaultAddressId)
276
- void store.selectById(el.defaultAddressId);
306
+ // A property set before the element was defined is an own property, hiding our accessor.
307
+ if (Object.hasOwn(el, "addressId")) {
308
+ const id = el.addressId;
309
+ delete el.addressId;
310
+ pendingIds.set(el, typeof id === "string" ? id : "");
311
+ }
312
+ const startId = pendingIds.get(el) ?? el.getAttribute("address-id") ?? el.defaultAddressId;
313
+ pendingIds.delete(el);
314
+ if (startId)
315
+ setAddressId(binding, startId);
277
316
  return binding;
278
317
  }
279
318
  const parts = createParts(autocomplete, {
@@ -334,6 +373,20 @@ if (Root) {
334
373
  return bindings.get(this)?.state.selection ?? null;
335
374
  },
336
375
  },
376
+ addressId: {
377
+ get() {
378
+ const binding = bindings.get(this);
379
+ return binding ? binding.id : (pendingIds.get(this) ?? "");
380
+ },
381
+ set(id) {
382
+ const value = typeof id === "string" ? id : "";
383
+ const binding = bindings.get(this);
384
+ if (binding)
385
+ setAddressId(binding, value);
386
+ else
387
+ pendingIds.set(this, value);
388
+ },
389
+ },
337
390
  selectById: {
338
391
  value(id) {
339
392
  const binding = bindings.get(this);
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./elements.ts";
2
- export type { AddressSelectEvent, DkAddressElement, StateChangeEvent } from "./elements.ts";
2
+ export type { AddressIdChangeEvent, AddressSelectEvent, DkAddressElement, StateChangeEvent, } from "./elements.ts";
3
3
  export { createClient, suggestionKey } from "@dk-address/client";
4
4
  export type { AccessAddress, Address, AdressevaelgerClient, ClientOptions, SearchKind, Suggestion, SuggestionType, } from "@dk-address/client";
5
5
  export type { AutosuggestState, Selection } from "@dk-address/core";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dk-address/web",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Danish address autocomplete as web components, built on KDS Adressevælger. For plain HTML and any framework.",
5
5
  "keywords": [
6
6
  "address",
@@ -45,10 +45,10 @@
45
45
  "test": "vitest run"
46
46
  },
47
47
  "dependencies": {
48
- "@dk-address/client": "0.1.2",
49
- "@dk-address/core": "0.1.2",
50
- "@ui-sdk/core": "0.1.0-beta.17",
51
- "@ui-sdk/web": "0.1.0-beta.17"
48
+ "@dk-address/client": "0.1.3",
49
+ "@dk-address/core": "0.1.3",
50
+ "@ui-sdk/core": "0.1.0-beta.20",
51
+ "@ui-sdk/web": "0.1.0-beta.20"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@repo/kds-fixtures": "0.0.0",