@digdir/designsystemet-react 1.0.0 → 1.0.1

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.
@@ -0,0 +1,160 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react = require('react');
6
+ require('@u-elements/u-tags');
7
+ var react$1 = require('@floating-ui/react');
8
+ var uDatalist = require('@u-elements/u-datalist');
9
+ var cl = require('clsx/lite');
10
+
11
+ const MultiSuggestionContext = react.createContext({});
12
+ const MultiSuggestion = react.forwardRef(function MultiSuggestion({ value, defaultValue, onValueChange, name, filter = true, allowCreate = false, className, ...rest }, ref) {
13
+ const [listId, setListId] = react.useState(react.useId());
14
+ const [selectedItems, setSelectedItems] = react.useState({});
15
+ const inputRef = react.useRef(null);
16
+ const uTagsRef = react.useRef(null);
17
+ const mergedRefs = react$1.useMergeRefs([ref, uTagsRef]);
18
+ const isControlled = Boolean(value);
19
+ const [controlledDirty, setControlledDirty] = react.useState(false);
20
+ /**
21
+ * If we have set a default value, set it on initial render
22
+ */
23
+ react.useEffect(() => {
24
+ if (!defaultValue)
25
+ return;
26
+ if (value) {
27
+ console.warn('defaultValue can not be used in combination with value');
28
+ return;
29
+ }
30
+ const items = uTagsRef.current?.querySelectorAll('u-option');
31
+ if (!items)
32
+ return;
33
+ const defaultItems = Array.from(items).filter((item) => defaultValue.includes(item.value));
34
+ for (const item of defaultItems) {
35
+ uTagsRef.current?.dispatchEvent(new CustomEvent('add', {
36
+ detail: { item },
37
+ }));
38
+ setSelectedItems((prevItems) => ({
39
+ ...prevItems,
40
+ [item.value]: item,
41
+ }));
42
+ }
43
+ return () => {
44
+ console.error('Default value changed during render');
45
+ };
46
+ }, [defaultValue]);
47
+ /**
48
+ * Controlled state management
49
+ */
50
+ react.useEffect(() => {
51
+ if (!value)
52
+ return;
53
+ const items = inputRef.current?.list?.options;
54
+ if (!items)
55
+ return;
56
+ const itemsArray = Array.from(items);
57
+ const itemsArrayValues = itemsArray.map((item) => item.value);
58
+ const selectedArray = Object.keys(selectedItems);
59
+ const validValues = value.filter((val) => itemsArrayValues.includes(val));
60
+ const itemsToAdd = validValues.filter((val) => !selectedArray.includes(val));
61
+ const itemsToRemove = selectedArray.filter((val) => !validValues.includes(val));
62
+ for (const item of itemsArray) {
63
+ if (itemsToAdd.includes(item.value)) {
64
+ uTagsRef.current?.dispatchEvent(new CustomEvent('add', {
65
+ detail: { item },
66
+ }));
67
+ setSelectedItems((prevItems) => ({
68
+ ...prevItems,
69
+ [item.value]: item,
70
+ }));
71
+ }
72
+ if (itemsToRemove.includes(item.value)) {
73
+ uTagsRef.current?.dispatchEvent(new CustomEvent('remove', {
74
+ detail: { item },
75
+ }));
76
+ setSelectedItems((prevItems) => {
77
+ const { [item.value]: _, ...rest } = prevItems;
78
+ return rest;
79
+ });
80
+ }
81
+ }
82
+ }, [value]);
83
+ /**
84
+ * Listerners and handling of adding/removing
85
+ */
86
+ react.useEffect(() => {
87
+ if (!uTagsRef?.current)
88
+ return;
89
+ const handleItemsChange = (e) => {
90
+ e.preventDefault();
91
+ const item = e.detail.item;
92
+ if (e.detail.action === 'add') {
93
+ /**
94
+ * If creating is off, check if the value is allowed to be added
95
+ */
96
+ if (!allowCreate) {
97
+ const optionExists = Array.from(inputRef.current?.list?.options || [], uDatalist.getDatalistValue).includes(item.value);
98
+ if (!optionExists)
99
+ return;
100
+ }
101
+ setSelectedItems((prevItems) => ({
102
+ ...prevItems,
103
+ [item.value]: item,
104
+ }));
105
+ }
106
+ if (e.detail.action === 'remove') {
107
+ setSelectedItems((prevItems) => {
108
+ const { [item.value]: _, ...rest } = prevItems;
109
+ return rest;
110
+ });
111
+ }
112
+ if (isControlled)
113
+ setControlledDirty(true);
114
+ };
115
+ uTagsRef.current.addEventListener('tags', handleItemsChange);
116
+ return () => {
117
+ uTagsRef.current?.removeEventListener('tags', handleItemsChange);
118
+ };
119
+ }, [uTagsRef, setSelectedItems]);
120
+ /**
121
+ * When controlled, trigger onValueChange callback for ordinary add/remove
122
+ */
123
+ react.useEffect(() => {
124
+ if (!controlledDirty)
125
+ return;
126
+ onValueChange?.(Object.keys(selectedItems));
127
+ setControlledDirty(false);
128
+ }, [controlledDirty]);
129
+ const handleFilter = react.useCallback((input) => {
130
+ const list = input?.list;
131
+ // Let <datalist> handle filtering if filter is true
132
+ if (filter === true || !list)
133
+ return;
134
+ // Handle custom filter
135
+ if (filter !== false) {
136
+ let index = 0;
137
+ for (const option of list.getElementsByTagName('u-option')) {
138
+ if (!option.hasAttribute('data-empty'))
139
+ option.disabled = !filter({
140
+ index: index++, // Increment index for each <option>
141
+ input,
142
+ optionElement: option,
143
+ text: option.text,
144
+ value: uDatalist.getDatalistValue(option),
145
+ });
146
+ }
147
+ }
148
+ uDatalist.syncDatalistState(input); // Sync the datalist state if filter is custom or false
149
+ }, [filter]);
150
+ return (jsxRuntime.jsxs(MultiSuggestionContext.Provider, { value: {
151
+ inputRef,
152
+ listId,
153
+ selectedItems,
154
+ setListId,
155
+ handleFilter,
156
+ }, children: [jsxRuntime.jsx("u-tags", { class: cl('ds-multi-suggestion', className), ref: mergedRefs, ...rest }), name && (jsxRuntime.jsx("select", { multiple: true, hidden: true, name: name, children: Object.values(selectedItems).map((item) => (jsxRuntime.jsx("option", { value: item.value }, item.value))) }))] }));
157
+ });
158
+
159
+ exports.MultiSuggestion = MultiSuggestion;
160
+ exports.MultiSuggestionContext = MultiSuggestionContext;
@@ -0,0 +1,20 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react = require('react');
6
+ var index = require('../Chip/index.js');
7
+ var MultiSuggestion = require('./MultiSuggestion.js');
8
+
9
+ const MultiSuggestionChips = ({ render = ({ value }) => value, }) => {
10
+ const { selectedItems } = react.useContext(MultiSuggestion.MultiSuggestionContext);
11
+ return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: selectedItems &&
12
+ Object.values(selectedItems).map((item) => (jsxRuntime.jsx(index.Chip.Removable, { value: item.value, asChild: true, children: jsxRuntime.jsx("data", { children: render({
13
+ text: item.textContent || item.value,
14
+ value: item.value,
15
+ element: item,
16
+ }) }) }, item.value))) }));
17
+ };
18
+ MultiSuggestionChips.displayName = 'MultiSuggestionChips';
19
+
20
+ exports.MultiSuggestionChips = MultiSuggestionChips;
@@ -0,0 +1,52 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react = require('react');
6
+ var MultiSuggestion = require('./MultiSuggestion.js');
7
+ var Button = require('../Button/Button.js');
8
+
9
+ /**
10
+ * Component that provides a clear button for the MultiSuggestion input.
11
+ *
12
+ * Place as a descendant of `MultiSuggestion`
13
+ *
14
+ * @example
15
+ * <MultiSuggestion>
16
+ * <MultiSuggestion.Input />
17
+ * <MultiSuggestion.Clear />
18
+ * <MultiSuggestion.List />
19
+ * </MultiSuggestion>
20
+ */
21
+ const MultiSuggestionClear = react.forwardRef(function MultiSuggestionClear({ 'aria-label': label = 'Tøm', onClick, ...rest }, ref) {
22
+ const { inputRef } = react.useContext(MultiSuggestion.MultiSuggestionContext); //, handleValueChange
23
+ const handleClear = (event) => {
24
+ if (!inputRef?.current)
25
+ throw new Error('Input is missing');
26
+ /* narrow type to make TS happy */
27
+ if (!(inputRef?.current instanceof HTMLInputElement))
28
+ throw new Error('Input is not an input element');
29
+ event.preventDefault();
30
+ setReactInputValue(inputRef.current, '');
31
+ inputRef.current.focus();
32
+ onClick?.(event);
33
+ };
34
+ return (jsxRuntime.jsx(Button.Button, { ref: ref, variant: 'tertiary', type: 'reset', "aria-label": label, onClick: handleClear, icon: true, ...rest }));
35
+ });
36
+ // Copied from https://github.com/facebook/react/issues/11488#issuecomment-1300987446
37
+ const setReactInputValue = (input, value) => {
38
+ const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;
39
+ if (nativeInputValueSetter) {
40
+ nativeInputValueSetter.call(input, value);
41
+ }
42
+ else {
43
+ throw new Error('Unable to find the native input value setter');
44
+ }
45
+ const inputEvent = new Event('input', { bubbles: true });
46
+ const changeEvent = new Event('change', { bubbles: true });
47
+ input.dispatchEvent(inputEvent);
48
+ input.dispatchEvent(changeEvent);
49
+ };
50
+
51
+ exports.MultiSuggestionClear = MultiSuggestionClear;
52
+ exports.setReactInputValue = setReactInputValue;
@@ -0,0 +1,23 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react = require('react');
6
+
7
+ /**
8
+ * Component that provides an empty MultiSuggestion list.
9
+ *
10
+ * Place as a descendant of `MultiSuggestion.List`
11
+ *
12
+ * @example
13
+ * <MultiSuggestion.List>
14
+ * <MultiSuggestion.Empty>Tomt</MultiSuggestion.Empty>
15
+ * </MultiSuggestion.List>
16
+ */
17
+ const MultiSuggestionEmpty = react.forwardRef(function MultiSuggestionEmpty(rest, ref) {
18
+ return (
19
+ // biome-ignore lint/a11y/noInteractiveElementToNoninteractiveRole: Empty option shoult not be interactive
20
+ jsxRuntime.jsx("u-option", { "data-empty": true, role: 'none', ref: ref, ...rest }));
21
+ });
22
+
23
+ exports.MultiSuggestionEmpty = MultiSuggestionEmpty;
@@ -0,0 +1,37 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react$1 = require('@floating-ui/react');
6
+ var uDatalist = require('@u-elements/u-datalist');
7
+ var react = require('react');
8
+ var MultiSuggestion = require('./MultiSuggestion.js');
9
+ var Input = require('../Input/Input.js');
10
+
11
+ /**
12
+ * Component that provides an input field for the MultiSuggestion list.
13
+ *
14
+ * Place as a descendant of `MultiSuggestion`
15
+ *
16
+ * @example
17
+ * <MultiSuggestion>
18
+ * <MultiSuggestion.Input />
19
+ * <MultiSuggestion.List />
20
+ * </MultiSuggestion>
21
+ */
22
+ const MultiSuggestionInput = react.forwardRef(function MultiSuggestionList({ value, onInput, ...rest }, ref) {
23
+ const { listId, inputRef, handleFilter } = react.useContext(MultiSuggestion.MultiSuggestionContext);
24
+ const mergedRefs = react$1.useMergeRefs([inputRef, ref]);
25
+ // Update also if controlled value
26
+ react.useEffect(() => {
27
+ handleFilter?.(inputRef?.current);
28
+ }, [value]);
29
+ return (jsxRuntime.jsx(Input.Input, { ref: mergedRefs, list: listId, value: value, onInput: (event) => {
30
+ onInput?.(event); // Should run first
31
+ if (!uDatalist.isDatalistClick(event.nativeEvent))
32
+ handleFilter?.(inputRef?.current);
33
+ }, placeholder: '' // We need an empty placeholder for the clear button to be able to show/hide
34
+ , ...rest }));
35
+ });
36
+
37
+ exports.MultiSuggestionInput = MultiSuggestionInput;
@@ -0,0 +1,30 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react = require('react');
6
+ require('@u-elements/u-datalist');
7
+ var MultiSuggestion = require('./MultiSuggestion.js');
8
+
9
+ /**
10
+ * Component that provides a MultiSuggestion list.
11
+ *
12
+ * Place as a descendant of `MultiSuggestion`
13
+ *
14
+ * @example
15
+ * <MultiSuggestion>
16
+ * <MultiSuggestion.Input />
17
+ * <MultiSuggestion.List />
18
+ * </MultiSuggestion>
19
+ */
20
+ const MultiSuggestionList = react.forwardRef(function MultiSuggestionList({ singular = '%d forslag', plural = '%d forslag', className, id, ...rest }, ref) {
21
+ const { inputRef, listId, setListId, handleFilter } = react.useContext(MultiSuggestion.MultiSuggestionContext);
22
+ react.useEffect(() => handleFilter?.(inputRef?.current)); // Must run on every render
23
+ react.useEffect(() => {
24
+ if (id && listId !== id)
25
+ setListId?.(id);
26
+ }, [listId, id, setListId]);
27
+ return (jsxRuntime.jsx("u-datalist", { "data-sr-singular": singular, "data-sr-plural": plural, class: className, id: listId, ref: ref, ...rest }));
28
+ });
29
+
30
+ exports.MultiSuggestionList = MultiSuggestionList;
@@ -0,0 +1,12 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var react = require('react');
6
+ require('@u-elements/u-datalist');
7
+
8
+ const MultiSuggestionOption = react.forwardRef(function MultiSuggestionOption({ className, ...rest }, ref) {
9
+ return (jsxRuntime.jsx("u-option", { class: className, ref: ref, ...rest }));
10
+ });
11
+
12
+ exports.MultiSuggestionOption = MultiSuggestionOption;
@@ -0,0 +1,34 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ var MultiSuggestion$1 = require('./MultiSuggestion.js');
5
+ var MultiSuggestionChips = require('./MultiSuggestionChips.js');
6
+ var MultiSuggestionClear = require('./MultiSuggestionClear.js');
7
+ var MultiSuggestionEmpty = require('./MultiSuggestionEmpty.js');
8
+ var MultiSuggestionInput = require('./MultiSuggestionInput.js');
9
+ var MultiSuggestionList = require('./MultiSuggestionList.js');
10
+ var MultiSuggestionOption = require('./MultiSuggestionOption.js');
11
+
12
+ const MultiSuggestion = Object.assign(MultiSuggestion$1.MultiSuggestion, {
13
+ Input: MultiSuggestionInput.MultiSuggestionInput,
14
+ List: MultiSuggestionList.MultiSuggestionList,
15
+ Option: MultiSuggestionOption.MultiSuggestionOption,
16
+ Chips: MultiSuggestionChips.MultiSuggestionChips,
17
+ Empty: MultiSuggestionEmpty.MultiSuggestionEmpty,
18
+ Clear: MultiSuggestionClear.MultiSuggestionClear,
19
+ });
20
+ MultiSuggestion.displayName = 'EXPERIMENTRAL_MultiSuggestion';
21
+ MultiSuggestion.Input.displayName = 'EXPERIMENTRAL_MultiSuggestion.Input';
22
+ MultiSuggestion.List.displayName = 'EXPERIMENTRAL_MultiSuggestion.List';
23
+ MultiSuggestion.Option.displayName = 'EXPERIMENTRAL_MultiSuggestion.Option';
24
+ MultiSuggestion.Chips.displayName = 'EXPERIMENTRAL_MultiSuggestion.Chips';
25
+ MultiSuggestion.Empty.displayName = 'EXPERIMENTRAL_MultiSuggestion.Empty';
26
+ MultiSuggestion.Clear.displayName = 'EXPERIMENTRAL_MultiSuggestion.Clear';
27
+
28
+ exports.EXPERIMENTAL_MultiSuggestionChips = MultiSuggestionChips.MultiSuggestionChips;
29
+ exports.EXPERIMENTAL_MultiSuggestionClear = MultiSuggestionClear.MultiSuggestionClear;
30
+ exports.EXPERIMENTAL_MultiSuggestionEmpty = MultiSuggestionEmpty.MultiSuggestionEmpty;
31
+ exports.EXPERIMENTAL_MultiSuggestionInput = MultiSuggestionInput.MultiSuggestionInput;
32
+ exports.EXPERIMENTAL_MultiSuggestionList = MultiSuggestionList.MultiSuggestionList;
33
+ exports.EXPERIMENTAL_MultiSuggestionOption = MultiSuggestionOption.MultiSuggestionOption;
34
+ exports.EXPERIMENTAL_MultiSuggestion = MultiSuggestion;
package/dist/cjs/index.js CHANGED
@@ -96,6 +96,13 @@ var SuggestionInput = require('./components/Suggestion/SuggestionInput.js');
96
96
  var SuggestionEmpty = require('./components/Suggestion/SuggestionEmpty.js');
