colibri-ui 0.8.9 → 0.8.10

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.
@@ -92,7 +92,10 @@ var MenuOverlay = function MenuOverlay(_a) {
92
92
  option: option,
93
93
  key: "item-".concat(optionIndex),
94
94
  onClick: function onClick() {
95
- return onChange(index$4.createGroupOptionString(group.value, option.value));
95
+ return onChange(index$4.toKey({
96
+ group: group.value,
97
+ option: option.value
98
+ }));
96
99
  }
97
100
  });
98
101
  })));
@@ -23,21 +23,6 @@ var classNames__default = /*#__PURE__*/_interopDefault(classNames);
23
23
  var styles__default = /*#__PURE__*/_interopDefault(styles);
24
24
 
25
25
  var clx = classNames__default.default.bind(styles__default.default);
26
- var sortItems = function sortItems(items) {
27
- return tslib.__spreadArray([], items, true).sort(function (a, b) {
28
- var parseKey = function parseKey(key) {
29
- var _a;
30
- return ((_a = key.match(/group-(\d+)-option-(\d+)/)) === null || _a === void 0 ? void 0 : _a.slice(1).map(Number)) || [];
31
- };
32
- var _a = parseKey(a),
33
- groupA = _a[0],
34
- optionA = _a[1];
35
- var _b = parseKey(b),
36
- groupB = _b[0],
37
- optionB = _b[1];
38
- return groupA - groupB || optionA - optionB;
39
- });
40
- };
41
26
  var getValue = function getValue(options) {
42
27
  return options.map(function (_a) {
43
28
  var label = _a.label;
@@ -60,30 +45,42 @@ var MultiSelect = function MultiSelect(_a) {
60
45
  size = _c === void 0 ? "m" : _c,
61
46
  placeholder = _a.placeholder,
62
47
  name = _a.name,
48
+ _d = _a.value,
49
+ value = _d === void 0 ? [] : _d,
63
50
  type = _a.type,
64
51
  onChange = _a.onChange;
65
- var _d = React.useState(false),
66
- isOpen = _d[0],
67
- setIsOpen = _d[1];
68
- var _e = React.useState([]),
69
- value = _e[0],
70
- setValue = _e[1];
52
+ var _e = React.useState(false),
53
+ isOpen = _e[0],
54
+ setIsOpen = _e[1];
55
+ var _f = React.useState(value || []),
56
+ selected = _f[0],
57
+ setSelected = _f[1];
58
+ var internalSelectedKeys = React.useMemo(function () {
59
+ return selected.map(index.toKey);
60
+ }, [selected]);
71
61
  var preparedGroups = React.useMemo(function () {
72
62
  return groups.map(function (group) {
73
63
  return tslib.__assign(tslib.__assign({}, group), {
74
64
  options: group.options.map(function (option) {
75
65
  return tslib.__assign(tslib.__assign({}, option), {
76
- selected: value.includes(index.createGroupOptionString(group.value, option.value))
66
+ selected: internalSelectedKeys.includes(index.toKey({
67
+ group: group.value,
68
+ option: option.value
69
+ }))
77
70
  });
78
71
  })
79
72
  });
80
73
  });
81
- }, [value, groups]);
74
+ }, [groups, internalSelectedKeys]);
82
75
  var handleChange = React.useCallback(function (key) {
83
- setValue(function (prev) {
84
- var newValue = prev.includes(key) ? prev.filter(function (v) {
85
- return v !== key;
86
- }) : sortItems(tslib.__spreadArray(tslib.__spreadArray([], prev, true), [key], false));
76
+ setSelected(function (prev) {
77
+ var exists = prev.some(function (v) {
78
+ return index.toKey(v) === key;
79
+ });
80
+ var item = index.fromKey(key);
81
+ var newValue = exists ? prev.filter(function (v) {
82
+ return index.toKey(v) !== key;
83
+ }) : tslib.__spreadArray(tslib.__spreadArray([], prev, true), [item], false);
87
84
  onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
88
85
  return newValue;
89
86
  });
@@ -92,7 +89,9 @@ var MultiSelect = function MultiSelect(_a) {
92
89
  return preparedGroups.flatMap(function (_a) {
93
90
  var groupVal = _a.value,
94
91
  options = _a.options;
95
- return options.filter(function (_a) {
92
+ return options
93
+ // eslint-disable-next-line @typescript-eslint/no-shadow
94
+ .filter(function (_a) {
96
95
  var selected = _a.selected;
97
96
  return selected;
98
97
  }).map(
@@ -102,7 +101,10 @@ var MultiSelect = function MultiSelect(_a) {
102
101
  optVal = _a.value;
103
102
  return {
104
103
  label: label,
105
- key: index.createGroupOptionString(groupVal, optVal)
104
+ key: index.toKey({
105
+ group: groupVal,
106
+ option: optVal
107
+ })
106
108
  };
107
109
  });
108
110
  });
@@ -162,7 +164,7 @@ var MultiSelect = function MultiSelect(_a) {
162
164
  type: "hidden",
163
165
  name: name,
164
166
  tabIndex: -1,
165
- value: value.join(",") || ""
167
+ value: internalSelectedKeys.join(",") || ""
166
168
  }))));
167
169
  };
168
170
 
@@ -15,6 +15,10 @@ export type GroupProps = {
15
15
  };
16
16
  type Size = "s" | "m" | "l";
17
17
  type Type = "chip";
18
+ export type SelectedItem = {
19
+ group: string;
20
+ option: string;
21
+ };
18
22
  export interface MultiSelectProps extends Omit<FormFieldProps, "children"> {
19
23
  groups: GroupProps[];
20
24
  className?: string;
@@ -27,6 +31,7 @@ export interface MultiSelectProps extends Omit<FormFieldProps, "children"> {
27
31
  placeholder?: string;
28
32
  name?: string;
29
33
  type?: Type;
30
- onChange?: (value: string[]) => void;
34
+ value?: SelectedItem[];
35
+ onChange?: (value: SelectedItem[]) => void;
31
36
  }
32
37
  export {};
@@ -1 +1,3 @@
1
- export declare function createGroupOptionString(groupValue: string, optionValue: string): string;
1
+ import type { SelectedItem } from "../index.props";
2
+ export declare const toKey: ({ group, option }: SelectedItem) => string;
3
+ export declare const fromKey: (key: string) => SelectedItem;
@@ -1,7 +1,19 @@
1
1
  'use strict';
2
2
 
3
- function createGroupOptionString(groupValue, optionValue) {
4
- return "group-".concat(groupValue, "-option-").concat(optionValue);
5
- }
3
+ var toKey = function toKey(_a) {
4
+ var group = _a.group,
5
+ option = _a.option;
6
+ return "group-".concat(group, "-option-").concat(option);
7
+ };
8
+ var fromKey = function fromKey(key) {
9
+ var _a = key.replace(/^group-/, "").split("-option-"),
10
+ group = _a[0],
11
+ option = _a[1];
12
+ return {
13
+ group: group,
14
+ option: option
15
+ };
16
+ };
6
17
 
7
- exports.createGroupOptionString = createGroupOptionString;
18
+ exports.fromKey = fromKey;
19
+ exports.toKey = toKey;
@@ -4,7 +4,7 @@ import Menu from '../../../Menu/index.js';
4
4
  import SelectItem from '../MenuOverlayItem/index.js';
5
5
  import Typography from '../../../Typography/index.js';
6
6
  import Separator from '../../../Separator/index.js';
7
- import { createGroupOptionString } from '../../utils/index.js';
7
+ import { toKey } from '../../utils/index.js';
8
8
  import styles from './index.module.css';
9
9
 
10
10
  var clx = classNames.bind(styles);
@@ -82,7 +82,10 @@ var MenuOverlay = function MenuOverlay(_a) {
82
82
  option: option,
83
83
  key: "item-".concat(optionIndex),
84
84
  onClick: function onClick() {
85
- return onChange(createGroupOptionString(group.value, option.value));
85
+ return onChange(toKey({
86
+ group: group.value,
87
+ option: option.value
88
+ }));
86
89
  }
87
90
  });
88
91
  })));
