@onsvisual/svelte-components 0.1.62 → 0.1.64

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.
@@ -1,5 +1,5 @@
1
1
  <script>
2
- import { createEventDispatcher } from "svelte";
2
+ import { onMount, onDestroy, getContext, createEventDispatcher } from "svelte";
3
3
 
4
4
  const dispatch = createEventDispatcher();
5
5
 
@@ -54,13 +54,41 @@
54
54
  */
55
55
  export let compact = false;
56
56
 
57
+ const checkboxes = getContext("checkboxes");
58
+
59
+ let el;
60
+
61
+ function findAncestor(el, cls) {
62
+ while ((el = el.parentElement) && !el.classList.contains(cls));
63
+ return el;
64
+ }
65
+
66
+ function updateGroup() {
67
+ const newGroup = $checkboxes.filter((c) => c.checked).map((c) => c.id);
68
+ if (newGroup.join() !== group.join()) group = newGroup;
69
+ }
70
+
57
71
  function doChange(e) {
58
- if (Array.isArray(group)) {
59
- if (checked) group = [...group, value];
60
- else group = group.filter((d) => d != value);
72
+ if (Array.isArray(group) && Array.isArray($checkboxes)) {
73
+ updateGroup();
61
74
  }
62
- dispatch("change", e);
75
+ dispatch("change", { id, checked, group, e });
63
76
  }
77
+
78
+ onMount(() => {
79
+ if (Array.isArray(group) && Array.isArray($checkboxes)) {
80
+ const root = findAncestor(el, "ons-checkboxes__items");
81
+ $checkboxes = [...root.getElementsByTagName("input")];
82
+ updateGroup();
83
+ }
84
+ });
85
+
86
+ onDestroy(() => {
87
+ if (Array.isArray(group) && Array.isArray($checkboxes)) {
88
+ $checkboxes = $checkboxes.filter((c) => c.id !== id);
89
+ updateGroup();
90
+ }
91
+ });
64
92
  </script>
65
93
 
66
94
  <span
@@ -79,6 +107,7 @@
79
107
  disabled="{disabled}"
80
108
  aria-disabled="{disabled}"
81
109
  on:change="{doChange}"
110
+ bind:this="{el}"
82
111
  />
83
112
  <label
84
113
  class="ons-checkbox__label"
@@ -1,4 +1,6 @@
1
1
  <script>
2
+ import { setContext } from "svelte";
3
+ import { writable } from "svelte/store";
2
4
  import Checkbox from "../Checkbox/Checkbox.svelte";
3
5
 
4
6
  /**
@@ -18,6 +20,9 @@
18
20
  * @type {array}
19
21
  */
20
22
  export let value = [];
23
+
24
+ const checkboxes = writable([]);
25
+ setContext("checkboxes", checkboxes);
21
26
  </script>
22
27
 
23
28
  {#if label}
@@ -26,7 +31,7 @@
26
31
  <div class="ons-checkboxes__items">
27
32
  <slot />
28
33
  {#if items}
29
- {#each items as item, i}
34
+ {#each items as item}
30
35
  <Checkbox {...item} bind:group="{value}" compact="{compact}" on:change />
31
36
  {/each}
32
37
  {/if}
@@ -37,7 +37,9 @@
37
37
 
38
38
  onMount(() => {
39
39
  if (sections && observer) {
40
- $sections = [...section.parentElement.childNodes].filter((c) => c.tagName === "SECTION");
40
+ $sections = [...section.parentElement.getElementsByTagName("section")].filter(
41
+ (c) => c.id && c.title
42
+ );
41
43
  $observer.observe(section);
42
44
  }
43
45
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "0.1.62",
3
+ "version": "0.1.64",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "homepage": "https://onsvisual.github.io/svelte-components",