colibri-ui 0.8.9 → 0.8.11
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/MultiSelect/components/MenuOverlay/index.js +4 -1
- package/MultiSelect/index.js +28 -32
- package/MultiSelect/index.props.d.ts +6 -1
- package/MultiSelect/utils/index.d.ts +3 -1
- package/MultiSelect/utils/index.js +16 -4
- package/esm/MultiSelect/components/MenuOverlay/index.js +5 -2
- package/esm/MultiSelect/index.js +30 -34
- package/esm/MultiSelect/index.props.d.ts +6 -1
- package/esm/MultiSelect/utils/index.d.ts +3 -1
- package/esm/MultiSelect/utils/index.js +15 -4
- package/package.json +1 -1
|
@@ -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.
|
|
95
|
+
return onChange(index$4.toKey({
|
|
96
|
+
group: group.value,
|
|
97
|
+
option: option.value
|
|
98
|
+
}));
|
|
96
99
|
}
|
|
97
100
|
});
|
|
98
101
|
})));
|
package/MultiSelect/index.js
CHANGED
|
@@ -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,39 +45,47 @@ 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
|
|
66
|
-
isOpen =
|
|
67
|
-
setIsOpen =
|
|
68
|
-
var
|
|
69
|
-
value
|
|
70
|
-
|
|
52
|
+
var _e = React.useState(false),
|
|
53
|
+
isOpen = _e[0],
|
|
54
|
+
setIsOpen = _e[1];
|
|
55
|
+
var internalSelectedKeys = React.useMemo(function () {
|
|
56
|
+
return value.map(index.toKey);
|
|
57
|
+
}, [value]);
|
|
71
58
|
var preparedGroups = React.useMemo(function () {
|
|
72
59
|
return groups.map(function (group) {
|
|
73
60
|
return tslib.__assign(tslib.__assign({}, group), {
|
|
74
61
|
options: group.options.map(function (option) {
|
|
75
62
|
return tslib.__assign(tslib.__assign({}, option), {
|
|
76
|
-
selected:
|
|
63
|
+
selected: internalSelectedKeys.includes(index.toKey({
|
|
64
|
+
group: group.value,
|
|
65
|
+
option: option.value
|
|
66
|
+
}))
|
|
77
67
|
});
|
|
78
68
|
})
|
|
79
69
|
});
|
|
80
70
|
});
|
|
81
|
-
}, [
|
|
71
|
+
}, [groups, internalSelectedKeys]);
|
|
82
72
|
var handleChange = React.useCallback(function (key) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return v !== key;
|
|
86
|
-
}) : sortItems(tslib.__spreadArray(tslib.__spreadArray([], prev, true), [key], false));
|
|
87
|
-
onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
88
|
-
return newValue;
|
|
73
|
+
var exists = value.some(function (v) {
|
|
74
|
+
return index.toKey(v) === key;
|
|
89
75
|
});
|
|
76
|
+
var item = index.fromKey(key);
|
|
77
|
+
var newValue = exists ? value.filter(function (v) {
|
|
78
|
+
return index.toKey(v) !== key;
|
|
79
|
+
}) : tslib.__spreadArray(tslib.__spreadArray([], value, true), [item], false);
|
|
80
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
90
81
|
}, [onChange]);
|
|
91
82
|
var preparedLabel = React.useMemo(function () {
|
|
92
83
|
return preparedGroups.flatMap(function (_a) {
|
|
93
84
|
var groupVal = _a.value,
|
|
94
85
|
options = _a.options;
|
|
95
|
-
return options
|
|
86
|
+
return options
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
88
|
+
.filter(function (_a) {
|
|
96
89
|
var selected = _a.selected;
|
|
97
90
|
return selected;
|
|
98
91
|
}).map(
|
|
@@ -102,7 +95,10 @@ var MultiSelect = function MultiSelect(_a) {
|
|
|
102
95
|
optVal = _a.value;
|
|
103
96
|
return {
|
|
104
97
|
label: label,
|
|
105
|
-
key: index.
|
|
98
|
+
key: index.toKey({
|
|
99
|
+
group: groupVal,
|
|
100
|
+
option: optVal
|
|
101
|
+
})
|
|
106
102
|
};
|
|
107
103
|
});
|
|
108
104
|
});
|
|
@@ -162,7 +158,7 @@ var MultiSelect = function MultiSelect(_a) {
|
|
|
162
158
|
type: "hidden",
|
|
163
159
|
name: name,
|
|
164
160
|
tabIndex: -1,
|
|
165
|
-
value:
|
|
161
|
+
value: internalSelectedKeys.join(",") || ""
|
|
166
162
|
}))));
|
|
167
163
|
};
|
|
168
164
|
|
|
@@ -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
|
-
|
|
34
|
+
value?: SelectedItem[];
|
|
35
|
+
onChange?: (value: SelectedItem[]) => void;
|
|
31
36
|
}
|
|
32
37
|
export {};
|
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
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
|
|
4
|
-
|
|
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.
|
|
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 {
|
|
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(
|
|
85
|
+
return onChange(toKey({
|
|
86
|
+
group: group.value,
|
|
87
|
+
option: option.value
|
|
88
|
+
}));
|
|
86
89
|
}
|
|
87
90
|
});
|
|
88
91
|
})));
|
package/esm/MultiSelect/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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 {
|
|
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,39 +35,47 @@ 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
|
|
56
|
-
isOpen =
|
|
57
|
-
setIsOpen =
|
|
58
|
-
var
|
|
59
|
-
value
|
|
60
|
-
|
|
42
|
+
var _e = useState(false),
|
|
43
|
+
isOpen = _e[0],
|
|
44
|
+
setIsOpen = _e[1];
|
|
45
|
+
var internalSelectedKeys = useMemo(function () {
|
|
46
|
+
return value.map(toKey);
|
|
47
|
+
}, [value]);
|
|
61
48
|
var preparedGroups = useMemo(function () {
|
|
62
49
|
return groups.map(function (group) {
|
|
63
50
|
return __assign(__assign({}, group), {
|
|
64
51
|
options: group.options.map(function (option) {
|
|
65
52
|
return __assign(__assign({}, option), {
|
|
66
|
-
selected:
|
|
53
|
+
selected: internalSelectedKeys.includes(toKey({
|
|
54
|
+
group: group.value,
|
|
55
|
+
option: option.value
|
|
56
|
+
}))
|
|
67
57
|
});
|
|
68
58
|
})
|
|
69
59
|
});
|
|
70
60
|
});
|
|
71
|
-
}, [
|
|
61
|
+
}, [groups, internalSelectedKeys]);
|
|
72
62
|
var handleChange = useCallback(function (key) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return v !== key;
|
|
76
|
-
}) : sortItems(__spreadArray(__spreadArray([], prev, true), [key], false));
|
|
77
|
-
onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
78
|
-
return newValue;
|
|
63
|
+
var exists = value.some(function (v) {
|
|
64
|
+
return toKey(v) === key;
|
|
79
65
|
});
|
|
66
|
+
var item = fromKey(key);
|
|
67
|
+
var newValue = exists ? value.filter(function (v) {
|
|
68
|
+
return toKey(v) !== key;
|
|
69
|
+
}) : __spreadArray(__spreadArray([], value, true), [item], false);
|
|
70
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
80
71
|
}, [onChange]);
|
|
81
72
|
var preparedLabel = useMemo(function () {
|
|
82
73
|
return preparedGroups.flatMap(function (_a) {
|
|
83
74
|
var groupVal = _a.value,
|
|
84
75
|
options = _a.options;
|
|
85
|
-
return options
|
|
76
|
+
return options
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
78
|
+
.filter(function (_a) {
|
|
86
79
|
var selected = _a.selected;
|
|
87
80
|
return selected;
|
|
88
81
|
}).map(
|
|
@@ -92,7 +85,10 @@ var MultiSelect = function MultiSelect(_a) {
|
|
|
92
85
|
optVal = _a.value;
|
|
93
86
|
return {
|
|
94
87
|
label: label,
|
|
95
|
-
key:
|
|
88
|
+
key: toKey({
|
|
89
|
+
group: groupVal,
|
|
90
|
+
option: optVal
|
|
91
|
+
})
|
|
96
92
|
};
|
|
97
93
|
});
|
|
98
94
|
});
|
|
@@ -152,7 +148,7 @@ var MultiSelect = function MultiSelect(_a) {
|
|
|
152
148
|
type: "hidden",
|
|
153
149
|
name: name,
|
|
154
150
|
tabIndex: -1,
|
|
155
|
-
value:
|
|
151
|
+
value: internalSelectedKeys.join(",") || ""
|
|
156
152
|
}))));
|
|
157
153
|
};
|
|
158
154
|
|
|
@@ -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
|
-
|
|
34
|
+
value?: SelectedItem[];
|
|
35
|
+
onChange?: (value: SelectedItem[]) => void;
|
|
31
36
|
}
|
|
32
37
|
export {};
|
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
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
|
|
2
|
-
|
|
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 {
|
|
16
|
+
export { fromKey, toKey };
|