mountain-ui 1.0.120 → 1.0.122

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mountain-ui",
3
3
  "private": false,
4
- "version": "1.0.120",
4
+ "version": "1.0.122",
5
5
  "description": "A comprehensive UI component library for ERP systems with field types, entities, and registry management built with Svelte",
6
6
  "type": "module",
7
7
  "module": "index.js",
@@ -1,7 +1,8 @@
1
1
  <script>
2
+ import { onMount } from 'svelte';
2
3
  import Label from '../../../components/Label.svelte';
3
4
  import Line from '../../../components/Line.svelte';
4
- import { Checkbox, Tag } from 'hamzus-ui';
5
+ import { Button, Checkbox, Input, Tag } from 'hamzus-ui';
5
6
 
6
7
  let {
7
8
  value = $bindable(null),
@@ -12,44 +13,70 @@
12
13
  secret = false,
13
14
  compact = false,
14
15
  choices = [],
16
+ canAddChoice = false,
15
17
  onChange = () => {},
16
18
  onChangeDetails = () => {},
17
19
  ...props
18
20
  } = $props();
19
21
 
20
- const selected = $derived.by(() => {
21
- if (!value) return [];
22
+ let freeChoices = $state([]);
23
+ let selected = $state([]);
22
24
 
23
- try {
24
- const parsed = JSON.parse(value);
25
- return Array.isArray(parsed) ? parsed : [];
26
- } catch {
27
- return [];
25
+ // Point central d'émission : combine les choix cochés + les freeChoices.
26
+ function emitChange(newSelected) {
27
+ const freeValues = freeChoices.map((f) => (f ?? '').trim()).filter((f) => f.length > 0);
28
+
29
+ const values = [...selected, ...freeValues];
30
+
31
+ value = values.length ? JSON.stringify(values) : null;
32
+ onChange(value);
33
+ onChangeDetails(values);
34
+ }
35
+
36
+ function handleChange(choice) {
37
+ // si selected contient la valeur on la retire sinon on la met
38
+ if (selected.includes(choice.value)) {
39
+ selected = selected.filter((el) => el !== choice.value);
40
+ } else {
41
+ selected.push(choice.value);
28
42
  }
29
- });
30
43
 
31
- function isChecked(choice) {
32
- return selected.includes(choice.value);
44
+ emitChange();
33
45
  }
34
46
 
35
- function handleChange(choice, checked) {
36
- const values = [...selected];
47
+ function handleAddChoice() {
48
+ freeChoices = [...freeChoices, ''];
49
+ }
50
+
51
+ function handleChangeFreeChoice(index, newValue) {
52
+ freeChoices[index] = newValue;
53
+ freeChoices = [...freeChoices];
37
54
 
38
- if (checked) {
39
- if (!values.includes(choice.value)) {
40
- values.push(choice.value);
55
+ emitChange();
56
+ }
57
+
58
+ onMount(() => {
59
+ if (!value) return;
60
+
61
+ try {
62
+ const parsed = JSON.parse(value);
63
+ if (!Array.isArray(parsed)) return;
64
+
65
+ const knownValues = choices.map((c) => c.value);
66
+
67
+ const unknown = parsed.filter((v) => !knownValues.includes(v));
68
+ const known = parsed.filter((v) => knownValues.includes(v));
69
+
70
+ if (known.length) {
71
+ selected = [...selected, ...known];
41
72
  }
42
- } else {
43
- const index = values.indexOf(choice.value);
44
- if (index !== -1) {
45
- values.splice(index, 1);
73
+ if (unknown.length) {
74
+ freeChoices = [...freeChoices, ...unknown];
46
75
  }
76
+ } catch {
77
+ // valeur invalide, on ignore
47
78
  }
48
-
49
- value = values.length ? JSON.stringify(values) : null;
50
- onChange(value);
51
- onChangeDetails(values);
52
- }
79
+ });
53
80
  </script>
54
81
 
55
82
  <Line {compact}>
@@ -59,8 +86,8 @@
59
86
 
60
87
  {#each choices as choice}
61
88
  <Checkbox
62
- checked={isChecked(choice)}
63
- onChange={(checked) => handleChange(choice, checked)}
89
+ checked={selected.includes(choice.value)}
90
+ onChange={(checked) => handleChange(choice)}
64
91
  direction="rtl"
65
92
  >
66
93
  <Tag color={choice.color.replace('--', '')}>
@@ -68,6 +95,18 @@
68
95
  </Tag>
69
96
  </Checkbox>
70
97
  {/each}
98
+ {#if canAddChoice}
99
+ <Button onClick={handleAddChoice}>
100
+ <h5>ajouter un choix</h5>
101
+ </Button>
102
+ {#each freeChoices as freeChoice, index}
103
+ <Input
104
+ placeholder="mon choix {index + 1}"
105
+ value={freeChoice}
106
+ onChange={(newValue) => handleChangeFreeChoice(index, newValue)}
107
+ />
108
+ {/each}
109
+ {/if}
71
110
  </div>
72
111
  </Line>
73
112
 
@@ -4,6 +4,9 @@
4
4
 
5
5
  let { value, choices } = $props();
6
6
 
7
+ let decodedValue = $state([]);
8
+
9
+
7
10
  $effect(() => {
8
11
  decodedValue = value.formated ?? [];
9
12
  });
@@ -12,9 +15,11 @@
12
15
 
13
16
  <div class="tags">
14
17
  {#each decodedValue as tagValue}
15
- {@const data = choices.find((el) => el.value === tagValue)}
18
+ {@const data = choices?.find((el) => el.value === tagValue)}
16
19
  {#if data}
17
20
  <Tag label={tagValue} color={data.color.replace('--', '')}></Tag>
21
+ {:else}
22
+ <Tag label={tagValue} style="--tag-color:var(--bg-blur);--font-color:var(--font-2);"></Tag>
18
23
  {/if}
19
24
  {/each}
20
25
  </div>
@@ -3,9 +3,9 @@
3
3
  import LineView from '../../../components/LineView.svelte';
4
4
  import { Tag } from 'hamzus-ui';
5
5
 
6
- let { value, choices, compact, unique, secret, label, required } = $props();
6
+ let { value, choices, compact, unique, secret, label, required } = $props();
7
7
 
8
- let decodedValue = $state([])
8
+ let decodedValue = $state([]);
9
9
 
10
10
  $effect(() => {
11
11
  decodedValue = [...(value.formated ?? [])];
@@ -19,6 +19,8 @@
19
19
  {@const data = choices.find((el) => el.value === tagValue)}
20
20
  {#if data}
21
21
  <Tag label={tagValue} color={data.color.replace('--', '')}></Tag>
22
+ {:else}
23
+ <Tag label={tagValue} style="--tag-color:var(--bg-blur);--font-color:var(--font-2);"></Tag>
22
24
  {/if}
23
25
  {/each}
24
26
  </div>