97
97
  var SuggestionOption = require('./components/Suggestion/SuggestionOption.js');
98
98
  var SuggestionClear = require('./components/Suggestion/SuggestionClear.js');
99
+ var index$k = require('./components/MultiSuggestion/index.js');
100
+ var MultiSuggestionInput = require('./components/MultiSuggestion/MultiSuggestionInput.js');
101
+ var MultiSuggestionList = require('./components/MultiSuggestion/MultiSuggestionList.js');
102
+ var MultiSuggestionOption = require('./components/MultiSuggestion/MultiSuggestionOption.js');
103
+ var MultiSuggestionChips = require('./components/MultiSuggestion/MultiSuggestionChips.js');
104
+ var MultiSuggestionEmpty = require('./components/MultiSuggestion/MultiSuggestionEmpty.js');
105
+ var MultiSuggestionClear = require('./components/MultiSuggestion/MultiSuggestionClear.js');
99
106
  var omit = require('./utilities/omit/omit.js');
100
107
  var useCheckboxGroup = require('./utilities/hooks/useCheckboxGroup/useCheckboxGroup.js');
101
108
  var useDebounceCallback = require('./utilities/hooks/useDebounceCallback/useDebounceCallback.js');
@@ -210,6 +217,13 @@ exports.EXPERIMENTAL_SuggestionInput = SuggestionInput.SuggestionInput;
210
217
  exports.EXPERIMENTAL_SuggestionEmpty = SuggestionEmpty.SuggestionEmpty;
