@pareto-engineering/design-system 4.0.0-alpha.73 → 4.0.0-alpha.74
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/dist/cjs/f/fields/SelectInput/SelectInput.js +33 -42
- package/dist/cjs/f/fields/SelectInput/common/Menu/Menu.js +83 -0
- package/dist/cjs/f/fields/SelectInput/common/Menu/index.js +13 -0
- package/dist/cjs/f/fields/SelectInput/common/Multiple/Multiple.js +244 -0
- package/dist/cjs/f/fields/SelectInput/common/Multiple/index.js +13 -0
- package/dist/cjs/f/fields/SelectInput/common/Single/Single.js +104 -0
- package/dist/cjs/f/fields/SelectInput/common/Single/index.js +13 -0
- package/dist/cjs/f/fields/SelectInput/common/index.js +26 -0
- package/dist/cjs/f/fields/SelectInput/styles.scss +149 -45
- package/dist/es/f/fields/SelectInput/SelectInput.js +31 -42
- package/dist/es/f/fields/SelectInput/common/Menu/Menu.js +73 -0
- package/dist/es/f/fields/SelectInput/common/Menu/index.js +2 -0
- package/dist/es/f/fields/SelectInput/common/Multiple/Multiple.js +235 -0
- package/dist/es/f/fields/SelectInput/common/Multiple/index.js +2 -0
- package/dist/es/f/fields/SelectInput/common/Single/Single.js +96 -0
- package/dist/es/f/fields/SelectInput/common/Single/index.js +2 -0
- package/dist/es/f/fields/SelectInput/common/index.js +3 -0
- package/dist/es/f/fields/SelectInput/styles.scss +149 -45
- package/package.json +2 -2
- package/src/stories/f/SelectInput.stories.jsx +15 -0
- package/src/ui/f/fields/SelectInput/SelectInput.jsx +34 -47
- package/src/ui/f/fields/SelectInput/common/Menu/Menu.jsx +103 -0
- package/src/ui/f/fields/SelectInput/common/Menu/index.js +2 -0
- package/src/ui/f/fields/SelectInput/common/Multiple/Multiple.jsx +288 -0
- package/src/ui/f/fields/SelectInput/common/Multiple/index.js +2 -0
- package/src/ui/f/fields/SelectInput/common/Single/Single.jsx +125 -0
- package/src/ui/f/fields/SelectInput/common/Single/index.js +2 -0
- package/src/ui/f/fields/SelectInput/common/index.js +3 -0
- package/src/ui/f/fields/SelectInput/styles.scss +149 -45
- package/tests/__snapshots__/Storyshots.test.js.snap +523 -393
|
@@ -14,72 +14,176 @@ $hover-border: var(--theme-hover-input-border);
|
|
|
14
14
|
$focus-border: var(--theme-focus-input-border);
|
|
15
15
|
$default-background: var(--background-inputs);
|
|
16
16
|
$disabled-background: var(--background-inputs-30);
|
|
17
|
+
$default-gap: var(--gap);
|
|
17
18
|
|
|
18
|
-
.#{bem.$base}.select-input {
|
|
19
|
-
display: flex;
|
|
20
|
-
flex-direction: column;
|
|
21
|
-
|
|
22
|
-
> .#{bem.$base}.form-label {
|
|
23
|
-
margin-bottom: var(--gap);
|
|
24
|
-
}
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
border: $default-border;
|
|
29
|
-
border-radius: $default-input-border-radius;
|
|
20
|
+
.#{bem.$base}.select-input {
|
|
21
|
+
> .#{bem.$base}.single {
|
|
30
22
|
display: flex;
|
|
31
23
|
flex-direction: column;
|
|
32
|
-
padding: $default-padding;
|
|
33
|
-
padding-right: 0;
|
|
34
|
-
position: relative;
|
|
35
24
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
> .#{bem.$base}.form-label {
|
|
26
|
+
margin-bottom: var(--gap);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.select-wrapper {
|
|
30
|
+
background-color: $default-background;
|
|
31
|
+
border: $default-border;
|
|
32
|
+
border-radius: $default-input-border-radius;
|
|
33
|
+
display: flex;
|
|
34
|
+
flex-direction: column;
|
|
35
|
+
padding: $default-padding;
|
|
36
|
+
padding-right: 0;
|
|
37
|
+
position: relative;
|
|
38
|
+
|
|
39
|
+
&:not(.disabled) {
|
|
40
|
+
&:hover,
|
|
41
|
+
&:active {
|
|
42
|
+
border: $hover-border;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
&:focus {
|
|
46
|
+
border: $focus-border;
|
|
47
|
+
}
|
|
40
48
|
}
|
|
41
49
|
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
&.disabled {
|
|
51
|
+
background: $disabled-background;
|
|
44
52
|
}
|
|
45
|
-
}
|
|
46
53
|
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
&::placeholder {
|
|
55
|
+
color: var(--metadata);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
&::after {
|
|
59
|
+
border-radius: $default-input-border-radius;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
>.#{bem.$base}.loading-circle {
|
|
63
|
+
position: absolute;
|
|
64
|
+
right: $default-spacing-size;
|
|
65
|
+
top: 50%;
|
|
66
|
+
transform: translateY(-50%);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
select {
|
|
70
|
+
appearance: none;
|
|
71
|
+
background-color: $default-background;
|
|
72
|
+
background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMTIiIHZpZXdCb3g9IjAgMCAyMCAxMiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTE5IDEuNUwxMCAxMC41TDEgMS41IiBzdHJva2U9IiM0QzRENTMiIHN0cm9rZS13aWR0aD0iMiIvPgo8L3N2Zz4=");
|
|
73
|
+
background-position: calc(100% - $default-spacing-size);
|
|
74
|
+
background-repeat: no-repeat;
|
|
75
|
+
background-size: $default-spacing-size;
|
|
76
|
+
padding-right: $default-spacing-size;
|
|
77
|
+
|
|
78
|
+
&.input {
|
|
79
|
+
border: none;
|
|
80
|
+
color: var(--y);
|
|
81
|
+
outline: none;
|
|
82
|
+
width: 100%;
|
|
83
|
+
|
|
84
|
+
/* stylelint-disable-next-line max-nesting-depth -- required */
|
|
85
|
+
&:disabled {
|
|
86
|
+
opacity: 0%;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
49
90
|
}
|
|
91
|
+
}
|
|
50
92
|
|
|
51
|
-
|
|
52
|
-
|
|
93
|
+
> .#{bem.$base}.multiple {
|
|
94
|
+
display: flex;
|
|
95
|
+
flex-direction: column;
|
|
96
|
+
outline: none;
|
|
97
|
+
position: relative;
|
|
98
|
+
|
|
99
|
+
> .#{bem.$base}.form-label {
|
|
100
|
+
margin-bottom: var(--gap);
|
|
53
101
|
}
|
|
54
102
|
|
|
55
|
-
|
|
103
|
+
|
|
104
|
+
.#{bem.$base}.popover {
|
|
105
|
+
border: $default-border;
|
|
56
106
|
border-radius: $default-input-border-radius;
|
|
57
|
-
|
|
107
|
+
width: 100%;
|
|
108
|
+
|
|
109
|
+
>.menu {
|
|
110
|
+
list-style: none;
|
|
111
|
+
margin: 0;
|
|
112
|
+
outline: 0;
|
|
113
|
+
padding: 0;
|
|
114
|
+
|
|
115
|
+
/* stylelint-disable selector-max-compound-selectors -- required */
|
|
116
|
+
>.item {
|
|
117
|
+
border-radius: $default-input-border-radius;
|
|
118
|
+
padding: $default-padding;
|
|
119
|
+
|
|
120
|
+
/* stylelint-disable max-nesting-depth -- required */
|
|
121
|
+
> p {
|
|
122
|
+
margin: 0;
|
|
123
|
+
}
|
|
58
124
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
125
|
+
&.#{bem.$modifier-active} {
|
|
126
|
+
background-color: var(--y);
|
|
127
|
+
|
|
128
|
+
> p {
|
|
129
|
+
color: var(--on-y);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
64
134
|
}
|
|
65
135
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
&.input {
|
|
76
|
-
border: none;
|
|
77
|
-
color: var(--y);
|
|
136
|
+
|
|
137
|
+
> .input-container {
|
|
138
|
+
position: relative;
|
|
139
|
+
|
|
140
|
+
> .input {
|
|
141
|
+
background: $default-background;
|
|
142
|
+
border: $default-border;
|
|
143
|
+
border-radius: calc(var(--theme-default-border-radius) / 2);
|
|
144
|
+
color: var(--paragraph);
|
|
78
145
|
outline: none;
|
|
146
|
+
padding: $default-padding;
|
|
79
147
|
width: 100%;
|
|
80
148
|
|
|
149
|
+
&::placeholder {
|
|
150
|
+
color: var(--metadata);
|
|
151
|
+
}
|
|
152
|
+
|
|
81
153
|
&:disabled {
|
|
82
|
-
|
|
154
|
+
background-color: $disabled-background;
|
|
155
|
+
color: var(--paragraph);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
&:not(:disabled) {
|
|
159
|
+
&:hover,
|
|
160
|
+
&:active {
|
|
161
|
+
border: $hover-border;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
&:focus {
|
|
165
|
+
border: $focus-border;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
>.selected-items {
|
|
173
|
+
display: flex;
|
|
174
|
+
flex-wrap: wrap;
|
|
175
|
+
gap: calc($default-gap / 2);
|
|
176
|
+
margin-bottom: calc($default-gap / 2);
|
|
177
|
+
|
|
178
|
+
>.item {
|
|
179
|
+
>.#{bem.$base}.button {
|
|
180
|
+
align-items: center;
|
|
181
|
+
display: flex;
|
|
182
|
+
gap: calc($default-gap / 2);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.close {
|
|
186
|
+
font-size: calc(var(--s-3) * 1em);
|
|
83
187
|
}
|
|
84
188
|
}
|
|
85
189
|
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
1
|
/* @pareto-engineering/generator-front 1.0.12 */
|
|
3
2
|
import * as React from 'react';
|
|
4
3
|
import { memo } from 'react';
|
|
5
|
-
import { useField } from 'formik';
|
|
6
4
|
import PropTypes from 'prop-types';
|
|
7
5
|
import styleNames from '@pareto-engineering/bem/exports';
|
|
8
|
-
import { LoadingCircle } from "../../../a";
|
|
9
|
-
import { FormLabel, FormDescription } from "../../common";
|
|
10
6
|
import "./styles.scss";
|
|
7
|
+
import { Single, Multiple } from "./common";
|
|
11
8
|
|
|
12
9
|
// Local Definitions
|
|
13
10
|
|
|
@@ -31,49 +28,33 @@ const SelectInput = ({
|
|
|
31
28
|
description,
|
|
32
29
|
disabled,
|
|
33
30
|
isLoading,
|
|
34
|
-
autoComplete
|
|
31
|
+
autoComplete,
|
|
32
|
+
placeholder,
|
|
33
|
+
getTagColor,
|
|
34
|
+
multiple
|
|
35
35
|
// ...otherProps
|
|
36
36
|
}) => {
|
|
37
|
-
const
|
|
37
|
+
const inputProps = {
|
|
38
38
|
name,
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
label,
|
|
40
|
+
labelColor,
|
|
41
|
+
color,
|
|
42
|
+
options,
|
|
43
|
+
validate,
|
|
44
|
+
optional,
|
|
45
|
+
description,
|
|
46
|
+
disabled,
|
|
47
|
+
isLoading,
|
|
48
|
+
placeholder,
|
|
49
|
+
getTagColor,
|
|
50
|
+
autoComplete
|
|
51
|
+
};
|
|
52
|
+
const Input = multiple ? Multiple : Single;
|
|
41
53
|
return /*#__PURE__*/React.createElement("div", {
|
|
42
54
|
id: id,
|
|
43
55
|
className: [baseClassName, componentClassName, userClassName].filter(e => e).join(' '),
|
|
44
56
|
style: style
|
|
45
|
-
}, /*#__PURE__*/React.createElement(
|
|
46
|
-
name: name,
|
|
47
|
-
color: labelColor,
|
|
48
|
-
optional: optional
|
|
49
|
-
// {...otherProps}
|
|
50
|
-
}, label), /*#__PURE__*/React.createElement("div", {
|
|
51
|
-
className: `select-wrapper${disabled ? ' disabled' : ''}`
|
|
52
|
-
}, /*#__PURE__*/React.createElement("select", _extends({
|
|
53
|
-
className: `input y-${color}`
|
|
54
|
-
}, field, {
|
|
55
|
-
value: field.value || '',
|
|
56
|
-
id: name,
|
|
57
|
-
disabled: disabled,
|
|
58
|
-
autoComplete: autoComplete
|
|
59
|
-
}), options.map(option => {
|
|
60
|
-
// i.e if option is a string like "blah", return { value: "blah", label: "blah" }
|
|
61
|
-
const newOption = typeof option === 'string' ? {
|
|
62
|
-
value: option,
|
|
63
|
-
label: option
|
|
64
|
-
} : option;
|
|
65
|
-
return /*#__PURE__*/React.createElement("option", {
|
|
66
|
-
key: newOption.value,
|
|
67
|
-
value: newOption.value,
|
|
68
|
-
disabled: newOption?.disabled || false
|
|
69
|
-
}, newOption.label);
|
|
70
|
-
})), isLoading && /*#__PURE__*/React.createElement(LoadingCircle, {
|
|
71
|
-
className: "x-main"
|
|
72
|
-
})), /*#__PURE__*/React.createElement(FormDescription, {
|
|
73
|
-
className: "s-1",
|
|
74
|
-
description: description,
|
|
75
|
-
name: name
|
|
76
|
-
}));
|
|
57
|
+
}, /*#__PURE__*/React.createElement(Input, inputProps));
|
|
77
58
|
};
|
|
78
59
|
SelectInput.propTypes = {
|
|
79
60
|
/**
|
|
@@ -135,10 +116,18 @@ SelectInput.propTypes = {
|
|
|
135
116
|
/**
|
|
136
117
|
* Whether the input is optional or not
|
|
137
118
|
*/
|
|
138
|
-
optional: PropTypes.bool
|
|
119
|
+
optional: PropTypes.bool,
|
|
120
|
+
/**
|
|
121
|
+
* Whether the select input should allow multiple selections
|
|
122
|
+
*/
|
|
123
|
+
multiple: PropTypes.bool,
|
|
124
|
+
/**
|
|
125
|
+
* The placeholder of the select input
|
|
126
|
+
*/
|
|
127
|
+
placeholder: PropTypes.string
|
|
139
128
|
};
|
|
140
129
|
SelectInput.defaultProps = {
|
|
141
130
|
disabled: false,
|
|
142
|
-
|
|
131
|
+
multiple: false
|
|
143
132
|
};
|
|
144
133
|
export default /*#__PURE__*/memo(SelectInput);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
/* @pareto-engineering/generator-front 1.0.12 */
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import PropTypes from 'prop-types';
|
|
5
|
+
import styleNames from '@pareto-engineering/bem/exports';
|
|
6
|
+
|
|
7
|
+
// Local Definitions
|
|
8
|
+
|
|
9
|
+
const baseClassName = styleNames.base;
|
|
10
|
+
const componentClassName = 'menu';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This is the component description.
|
|
14
|
+
*/
|
|
15
|
+
const Menu = /*#__PURE__*/React.forwardRef(({
|
|
16
|
+
id,
|
|
17
|
+
className: userClassName,
|
|
18
|
+
style,
|
|
19
|
+
items,
|
|
20
|
+
isOpen,
|
|
21
|
+
highlightedIndex,
|
|
22
|
+
getItemProps,
|
|
23
|
+
...otherProps
|
|
24
|
+
}, ref) => /*#__PURE__*/React.createElement("ul", _extends({
|
|
25
|
+
id: id,
|
|
26
|
+
className: [baseClassName, componentClassName, userClassName].filter(e => e).join(' '),
|
|
27
|
+
style: style,
|
|
28
|
+
ref: ref
|
|
29
|
+
}, otherProps), isOpen && items.map((item, index) => /*#__PURE__*/React.createElement("li", _extends({
|
|
30
|
+
key: item.label
|
|
31
|
+
}, getItemProps({
|
|
32
|
+
item,
|
|
33
|
+
index
|
|
34
|
+
}), {
|
|
35
|
+
className: ['item', highlightedIndex === index && styleNames.modifierActive].filter(Boolean).join(' ')
|
|
36
|
+
}), /*#__PURE__*/React.createElement("p", null, item.label)))));
|
|
37
|
+
Menu.propTypes = {
|
|
38
|
+
/**
|
|
39
|
+
* The HTML id for this element
|
|
40
|
+
*/
|
|
41
|
+
id: PropTypes.string,
|
|
42
|
+
/**
|
|
43
|
+
* The HTML class names for this element
|
|
44
|
+
*/
|
|
45
|
+
className: PropTypes.string,
|
|
46
|
+
/**
|
|
47
|
+
* The React-written, css properties for this element.
|
|
48
|
+
*/
|
|
49
|
+
style: PropTypes.objectOf(PropTypes.string),
|
|
50
|
+
/**
|
|
51
|
+
* The items to be displayed in the menu
|
|
52
|
+
*/
|
|
53
|
+
items: PropTypes.arrayOf(PropTypes.shape({
|
|
54
|
+
value: PropTypes.string,
|
|
55
|
+
label: PropTypes.string
|
|
56
|
+
})),
|
|
57
|
+
/**
|
|
58
|
+
* Whether or not the menu is open
|
|
59
|
+
*/
|
|
60
|
+
isOpen: PropTypes.bool,
|
|
61
|
+
/**
|
|
62
|
+
* The index of the highlighted item
|
|
63
|
+
*/
|
|
64
|
+
highlightedIndex: PropTypes.number,
|
|
65
|
+
/**
|
|
66
|
+
* The function to get the item props
|
|
67
|
+
*/
|
|
68
|
+
getItemProps: PropTypes.func
|
|
69
|
+
};
|
|
70
|
+
Menu.defaultProps = {
|
|
71
|
+
// someProp:false
|
|
72
|
+
};
|
|
73
|
+
export default Menu;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
/* @pareto-engineering/generator-front 1.0.12 */
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { useState, useEffect, useMemo, useRef } from 'react';
|
|
5
|
+
import { useField } from 'formik';
|
|
6
|
+
import PropTypes from 'prop-types';
|
|
7
|
+
import { useCombobox, useMultipleSelection } from 'downshift';
|
|
8
|
+
import styleNames from '@pareto-engineering/bem/exports';
|
|
9
|
+
import { Button } from "../../../../../b";
|
|
10
|
+
import { Popover } from "../../../../../a";
|
|
11
|
+
import { FormDescription, FormLabel } from "../../../..";
|
|
12
|
+
import { Menu } from "../Menu";
|
|
13
|
+
|
|
14
|
+
// Local Definitions
|
|
15
|
+
|
|
16
|
+
const baseClassName = styleNames.base;
|
|
17
|
+
const componentClassName = 'multiple';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {Array[Object]} first - first array to check if it has an item not in the second array.
|
|
21
|
+
* @param {Array[Object]} second - second array to check against the first array.
|
|
22
|
+
*
|
|
23
|
+
* @returns {Boolean} - true if the first array has an item not in the second array.
|
|
24
|
+
*/
|
|
25
|
+
const testIfArraysAreUnique = (first, second) => first.filter(objInFirstArray => !second.some(objInSecondArray => objInFirstArray.value === objInSecondArray.value)).length > 0;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* This is the component description.
|
|
29
|
+
*/
|
|
30
|
+
const Multiple = ({
|
|
31
|
+
id,
|
|
32
|
+
className: userClassName,
|
|
33
|
+
style,
|
|
34
|
+
name,
|
|
35
|
+
label,
|
|
36
|
+
labelColor,
|
|
37
|
+
color,
|
|
38
|
+
options,
|
|
39
|
+
validate,
|
|
40
|
+
optional,
|
|
41
|
+
description,
|
|
42
|
+
disabled,
|
|
43
|
+
autoComplete,
|
|
44
|
+
transformSearch,
|
|
45
|
+
getTagColor,
|
|
46
|
+
placeholder
|
|
47
|
+
// ...otherProps
|
|
48
|
+
}) => {
|
|
49
|
+
const [, meta, helpers] = useField({
|
|
50
|
+
name,
|
|
51
|
+
validate
|
|
52
|
+
});
|
|
53
|
+
const {
|
|
54
|
+
setValue
|
|
55
|
+
} = helpers;
|
|
56
|
+
const {
|
|
57
|
+
value
|
|
58
|
+
} = meta;
|
|
59
|
+
const [searchInputValue, setSearchInputValue] = useState('');
|
|
60
|
+
const {
|
|
61
|
+
getSelectedItemProps,
|
|
62
|
+
getDropdownProps,
|
|
63
|
+
addSelectedItem,
|
|
64
|
+
removeSelectedItem,
|
|
65
|
+
setSelectedItems,
|
|
66
|
+
selectedItems
|
|
67
|
+
} = useMultipleSelection({
|
|
68
|
+
initialSelectedItems: value || []
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @returns {Boolean} - Unique items from the options array so that the combobox
|
|
73
|
+
* shows only the options that are not yet selected.
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
const items = useMemo(() => {
|
|
77
|
+
const lowerCasedInputValue = searchInputValue.toLowerCase();
|
|
78
|
+
return options.filter(option => {
|
|
79
|
+
const lowerCasedOption = option.label.toLowerCase();
|
|
80
|
+
return lowerCasedOption.includes(lowerCasedInputValue) && !selectedItems.includes(option);
|
|
81
|
+
});
|
|
82
|
+
}, [searchInputValue, selectedItems]);
|
|
83
|
+
const {
|
|
84
|
+
isOpen,
|
|
85
|
+
getLabelProps,
|
|
86
|
+
getMenuProps,
|
|
87
|
+
getInputProps,
|
|
88
|
+
getComboboxProps,
|
|
89
|
+
highlightedIndex,
|
|
90
|
+
getItemProps
|
|
91
|
+
} = useCombobox({
|
|
92
|
+
inputValue: searchInputValue,
|
|
93
|
+
defaultHighlightedIndex: 0,
|
|
94
|
+
// after selection, highlight the first item.
|
|
95
|
+
selectedItem: null,
|
|
96
|
+
items,
|
|
97
|
+
circularNavigation: true,
|
|
98
|
+
stateReducer: (state, actionAndChanges) => {
|
|
99
|
+
const {
|
|
100
|
+
changes,
|
|
101
|
+
type
|
|
102
|
+
} = actionAndChanges;
|
|
103
|
+
switch (type) {
|
|
104
|
+
case useCombobox.stateChangeTypes.InputKeyDownEnter:
|
|
105
|
+
case useCombobox.stateChangeTypes.ItemClick:
|
|
106
|
+
return {
|
|
107
|
+
...changes,
|
|
108
|
+
isOpen: true // keep the menu open after selection.
|
|
109
|
+
};
|
|
110
|
+
default:
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
return changes;
|
|
114
|
+
},
|
|
115
|
+
onStateChange: ({
|
|
116
|
+
inputValue: newSearchInputValue,
|
|
117
|
+
type,
|
|
118
|
+
selectedItem
|
|
119
|
+
}) => {
|
|
120
|
+
switch (type) {
|
|
121
|
+
case useCombobox.stateChangeTypes.InputChange:
|
|
122
|
+
{
|
|
123
|
+
const transformedInput = transformSearch(newSearchInputValue);
|
|
124
|
+
setSearchInputValue(transformedInput);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case useCombobox.stateChangeTypes.InputKeyDownEnter:
|
|
128
|
+
case useCombobox.stateChangeTypes.ItemClick:
|
|
129
|
+
case useCombobox.stateChangeTypes.InputBlur:
|
|
130
|
+
if (selectedItem) {
|
|
131
|
+
setSearchInputValue('');
|
|
132
|
+
addSelectedItem(selectedItem);
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
if (selectedItems?.length > 0) {
|
|
142
|
+
setValue(selectedItems);
|
|
143
|
+
}
|
|
144
|
+
}, [selectedItems]);
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (value?.length > 0 && (testIfArraysAreUnique(value, selectedItems) || testIfArraysAreUnique(selectedItems, value))) {
|
|
147
|
+
setSelectedItems(value);
|
|
148
|
+
}
|
|
149
|
+
}, [value]);
|
|
150
|
+
const parentRef = useRef(null);
|
|
151
|
+
return /*#__PURE__*/React.createElement("div", _extends({
|
|
152
|
+
id: id,
|
|
153
|
+
className: [baseClassName, componentClassName, userClassName].filter(e => e).join(' '),
|
|
154
|
+
style: style,
|
|
155
|
+
ref: parentRef
|
|
156
|
+
}, getComboboxProps()), /*#__PURE__*/React.createElement(FormLabel, _extends({
|
|
157
|
+
className: [baseClassName, componentClassName].filter(e => e).join(' ')
|
|
158
|
+
}, getLabelProps(), {
|
|
159
|
+
name: name,
|
|
160
|
+
optional: optional,
|
|
161
|
+
color: labelColor
|
|
162
|
+
}), label), selectedItems?.length > 0 && /*#__PURE__*/React.createElement("div", {
|
|
163
|
+
className: "selected-items"
|
|
164
|
+
}, selectedItems.map((selectedItem, index) => /*#__PURE__*/React.createElement("div", _extends({
|
|
165
|
+
key: selectedItem.label
|
|
166
|
+
}, getSelectedItemProps({
|
|
167
|
+
selectedItem,
|
|
168
|
+
index
|
|
169
|
+
}), {
|
|
170
|
+
className: "item"
|
|
171
|
+
}), /*#__PURE__*/React.createElement(Button, {
|
|
172
|
+
onClick: e => {
|
|
173
|
+
e.stopPropagation();
|
|
174
|
+
removeSelectedItem(selectedItem);
|
|
175
|
+
},
|
|
176
|
+
isCompact: true,
|
|
177
|
+
color: getTagColor ? getTagColor(selectedItem) : color
|
|
178
|
+
}, /*#__PURE__*/React.createElement("span", null, selectedItem.label), /*#__PURE__*/React.createElement("span", {
|
|
179
|
+
className: "icon close"
|
|
180
|
+
}, "Y"))))), /*#__PURE__*/React.createElement("div", {
|
|
181
|
+
className: "input-container"
|
|
182
|
+
}, /*#__PURE__*/React.createElement("input", _extends({}, getInputProps(getDropdownProps({
|
|
183
|
+
preventKeyAction: isOpen
|
|
184
|
+
})), {
|
|
185
|
+
className: "input",
|
|
186
|
+
disabled: disabled,
|
|
187
|
+
placeholder: placeholder,
|
|
188
|
+
autoComplete: autoComplete
|
|
189
|
+
}))), /*#__PURE__*/React.createElement(FormDescription, {
|
|
190
|
+
className: "s-1",
|
|
191
|
+
description: description,
|
|
192
|
+
name: name
|
|
193
|
+
}), /*#__PURE__*/React.createElement(Popover, {
|
|
194
|
+
isOpen: isOpen,
|
|
195
|
+
parentRef: parentRef
|
|
196
|
+
}, /*#__PURE__*/React.createElement(Menu, _extends({
|
|
197
|
+
className: `y-${color}`,
|
|
198
|
+
isOpen: isOpen,
|
|
199
|
+
getItemProps: getItemProps,
|
|
200
|
+
highlightedIndex: highlightedIndex,
|
|
201
|
+
items: items
|
|
202
|
+
}, getMenuProps()))));
|
|
203
|
+
};
|
|
204
|
+
Multiple.propTypes = {
|
|
205
|
+
/**
|
|
206
|
+
* The HTML id for this element
|
|
207
|
+
*/
|
|
208
|
+
id: PropTypes.string,
|
|
209
|
+
/**
|
|
210
|
+
* The HTML class names for this element
|
|
211
|
+
*/
|
|
212
|
+
className: PropTypes.string,
|
|
213
|
+
/**
|
|
214
|
+
* The React-written, css properties for this element.
|
|
215
|
+
*/
|
|
216
|
+
style: PropTypes.objectOf(PropTypes.string),
|
|
217
|
+
/**
|
|
218
|
+
* A function that transforms the search input value
|
|
219
|
+
*/
|
|
220
|
+
transformSearch: PropTypes.func,
|
|
221
|
+
/**
|
|
222
|
+
* The minimum length of the search input value
|
|
223
|
+
*/
|
|
224
|
+
minLength: PropTypes.number,
|
|
225
|
+
/**
|
|
226
|
+
* The base color of the component
|
|
227
|
+
*/
|
|
228
|
+
color: PropTypes.string
|
|
229
|
+
};
|
|
230
|
+
Multiple.defaultProps = {
|
|
231
|
+
transformSearch: e => e,
|
|
232
|
+
minLength: 2,
|
|
233
|
+
color: 'interactive'
|
|
234
|
+
};
|
|
235
|
+
export default Multiple;
|