@onsvisual/svelte-components 0.1.61 → 0.1.63

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,7 @@
37
37
 
38
38
  onMount(() => {
39
39
  if (sections && observer) {
40
- $sections = [...$sections, { title, id, subsection }];
40
+ $sections = [...section.parentElement.childNodes].filter((c) => c.tagName === "SECTION");
41
41
  $observer.observe(section);
42
42
  }
43
43
  });
@@ -50,7 +50,14 @@
50
50
  });
51
51
  </script>
52
52
 
53
- <section id="{id}" class="{cls}" aria-label="{title}" bind:this="{section}">
53
+ <section
54
+ id="{id}"
55
+ title="{title}"
56
+ data-subsection="{subsection}"
57
+ class="{cls}"
58
+ aria-label="{title}"
59
+ bind:this="{section}"
60
+ >
54
61
  {#if title}
55
62
  {#if subsection}
56
63
  <h3 class="subsection-title ons-u-fs-m" class:ons-u-vh="{hideTitle}">{title}</h3>
@@ -114,7 +114,10 @@
114
114
  {/if}
115
115
  <ol class="ons-list ons-u-mb-m ons-list--dashed">
116
116
  {#each $sections as section}
117
- <li class="ons-list__item" class:ons-list__item-indented="{section.subsection}">
117
+ <li
118
+ class="ons-list__item"
119
+ class:ons-list__item-indented="{section.dataset.subsection === 'true'}"
120
+ >
118
121
  <a
119
122
  href="#{section.id}"
120
123
  class="ons-list__link"
@@ -25,20 +25,22 @@
25
25
 
26
26
  onMount(() => {
27
27
  if (sections) {
28
- if (!$selected) $selected = id;
29
- $sections = [...$sections, { title, id, el }];
28
+ $sections = [...el.parentElement.childNodes].filter((c) => c.tagName === "SECTION");
29
+ if ($selected === null || $selected === undefined) $selected = $sections[0]?.id;
30
30
  }
31
31
  });
32
32
 
33
33
  onDestroy(() => {
34
34
  if (sections) {
35
35
  $sections = $sections.filter((s) => s.id !== id);
36
+ if ($selected === id) $selected = $sections.length > 0 ? $sections[0].id : null;
36
37
  }
37
38
  });
38
39
  </script>
39
40
 
40
41
  <section
41
42
  id="{id}"
43
+ title="{title}"
42
44
  class="ons-tabs__panel"
43
45
  bind:this="{el}"
44
46
  class:ons-tabs__panel--hidden="{$selected !== id}"
@@ -29,8 +29,7 @@
29
29
  $_selected = id;
30
30
  [...$sections].forEach(
31
31
  (s) =>
32
- (s.el.className =
33
- s.id === id ? "ons-tabs__panel" : "ons-tabs__panel ons-tabs__panel--hidden")
32
+ (s.className = s.id === id ? "ons-tabs__panel" : "ons-tabs__panel ons-tabs__panel--hidden")
34
33
  );
35
34
  dispatch("change", { id });
36
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "0.1.61",
3
+ "version": "0.1.63",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "homepage": "https://onsvisual.github.io/svelte-components",