211
218
  exports.EXPERIMENTAL_SuggestionOption = SuggestionOption.SuggestionOption;
212
219
  exports.EXPERIMENTAL_SuggestionClear = SuggestionClear.SuggestionClear;
220
+ exports.EXPERIMENTAL_MultiSuggestion = index$k.EXPERIMENTAL_MultiSuggestion;
221
+ exports.EXPERIMENTAL_MultiSuggestionInput = MultiSuggestionInput.MultiSuggestionInput;
222
+ exports.EXPERIMENTAL_MultiSuggestionList = MultiSuggestionList.MultiSuggestionList;
223
+ exports.EXPERIMENTAL_MultiSuggestionOption = MultiSuggestionOption.MultiSuggestionOption;
224
+ exports.EXPERIMENTAL_MultiSuggestionChips = MultiSuggestionChips.MultiSuggestionChips;
225
+ exports.EXPERIMENTAL_MultiSuggestionEmpty = MultiSuggestionEmpty.MultiSuggestionEmpty;
226
+ exports.EXPERIMENTAL_MultiSuggestionClear = MultiSuggestionClear.MultiSuggestionClear;
213
227
  exports.omit = omit.omit;
214
228
  exports.useCheckboxGroup = useCheckboxGroup.useCheckboxGroup;