@@ -1,4 +1,4 @@
1
- import { __spreadArray, __assign } from 'tslib';
1
+ import { __assign, __spreadArray } from 'tslib';
2
2
  import React__default, { useState, useMemo, useCallback } from 'react';
3
3
  import classNames from 'classnames/bind';
4
4
  import Dropdown from '../Dropdown/index.js';
@@ -8,26 +8,11 @@ import '../Icons/index.js';
8
8
  import Chip from '../Chip/index.js';
9
9
  import styles from './index.module.css';
10
10
  import MenuOverlay from './components/MenuOverlay/index.js';
11
- import { createGroupOptionString } from './utils/index.js';
11
+ import { toKey, fromKey } from './utils/index.js';
12
12
  import Chevron from '../Icons/Chevron.js';
13
13
  import CrossFill from '../Icons/CrossFill.js';
14
14
 
15
15
  var clx = classNames.bind(styles);
16
- var sortItems = function sortItems(items) {
17
- return __spreadArray([], items, true).sort(function (a, b) {
18
- var parseKey = function parseKey(key) {
19
- var _a;
20
- return ((_a = key.match(/group-(\d+)-option-(\d+)/)) === null || _a === void 0 ? void 0 : _a.slice(1).map(Number)) || [];
21
- };
22
- var _a = parseKey(a),
23
- groupA = _a[0],
24
- optionA = _a[1];
25
- var _b = parseKey(b),
26
- groupB = _b[0],
27
- optionB = _b[1];
28
- return groupA - groupB || optionA - optionB;
29
- });
30
- };
31
16
  var getValue = function getValue(options) {
32
17
  return options.map(function (_a) {
33
18
  var label = _a.label;
@@ -50,30 +35,42 @@ var MultiSelect = function MultiSelect(_a) {
50
35
  size = _c === void 0 ? "m" : _c,
51
36
  placeholder = _a.placeholder,
52
37
  name = _a.name,
38
+ _d = _a.value,
39
+ value = _d === void 0 ? [] : _d,
53
40
  type = _a.type,
54
41
  onChange = _a.onChange;
55
- var _d = useState(false),
56
- isOpen = _d[0],
57
- setIsOpen = _d[1];
58
- var _e = useState([]),
59
- value = _e[0],
60
- setValue = _e[1];
42
+ var _e = useState(false),
43
+ isOpen = _e[0],
44
+ setIsOpen = _e[1];
45
+ var _f = useState(value || []),
46
+ selected = _f[0],
47
+ setSelected = _f[1];
48
+ var internalSelectedKeys = useMemo(function () {
49
+ return selected.map(toKey);
50
+ }, [selected]);
61
51
  var preparedGroups = useMemo(function () {
62
52
  return groups.map(function (group) {
63
53
  return __assign(__assign({}, group), {
64
54
  options: group.options.map(function (option) {
65
55
  return __assign(__assign({}, option), {
66
- selected: value.includes(createGroupOptionString(group.value, option.value))
56
+ selected: internalSelectedKeys.includes(toKey({
57
+ group: group.value,
58
+ option: option.value
59
+ }))
67
60
  });
68
61
  })
69
62
  });
70
63
  });
71
- }, [value, groups]);
64
+ }, [groups, internalSelectedKeys]);
72
65
  var handleChange = useCallback(function (key) {
73
- setValue(function (prev) {
74
- var newValue = prev.includes(key) ? prev.filter(function (v) {
75
- return v !== key;
76
- }) : sortItems(__spreadArray(__spreadArray([], prev, true), [key], false));
66
+ setSelected(function (prev) {
67
+ var exists = prev.some(function (v) {
68
+ return toKey(v) === key;
69
+ });
70
+ var item = fromKey(key);
71
+ var newValue = exists ? prev.filter(function (v) {
72
+ return toKey(v) !== key;
73
+ }) : __spreadArray(__spreadArray([], prev, true), [item], false);
77
74
  onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
78
75
  return newValue;
79
76
  });
@@ -82,7 +79,9 @@ var MultiSelect = function MultiSelect(_a) {
82
79
  return preparedGroups.flatMap(function (_a) {
83
80
  var groupVal = _a.value,
84
81
  options = _a.options;
85
- return options.filter(function (_a) {
82
+ return options
83
+ // eslint-disable-next-line @typescript-eslint/no-shadow
84
+ .filter(function (_a) {
86
85
  var selected = _a.selected;
87
86
  return selected;
88
87
  }).map(
@@ -92,7 +91,10 @@ var MultiSelect = function MultiSelect(_a) {
92
91
  optVal = _a.value;
93
92
  return {
94
93
  label: label,
95
- key: createGroupOptionString(groupVal, optVal)
94
+ key: toKey({
95
+ group: groupVal,
96
+ option: optVal
97
+ })
96
98
  };
97
99
  });
98
100
  });
@@ -152,7 +154,7 @@ var MultiSelect = function MultiSelect(_a) {
152
154
  type: "hidden",
153
155
  name: name,
154
156
  tabIndex: -1,
155
- value: value.join(",") || ""
157
+ value: internalSelectedKeys.join(",") || ""
156
158
  }))));
157
159
  };
158
160
 
@@ -15,6 +15,10 @@ export type GroupProps = {
15
15
  };
16
16
  type Size = "s" | "m" | "l";
17
17
  type Type = "chip";
18
+ export type SelectedItem = {
19
+ group: string;
20
+ option: string;
21
+ };
18
22
  export interface MultiSelectProps extends Omit<FormFieldProps, "children"> {
19
23
  groups: GroupProps[];
20
24
  className?: string;
@@ -27,6 +31,7 @@ export interface MultiSelectProps extends Omit<FormFieldProps, "children"> {
27
31
  placeholder?: string;
28
32
  name?: string;
29
33
  type?: Type;
30
- onChange?: (value: string[]) => void;
34
+ value?: SelectedItem[];
35
+ onChange?: (value: SelectedItem[]) => void;
31
36
  }
32
37
  export {};
@@ -1 +1,3 @@
1
- export declare function createGroupOptionString(groupValue: string, optionValue: string): string;
1
+ import type { SelectedItem } from "../index.props";
2
+ export declare const toKey: ({ group, option }: SelectedItem) => string;
3
+ export declare const fromKey: (key: string) => SelectedItem;
@@ -1,5 +1,16 @@
1
- function createGroupOptionString(groupValue, optionValue) {
2
- return "group-".concat(groupValue, "-option-").concat(optionValue);
3
- }
1
+ var toKey = function toKey(_a) {
2
+ var group = _a.group,
3
+ option = _a.option;
4
+ return "group-".concat(group, "-option-").concat(option);
5
+ };
6
+ var fromKey = function fromKey(key) {
7
+ var _a = key.replace(/^group-/, "").split("-option-"),
8
+ group = _a[0],
9
+ option = _a[1];
10
+ return {
11
+ group: group,
12
+ option: option
13
+ };
14
+ };
4
15
 
5
- export { createGroupOptionString };
16
+ export { fromKey, toKey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "colibri-ui",
3
- "version": "0.8.9",
3
+ "version": "0.8.10",
4
4
  "description": "React UI components library",
5
5
  "main": "index.js",
6
6
  "scripts": {},