@grucloud/bau-ui 0.57.1 → 0.58.0
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/checkbox/checkbox.js +0 -1
- package/multiSelect/index.d.ts +22 -0
- package/multiSelect/index.js +1 -0
- package/multiSelect/multiSelect.js +235 -0
- package/package.json +4 -4
- package/popover/popover.js +4 -5
package/checkbox/checkbox.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare module "@grucloud/bau-ui/multiSelect" {
|
|
2
|
+
type DefaultDesignProps = import("../constants").DefaultDesignProps;
|
|
3
|
+
type ComponentOption = import("../bau-ui").ComponentOption;
|
|
4
|
+
|
|
5
|
+
export type MultiSelectProps = {
|
|
6
|
+
name: string;
|
|
7
|
+
options: object[];
|
|
8
|
+
id?: string;
|
|
9
|
+
label: string;
|
|
10
|
+
defaultOption?: any;
|
|
11
|
+
getOptionLabel: (item: object | string) => HTMLElement | string;
|
|
12
|
+
getOptionValue: (item: object | string) => string;
|
|
13
|
+
Option: (props) => HTMLElement;
|
|
14
|
+
} & DefaultDesignProps;
|
|
15
|
+
|
|
16
|
+
type Component = import("../bau-ui").Component<
|
|
17
|
+
MultiSelectProps,
|
|
18
|
+
HTMLInputElement
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
export default function (context: any, option?: ComponentOption): Component;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./multiSelect";
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import classNames from "@grucloud/bau-css/classNames.js";
|
|
2
|
+
import { toPropsAndChildren } from "@grucloud/bau/bau.js";
|
|
3
|
+
import popover from "../popover/popover.js";
|
|
4
|
+
import button from "../button/button.js";
|
|
5
|
+
import list from "../list/list.js";
|
|
6
|
+
import spinner from "../spinner/spinner.js";
|
|
7
|
+
import checkbox from "../checkbox/checkbox";
|
|
8
|
+
import { Colors } from "../constants.js";
|
|
9
|
+
|
|
10
|
+
const colorsToCss = () =>
|
|
11
|
+
Colors.map(
|
|
12
|
+
(color) =>
|
|
13
|
+
`
|
|
14
|
+
& button.plain.${color}::after {
|
|
15
|
+
color: var(--color-${color});
|
|
16
|
+
}
|
|
17
|
+
& button.outline.${color}::after {
|
|
18
|
+
color: var(--color-${color});
|
|
19
|
+
}
|
|
20
|
+
`
|
|
21
|
+
).join("\n");
|
|
22
|
+
|
|
23
|
+
export default function (context, componentOptions = {}) {
|
|
24
|
+
const { bau, css } = context;
|
|
25
|
+
const { div, li, select } = bau.tags;
|
|
26
|
+
const Button = button(context);
|
|
27
|
+
const Popover = popover(context);
|
|
28
|
+
const List = list(context);
|
|
29
|
+
const Checkbox = checkbox(context, { color: "neutral", variant: "outline" });
|
|
30
|
+
|
|
31
|
+
const className = css`
|
|
32
|
+
& select {
|
|
33
|
+
width: 0;
|
|
34
|
+
height: 0;
|
|
35
|
+
opacity: 0;
|
|
36
|
+
}
|
|
37
|
+
& button {
|
|
38
|
+
&::after {
|
|
39
|
+
content: "\u25BC";
|
|
40
|
+
}
|
|
41
|
+
&.loading::after {
|
|
42
|
+
display: none;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
& label {
|
|
46
|
+
display: flex;
|
|
47
|
+
gap: 0.5rem;
|
|
48
|
+
flex-grow: 1;
|
|
49
|
+
}
|
|
50
|
+
${colorsToCss()}
|
|
51
|
+
`;
|
|
52
|
+
|
|
53
|
+
return function MultiSelect(...args) {
|
|
54
|
+
let [
|
|
55
|
+
{
|
|
56
|
+
size = componentOptions.size ?? "md",
|
|
57
|
+
variant = componentOptions.variant ?? "outline",
|
|
58
|
+
color = componentOptions.color ?? "neutral",
|
|
59
|
+
name,
|
|
60
|
+
label,
|
|
61
|
+
Option,
|
|
62
|
+
options,
|
|
63
|
+
defaultValue = [],
|
|
64
|
+
getOptionLabel,
|
|
65
|
+
getOptionValue,
|
|
66
|
+
renderValue,
|
|
67
|
+
onSelect = () => {},
|
|
68
|
+
loading,
|
|
69
|
+
...props
|
|
70
|
+
},
|
|
71
|
+
...children
|
|
72
|
+
] = toPropsAndChildren(args);
|
|
73
|
+
|
|
74
|
+
const Spinner = spinner(context, { variant, color, size });
|
|
75
|
+
|
|
76
|
+
const selectedState = bau.state(defaultValue);
|
|
77
|
+
const openState = bau.state(false);
|
|
78
|
+
const itemIndexActive = bau.state(0);
|
|
79
|
+
|
|
80
|
+
const dialogOpen = () => {
|
|
81
|
+
popoverEl.openDialog();
|
|
82
|
+
popoverEl.focus();
|
|
83
|
+
openState.val = true;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const dialogClose = () => {
|
|
87
|
+
popoverEl.closeDialog();
|
|
88
|
+
openState.val = false;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const onClose = () => {
|
|
92
|
+
openState.val = false;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const onclickButton = (event) => {
|
|
96
|
+
popoverEl.open ? dialogClose() : dialogOpen();
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const findOptions = () =>
|
|
101
|
+
Array.from(selectEl.selectedOptions).map(({ value }) =>
|
|
102
|
+
options.find((option) => getOptionValue(option) == value)
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const onkeydown = (event) => {
|
|
106
|
+
event.preventDefault();
|
|
107
|
+
switch (event.key) {
|
|
108
|
+
case "Escape":
|
|
109
|
+
dialogClose();
|
|
110
|
+
break;
|
|
111
|
+
case "ArrowDown":
|
|
112
|
+
if (itemIndexActive.val < options.length - 1) {
|
|
113
|
+
itemIndexActive.val++;
|
|
114
|
+
} else {
|
|
115
|
+
itemIndexActive.val = 0;
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
case "ArrowUp":
|
|
119
|
+
if (itemIndexActive.val <= 0) {
|
|
120
|
+
itemIndexActive.val = options.length - 1;
|
|
121
|
+
} else {
|
|
122
|
+
itemIndexActive.val--;
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
case "Enter":
|
|
126
|
+
if (popoverEl.open) {
|
|
127
|
+
const checkbox =
|
|
128
|
+
event.currentTarget.querySelectorAll("input")[
|
|
129
|
+
itemIndexActive.val
|
|
130
|
+
];
|
|
131
|
+
checkbox.checked = !checkbox.checked;
|
|
132
|
+
const currentOption = selectEl.options[itemIndexActive.val + 1];
|
|
133
|
+
currentOption.selected = !currentOption.selected;
|
|
134
|
+
selectedState.val = findOptions();
|
|
135
|
+
} else {
|
|
136
|
+
dialogOpen();
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const onChangeCheckbox = (opt) => (event) => {
|
|
143
|
+
const option = [...selectEl.options].find(
|
|
144
|
+
({ value }) => value == getOptionValue(opt)
|
|
145
|
+
);
|
|
146
|
+
if (event.target.checked) {
|
|
147
|
+
option.selected = true;
|
|
148
|
+
} else {
|
|
149
|
+
option.selected = false;
|
|
150
|
+
}
|
|
151
|
+
selectedState.val = findOptions();
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const Content = () => {
|
|
155
|
+
return List(
|
|
156
|
+
{ tabindex: "0", class: classNames(color, variant) },
|
|
157
|
+
options.map((option, index) =>
|
|
158
|
+
li(
|
|
159
|
+
{
|
|
160
|
+
class: () => classNames(itemIndexActive.val == index && "active"),
|
|
161
|
+
},
|
|
162
|
+
bau.tags.label(
|
|
163
|
+
Checkbox({
|
|
164
|
+
checked: defaultValue.find(
|
|
165
|
+
(v) => getOptionValue(v) == getOptionValue(option)
|
|
166
|
+
),
|
|
167
|
+
name: `${name}-${getOptionValue(option)}`,
|
|
168
|
+
onchange: onChangeCheckbox(option),
|
|
169
|
+
}),
|
|
170
|
+
Option(option)
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const buttonEl = Button(
|
|
178
|
+
{
|
|
179
|
+
type: "button",
|
|
180
|
+
role: "combobox",
|
|
181
|
+
"aria-autocomplete": "list",
|
|
182
|
+
"aria-expanded": openState,
|
|
183
|
+
"aria-label": label,
|
|
184
|
+
onclick: onclickButton,
|
|
185
|
+
color,
|
|
186
|
+
variant,
|
|
187
|
+
size,
|
|
188
|
+
class: loading == true && "loading",
|
|
189
|
+
disabled: loading,
|
|
190
|
+
},
|
|
191
|
+
() => (selectedState.val.length ? renderValue(selectedState.val) : label),
|
|
192
|
+
() => loading == true && Spinner({ visibility: loading })
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const popoverEl = Popover({
|
|
196
|
+
triggerEl: buttonEl,
|
|
197
|
+
contentEl: Content(),
|
|
198
|
+
onClose,
|
|
199
|
+
});
|
|
200
|
+
// Hidden select, required to save the value.
|
|
201
|
+
const selectEl = select(
|
|
202
|
+
{ name, multiple: true, ...props },
|
|
203
|
+
bau.tags.option({ value: "" }, "--Category--"),
|
|
204
|
+
options.map((option) =>
|
|
205
|
+
bau.tags.option(
|
|
206
|
+
{
|
|
207
|
+
value: getOptionValue(option),
|
|
208
|
+
selected: defaultValue.find(
|
|
209
|
+
(v) => getOptionValue(v) == getOptionValue(option)
|
|
210
|
+
),
|
|
211
|
+
},
|
|
212
|
+
getOptionLabel(option)
|
|
213
|
+
)
|
|
214
|
+
)
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
return div(
|
|
218
|
+
{
|
|
219
|
+
...props,
|
|
220
|
+
class: classNames(
|
|
221
|
+
"multiSelect",
|
|
222
|
+
color,
|
|
223
|
+
size,
|
|
224
|
+
className,
|
|
225
|
+
componentOptions?.class,
|
|
226
|
+
props?.class
|
|
227
|
+
),
|
|
228
|
+
onkeydown,
|
|
229
|
+
},
|
|
230
|
+
selectEl,
|
|
231
|
+
buttonEl,
|
|
232
|
+
popoverEl
|
|
233
|
+
);
|
|
234
|
+
};
|
|
235
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grucloud/bau-ui",
|
|
3
3
|
"description": "Components library built with Bau",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.58.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "BSD",
|
|
7
7
|
"keywords": [
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"directory": "bau-ui"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@grucloud/bau": "^0.
|
|
24
|
-
"@grucloud/bau-css": "^0.
|
|
23
|
+
"@grucloud/bau": "^0.58.0",
|
|
24
|
+
"@grucloud/bau-css": "^0.58.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"typedoc": "0.24.8"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "d9e041fc2420252ca2cca75d00e2c61c3ad5f885"
|
|
30
30
|
}
|
package/popover/popover.js
CHANGED
|
@@ -22,9 +22,6 @@ export default function (context, options) {
|
|
|
22
22
|
padding: 0rem;
|
|
23
23
|
margin: 0rem;
|
|
24
24
|
transition: opacity 0.3s ease-in-out;
|
|
25
|
-
&::backdrop {
|
|
26
|
-
background: var(--background-color);
|
|
27
|
-
}
|
|
28
25
|
opacity: 0;
|
|
29
26
|
`;
|
|
30
27
|
|
|
@@ -66,8 +63,10 @@ export default function (context, options) {
|
|
|
66
63
|
{
|
|
67
64
|
role: "presentation",
|
|
68
65
|
class: classNames("popover", className, options?.class, props?.class),
|
|
69
|
-
onclick: (event) =>
|
|
70
|
-
event.
|
|
66
|
+
onclick: (event) => {
|
|
67
|
+
event.stopPropagation();
|
|
68
|
+
event.target === dialogEl && (closeDialog(), onClose?.call());
|
|
69
|
+
},
|
|
71
70
|
},
|
|
72
71
|
Paper(contentEl)
|
|
73
72
|
);
|