215
229
  exports.useDebounceCallback = useDebounceCallback.useDebounceCallback;
@@ -0,0 +1,157 @@
1
+ 'use client';
2
+ import { jsxs, jsx } from 'react/jsx-runtime';
3
+ import { forwardRef, createContext, useState, useId, useRef, useEffect, useCallback } from 'react';
4
+ import '@u-elements/u-tags';
5
+ import { useMergeRefs } from '@floating-ui/react';
6
+ import { getDatalistValue, syncDatalistState } from '@u-elements/u-datalist';
7
+ import cl from 'clsx/lite';
8
+
9
+ const MultiSuggestionContext = createContext({});
10
+ const MultiSuggestion = forwardRef(function MultiSuggestion({ value, defaultValue, onValueChange, name, filter = true, allowCreate = false, className, ...rest }, ref) {
11
+ const [listId, setListId] = useState(useId());
12
+ const [selectedItems, setSelectedItems] = useState({});
13
+ const inputRef = useRef(null);
14
+ const uTagsRef = useRef(null);
15
+ const mergedRefs = useMergeRefs([ref, uTagsRef]);
16
+ const isControlled = Boolean(value);
17
+ const [controlledDirty, setControlledDirty] = useState(false);
18
+ /**
19
+ * If we have set a default value, set it on initial render
20
+ */
21
+ useEffect(() => {
22
+ if (!defaultValue)
23
+ return;
24
+ if (value) {
25
+ console.warn('defaultValue can not be used in combination with value');
26
+ return;
27
+ }
28
+ const items = uTagsRef.current?.querySelectorAll('u-option');
29
+ if (!items)
30
+ return;
31
+ const defaultItems = Array.from(items).filter((item) => defaultValue.includes(item.value));
32
+ for (const item of defaultItems) {
33
+ uTagsRef.current?.dispatchEvent(new CustomEvent('add', {
34
+ detail: { item },
35
+ }));
36
+ setSelectedItems((prevItems) => ({
37
+ ...prevItems,
38
+ [item.value]: item,
39
+ }));
40
+ }
41
+ return () => {
42
+ console.error('Default value changed during render');
43
+ };
44
+ }, [defaultValue]);
45
+ /**
46
+ * Controlled state management
47
+ */
48
+ useEffect(() => {
49
+ if (!value)
50
+ return;
51
+ const items = inputRef.current?.list?.options;
52
+ if (!items)
53
+ return;
54
+ const itemsArray = Array.from(items);
55
+ const itemsArrayValues = itemsArray.map((item) => item.value);
56
+ const selectedArray = Object.keys(selectedItems);
57
+ const validValues = value.filter((val) => itemsArrayValues.includes(val));
58
+ const itemsToAdd = validValues.filter((val) => !selectedArray.includes(val));
59
+ const itemsToRemove = selectedArray.filter((val) => !validValues.includes(val));
60
+ for (const item of itemsArray) {
61
+ if (itemsToAdd.includes(item.value)) {
62
+ uTagsRef.current?.dispatchEvent(new CustomEvent('add', {
63
+ detail: { item },
64
+ }));
65
+ setSelectedItems((prevItems) => ({
66
+ ...prevItems,
67
+ [item.value]: item,
68
+ }));
69
+ }
70
+ if (itemsToRemove.includes(item.value)) {
71
+ uTagsRef.current?.dispatchEvent(new CustomEvent('remove', {
72
+ detail: { item },
73
+ }));
74
+ setSelectedItems((prevItems) => {
75
+ const { [item.value]: _, ...rest } = prevItems;
76
+ return rest;
77
+ });
78
+ }
79
+ }
80
+ }, [value]);
81
+ /**
82
+ * Listerners and handling of adding/removing
83
+ */
84
+ useEffect(() => {
85
+ if (!uTagsRef?.current)
86
+ return;
87
+ const handleItemsChange = (e) => {
88
+ e.preventDefault();
89
+ const item = e.detail.item;
90
+ if (e.detail.action === 'add') {
91
+ /**
92
+ * If creating is off, check if the value is allowed to be added
93
+ */
94
+ if (!allowCreate) {
95
+ const optionExists = Array.from(inputRef.current?.list?.options || [], getDatalistValue).includes(item.value);
96
+ if (!optionExists)
97
+ return;
98
+ }
99
+ setSelectedItems((prevItems) => ({
100
+ ...prevItems,
101
+ [item.value]: item,
102
+ }));
103
+ }
104
+ if (e.detail.action === 'remove') {
105
+ setSelectedItems((prevItems) => {
106
+ const { [item.value]: _, ...rest } = prevItems;
107
+ return rest;
108
+ });
109
+ }
110
+ if (isControlled)
111
+ setControlledDirty(true);
112
+ };
113
+ uTagsRef.current.addEventListener('tags', handleItemsChange);
114
+ return () => {
115
+ uTagsRef.current?.removeEventListener('tags', handleItemsChange);
116
+ };
117
+ }, [uTagsRef, setSelectedItems]);
118
+ /**
119
+ * When controlled, trigger onValueChange callback for ordinary add/remove
120
+ */
121
+ useEffect(() => {
122
+ if (!controlledDirty)
123
+ return;
124
+ onValueChange?.(Object.keys(selectedItems));
125
+ setControlledDirty(false);
126
+ }, [controlledDirty]);
127
+ const handleFilter = useCallback((input) => {
128
+ const list = input?.list;
129
+ // Let <datalist> handle filtering if filter is true
130
+ if (filter === true || !list)
131
+ return;
132
+ // Handle custom filter
133
+ if (filter !== false) {
134
+ let index = 0;
135
+ for (const option of list.getElementsByTagName('u-option')) {
136
+ if (!option.hasAttribute('data-empty'))
137
+ option.disabled = !filter({
138
+ index: index++, // Increment index for each <option>
139
+ input,
140
+ optionElement: option,
141
+ text: option.text,
142
+ value: getDatalistValue(option),
143
+ });
144
+ }
145
+ }
146
+ syncDatalistState(input); // Sync the datalist state if filter is custom or false
147
+ }, [filter]);
148
+ return (jsxs(MultiSuggestionContext.Provider, { value: {
149
+ inputRef,
150
+ listId,
151
+ selectedItems,
152
+ setListId,
153
+ handleFilter,
154
+ }, children: [jsx("u-tags", { class: cl('ds-multi-suggestion', className), ref: mergedRefs, ...rest }), name && (jsx("select", { multiple: true, hidden: true, name: name, children: Object.values(selectedItems).map((item) => (jsx("option", { value: item.value }, item.value))) }))] }));
155
+ });
156
+
157
+ export { MultiSuggestion, MultiSuggestionContext };
@@ -0,0 +1,18 @@
1
+ 'use client';
2
+ import { jsx, Fragment } from 'react/jsx-runtime';
3
+ import { useContext } from 'react';
4
+ import { Chip } from '../Chip/index.js';
5
+ import { MultiSuggestionContext } from './MultiSuggestion.js';
6
+
7
+ const MultiSuggestionChips = ({ render = ({ value }) => value, }) => {
8
+ const { selectedItems } = useContext(MultiSuggestionContext);
9
+ return (jsx(Fragment, { children: selectedItems &&
10
+ Object.values(selectedItems).map((item) => (jsx(Chip.Removable, { value: item.value, asChild: true, children: jsx("data", { children: render({
11
+ text: item.textContent || item.value,
12
+ value: item.value,
13
+ element: item,
14
+ }) }) }, item.value))) }));
15
+ };
16
+ MultiSuggestionChips.displayName = 'MultiSuggestionChips';
17
+
18
+ export { MultiSuggestionChips };
@@ -0,0 +1,49 @@
1
+ 'use client';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { forwardRef, useContext } from 'react';
4
+ import { MultiSuggestionContext } from './MultiSuggestion.js';
5
+ import { Button } from '../Button/Button.js';
6
+
7
+ /**
8
+ * Component that provides a clear button for the MultiSuggestion input.
9
+ *
10
+ * Place as a descendant of `MultiSuggestion`
11
+ *
12
+ * @example
13
+ * <MultiSuggestion>
14
+ * <MultiSuggestion.Input />
15
+ * <MultiSuggestion.Clear />
16
+ * <MultiSuggestion.List />
17
+ * </MultiSuggestion>
18
+ */
19
+ const MultiSuggestionClear = forwardRef(function MultiSuggestionClear({ 'aria-label': label = 'Tøm', onClick, ...rest }, ref) {
20
+ const { inputRef } = useContext(MultiSuggestionContext); //, handleValueChange
21
+ const handleClear = (event) => {
22
+ if (!inputRef?.current)
23
+ throw new Error('Input is missing');
24
+ /* narrow type to make TS happy */
25
+ if (!(inputRef?.current instanceof HTMLInputElement))
26
+ throw new Error('Input is not an input element');
27
+ event.preventDefault();
28
+ setReactInputValue(inputRef.current, '');
29
+ inputRef.current.focus();
30
+ onClick?.(event);
31
+ };
32
+ return (jsx(Button, { ref: ref, variant: 'tertiary', type: 'reset', "aria-label": label, onClick: handleClear, icon: true, ...rest }));
33
+ });
34
+ // Copied from https://github.com/facebook/react/issues/11488#issuecomment-1300987446
35
+ const setReactInputValue = (input, value) => {
36
+ const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;
37
+ if (nativeInputValueSetter) {
38
+ nativeInputValueSetter.call(input, value);
39
+ }
40
+ else {
41
+ throw new Error('Unable to find the native input value setter');
42
+ }
43
+ const inputEvent = new Event('input', { bubbles: true });
44
+ const changeEvent = new Event('change', { bubbles: true });
45
+ input.dispatchEvent(inputEvent);
46
+ input.dispatchEvent(changeEvent);
47
+ };
48
+
49
+ export { MultiSuggestionClear, setReactInputValue };
@@ -0,0 +1,21 @@
1
+ 'use client';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { forwardRef } from 'react';
4
+
5
+ /**
6
+ * Component that provides an empty MultiSuggestion list.
7
+ *
8
+ * Place as a descendant of `MultiSuggestion.List`
9
+ *
10
+ * @example
11
+ * <MultiSuggestion.List>
12
+ * <MultiSuggestion.Empty>Tomt</MultiSuggestion.Empty>
13
+ * </MultiSuggestion.List>
14
+ */
15
+ const MultiSuggestionEmpty = forwardRef(function MultiSuggestionEmpty(rest, ref) {
16
+ return (
17
+ // biome-ignore lint/a11y/noInteractiveElementToNoninteractiveRole: Empty option shoult not be interactive
18
+ jsx("u-option", { "data-empty": true, role: 'none', ref: ref, ...rest }));
19
+ });
20
+
21
+ export { MultiSuggestionEmpty };
@@ -0,0 +1,35 @@
1
+ 'use client';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { useMergeRefs } from '@floating-ui/react';
4
+ import { isDatalistClick } from '@u-elements/u-datalist';
5
+ import { forwardRef, useContext, useEffect } from 'react';
6
+ import { MultiSuggestionContext } from './MultiSuggestion.js';
7
+ import { Input } from '../Input/Input.js';
8
+
9
+ /**
10
+ * Component that provides an input field for the MultiSuggestion list.
11
+ *
12
+ * Place as a descendant of `MultiSuggestion`
13
+ *
14
+ * @example
15
+ * <MultiSuggestion>
16
+ * <MultiSuggestion.Input />
17
+ * <MultiSuggestion.List />
18
+ * </MultiSuggestion>
19
+ */
20
+ const MultiSuggestionInput = forwardRef(function MultiSuggestionList({ value, onInput, ...rest }, ref) {
21
+ const { listId, inputRef, handleFilter } = useContext(MultiSuggestionContext);
22
+ const mergedRefs = useMergeRefs([inputRef, ref]);
23
+ // Update also if controlled value
24
+ useEffect(() => {
25
+ handleFilter?.(inputRef?.current);
26
+ }, [value]);
27
+ return (jsx(Input, { ref: mergedRefs, list: listId, value: value, onInput: (event) => {
28
+ onInput?.(event); // Should run first
29
+ if (!isDatalistClick(event.nativeEvent))
30
+ handleFilter?.(inputRef?.current);
31
+ }, placeholder: '' // We need an empty placeholder for the clear button to be able to show/hide
32
+ , ...rest }));
33
+ });
34
+
35
+ export { MultiSuggestionInput };
@@ -0,0 +1,28 @@
1
+ 'use client';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { forwardRef, useContext, useEffect } from 'react';
4
+ import '@u-elements/u-datalist';
5
+ import { MultiSuggestionContext } from './MultiSuggestion.js';
6
+
7
+ /**
8
+ * Component that provides a MultiSuggestion list.
9
+ *
10
+ * Place as a descendant of `MultiSuggestion`
11
+ *
12
+ * @example
13
+ * <MultiSuggestion>
14
+ * <MultiSuggestion.Input />
15
+ * <MultiSuggestion.List />
16
+ * </MultiSuggestion>
17
+ */
18
+ const MultiSuggestionList = forwardRef(function MultiSuggestionList({ singular = '%d forslag', plural = '%d forslag', className, id, ...rest }, ref) {
19
+ const { inputRef, listId, setListId, handleFilter } = useContext(MultiSuggestionContext);
20
+ useEffect(() => handleFilter?.(inputRef?.current)); // Must run on every render
21
+ useEffect(() => {
22
+ if (id && listId !== id)
23
+ setListId?.(id);
24
+ }, [listId, id, setListId]);
25
+ return (jsx("u-datalist", { "data-sr-singular": singular, "data-sr-plural": plural, class: className, id: listId, ref: ref, ...rest }));
26
+ });
27
+
28
+ export { MultiSuggestionList };
@@ -0,0 +1,10 @@
1
+ 'use client';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { forwardRef } from 'react';
4
+ import '@u-elements/u-datalist';
5
+
6
+ const MultiSuggestionOption = forwardRef(function MultiSuggestionOption({ className, ...rest }, ref) {
7
+ return (jsx("u-option", { class: className, ref: ref, ...rest }));
8
+ });
9
+
10
+ export { MultiSuggestionOption };
@@ -0,0 +1,26 @@
1
+ 'use client';
2
+ import { MultiSuggestion as MultiSuggestion$1 } from './MultiSuggestion.js';
3
+ import { MultiSuggestionChips } from './MultiSuggestionChips.js';
4
+ import { MultiSuggestionClear } from './MultiSuggestionClear.js';
5
+ import { MultiSuggestionEmpty } from './MultiSuggestionEmpty.js';
6
+ import { MultiSuggestionInput } from './MultiSuggestionInput.js';
7
+ import { MultiSuggestionList } from './MultiSuggestionList.js';
8
+ import { MultiSuggestionOption } from './MultiSuggestionOption.js';
9
+
10
+ const MultiSuggestion = Object.assign(MultiSuggestion$1, {
11
+ Input: MultiSuggestionInput,
12
+ List: MultiSuggestionList,
13
+ Option: MultiSuggestionOption,
14
+ Chips: MultiSuggestionChips,
15
+ Empty: MultiSuggestionEmpty,
16
+ Clear: MultiSuggestionClear,
17
+ });
18
+ MultiSuggestion.displayName = 'EXPERIMENTRAL_MultiSuggestion';
19
+ MultiSuggestion.Input.displayName = 'EXPERIMENTRAL_MultiSuggestion.Input';
20
+ MultiSuggestion.List.displayName = 'EXPERIMENTRAL_MultiSuggestion.List';
21
+ MultiSuggestion.Option.displayName = 'EXPERIMENTRAL_MultiSuggestion.Option';
22
+ MultiSuggestion.Chips.displayName = 'EXPERIMENTRAL_MultiSuggestion.Chips';
23
+ MultiSuggestion.Empty.displayName = 'EXPERIMENTRAL_MultiSuggestion.Empty';
24
+ MultiSuggestion.Clear.displayName = 'EXPERIMENTRAL_MultiSuggestion.Clear';
25
+
26
+ export { MultiSuggestion as EXPERIMENTAL_MultiSuggestion, MultiSuggestionChips as EXPERIMENTAL_MultiSuggestionChips, MultiSuggestionClear as EXPERIMENTAL_MultiSuggestionClear, MultiSuggestionEmpty as EXPERIMENTAL_MultiSuggestionEmpty, MultiSuggestionInput as EXPERIMENTAL_MultiSuggestionInput, MultiSuggestionList as EXPERIMENTAL_MultiSuggestionList, MultiSuggestionOption as EXPERIMENTAL_MultiSuggestionOption };
package/dist/esm/index.js CHANGED
@@ -94,6 +94,13 @@ export { SuggestionInput as EXPERIMENTAL_SuggestionInput } from './components/Su
94
94
  export { SuggestionEmpty as EXPERIMENTAL_SuggestionEmpty } from './components/Suggestion/SuggestionEmpty.js';
95
95
  export { SuggestionOption as EXPERIMENTAL_SuggestionOption } from './components/Suggestion/SuggestionOption.js';
96
96
  export { SuggestionClear as EXPERIMENTAL_SuggestionClear } from './components/Suggestion/SuggestionClear.js';
97
+ export { EXPERIMENTAL_MultiSuggestion } from './components/MultiSuggestion/index.js';
98
+ export { MultiSuggestionInput as EXPERIMENTAL_MultiSuggestionInput } from './components/MultiSuggestion/MultiSuggestionInput.js';
99
+ export { MultiSuggestionList as EXPERIMENTAL_MultiSuggestionList } from './components/MultiSuggestion/MultiSuggestionList.js';
100
+ export { MultiSuggestionOption as EXPERIMENTAL_MultiSuggestionOption } from './components/MultiSuggestion/MultiSuggestionOption.js';
101
+ export { MultiSuggestionChips as EXPERIMENTAL_MultiSuggestionChips } from './components/MultiSuggestion/MultiSuggestionChips.js';
102
+ export { MultiSuggestionEmpty as EXPERIMENTAL_MultiSuggestionEmpty } from './components/MultiSuggestion/MultiSuggestionEmpty.js';
103
+ export { MultiSuggestionClear as EXPERIMENTAL_MultiSuggestionClear } from './components/MultiSuggestion/MultiSuggestionClear.js';
97
104
  export { omit } from './utilities/omit/omit.js';
98
105
  export { useCheckboxGroup } from './utilities/hooks/useCheckboxGroup/useCheckboxGroup.js';
99
106
  export { useDebounceCallback } from './utilities/hooks/useDebounceCallback/useDebounceCallback.js';
@@ -38,4 +38,5 @@ export * from './Combobox';
38
38
  export * from './Table';
39
39
  export * from './ErrorSummary';
40
40
  export * from './Suggestion';
41
+ export * from './MultiSuggestion';
41
42
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@digdir/designsystemet-react",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "React components for Designsystemet",
6
6
  "author": "Designsystemet team",
7
7
  "repository": {