@scm-manager/ui-forms 2.44.2 → 2.44.3-20230527-064447
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/.turbo/turbo-build.log +7 -7
- package/build/index.d.ts +106 -45
- package/build/index.js +525 -255
- package/build/index.mjs +396 -122
- package/package.json +7 -6
- package/src/Form.stories.tsx +22 -0
- package/src/chip-input/ChipInputField.stories.tsx +41 -12
- package/src/chip-input/ChipInputField.tsx +83 -56
- package/src/chip-input/ControlledChipInputField.tsx +12 -4
- package/src/combobox/Combobox.stories.tsx +96 -0
- package/src/combobox/Combobox.tsx +217 -0
- package/src/combobox/ComboboxField.tsx +62 -0
- package/src/combobox/ControlledComboboxField.tsx +96 -0
- package/src/headless-chip-input/ChipInput.tsx +127 -59
- package/src/helpers.ts +23 -0
- package/src/index.ts +4 -0
package/src/Form.stories.tsx
CHANGED
|
@@ -37,6 +37,9 @@ import AddListEntryForm from "./AddListEntryForm";
|
|
|
37
37
|
import { ScmFormListContextProvider } from "./ScmFormListContext";
|
|
38
38
|
import { HalRepresentation } from "@scm-manager/ui-types";
|
|
39
39
|
import ControlledChipInputField from "./chip-input/ControlledChipInputField";
|
|
40
|
+
import Combobox from "./combobox/Combobox";
|
|
41
|
+
import ControlledComboboxField from "./combobox/ControlledComboboxField";
|
|
42
|
+
import { defaultOptionFactory } from "./helpers";
|
|
40
43
|
|
|
41
44
|
export type SimpleWebHookConfiguration = {
|
|
42
45
|
urlPattern: string;
|
|
@@ -241,6 +244,8 @@ storiesOf("Forms", module)
|
|
|
241
244
|
disableB: false,
|
|
242
245
|
disableC: true,
|
|
243
246
|
labels: ["test"],
|
|
247
|
+
people: [{ displayName: "Trillian", id: 2 }],
|
|
248
|
+
author: null,
|
|
244
249
|
}}
|
|
245
250
|
>
|
|
246
251
|
<ControlledInputField name="url" />
|
|
@@ -250,6 +255,23 @@ storiesOf("Forms", module)
|
|
|
250
255
|
<ControlledCheckboxField name="disableB" />
|
|
251
256
|
<ControlledCheckboxField name="disableC" />
|
|
252
257
|
<ControlledChipInputField name="labels" />
|
|
258
|
+
<ControlledChipInputField
|
|
259
|
+
name="people"
|
|
260
|
+
optionFactory={(person) => ({ label: person.displayName, value: person })}
|
|
261
|
+
>
|
|
262
|
+
<Combobox
|
|
263
|
+
options={[
|
|
264
|
+
{ label: "Zenod", value: { id: 1, displayName: "Zenod" } },
|
|
265
|
+
{ label: "Arthur", value: { id: 3, displayName: "Arthur" } },
|
|
266
|
+
{ label: "Cookie Monster", value: { id: 4, displayName: "Cookie Monster" } },
|
|
267
|
+
]}
|
|
268
|
+
/>
|
|
269
|
+
</ControlledChipInputField>
|
|
270
|
+
<ControlledComboboxField
|
|
271
|
+
options={["Zenod", "Arthur", "Trillian"].map(defaultOptionFactory)}
|
|
272
|
+
name="author"
|
|
273
|
+
nullable
|
|
274
|
+
/>
|
|
253
275
|
</Form>
|
|
254
276
|
))
|
|
255
277
|
.add("ReadOnly", () => (
|
|
@@ -25,16 +25,45 @@
|
|
|
25
25
|
import { storiesOf } from "@storybook/react";
|
|
26
26
|
import React, { useState } from "react";
|
|
27
27
|
import ChipInputField from "./ChipInputField";
|
|
28
|
+
import Combobox from "../combobox/Combobox";
|
|
29
|
+
import { Option } from "@scm-manager/ui-types";
|
|
28
30
|
|
|
29
|
-
storiesOf("Chip Input Field", module)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
storiesOf("Chip Input Field", module)
|
|
32
|
+
.add("Default", () => {
|
|
33
|
+
const [value, setValue] = useState<Option<string>[]>([]);
|
|
34
|
+
return (
|
|
35
|
+
<ChipInputField
|
|
36
|
+
value={value}
|
|
37
|
+
onChange={setValue}
|
|
38
|
+
label="Test Chips"
|
|
39
|
+
placeholder="Type a new chip name and press enter to add"
|
|
40
|
+
aria-label="My personal chip list"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
})
|
|
44
|
+
.add("With Autocomplete", () => {
|
|
45
|
+
const people = ["Durward Reynolds", "Kenton Towne", "Therese Wunsch", "Benedict Kessler", "Katelyn Rohan"];
|
|
46
|
+
|
|
47
|
+
const [value, setValue] = useState<Option<string>[]>([]);
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<ChipInputField
|
|
51
|
+
value={value}
|
|
52
|
+
onChange={setValue}
|
|
53
|
+
label="Persons"
|
|
54
|
+
placeholder="Enter a new person"
|
|
55
|
+
aria-label="Enter a new person"
|
|
56
|
+
>
|
|
57
|
+
<Combobox
|
|
58
|
+
options={(query: string) =>
|
|
59
|
+
Promise.resolve(
|
|
60
|
+
people
|
|
61
|
+
.map<Option<string>>((p) => ({ label: p, value: p }))
|
|
62
|
+
.filter((t) => !value.some((val) => val.label === t.label) && t.label.startsWith(query))
|
|
63
|
+
.concat({ label: query, value: query, displayValue: `Use '${query}'` })
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
/>
|
|
67
|
+
</ChipInputField>
|
|
68
|
+
);
|
|
69
|
+
});
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import React, {
|
|
25
|
+
import React, { KeyboardEventHandler, ReactElement, useCallback } from "react";
|
|
26
26
|
import { createAttributesForTesting, useGeneratedId } from "@scm-manager/ui-components";
|
|
27
27
|
import Field from "../base/Field";
|
|
28
28
|
import Label from "../base/label/Label";
|
|
@@ -34,8 +34,10 @@ import { createVariantClass } from "../variants";
|
|
|
34
34
|
import ChipInput, { NewChipInput } from "../headless-chip-input/ChipInput";
|
|
35
35
|
import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
|
|
36
36
|
import { useTranslation } from "react-i18next";
|
|
37
|
+
import { withForwardRef } from "../helpers";
|
|
38
|
+
import { Option } from "@scm-manager/ui-types";
|
|
37
39
|
|
|
38
|
-
const StyledChipInput = styled(ChipInput)`
|
|
40
|
+
const StyledChipInput: typeof ChipInput = styled(ChipInput)`
|
|
39
41
|
min-height: 40px;
|
|
40
42
|
height: min-content;
|
|
41
43
|
gap: 0.5rem;
|
|
@@ -48,79 +50,102 @@ const StyledChipInput = styled(ChipInput)`
|
|
|
48
50
|
const StyledInput = styled(NewChipInput)`
|
|
49
51
|
color: var(--scm-secondary-more-color);
|
|
50
52
|
font-size: 1rem;
|
|
53
|
+
height: initial;
|
|
54
|
+
padding: 0;
|
|
55
|
+
border-radius: 0;
|
|
51
56
|
&:focus {
|
|
52
57
|
outline: none;
|
|
53
58
|
}
|
|
59
|
+
` as unknown as typeof NewChipInput;
|
|
60
|
+
|
|
61
|
+
const StyledDelete = styled(ChipInput.Chip.Delete)`
|
|
62
|
+
&:focus {
|
|
63
|
+
outline-offset: 0;
|
|
64
|
+
}
|
|
54
65
|
`;
|
|
55
66
|
|
|
56
|
-
type InputFieldProps = {
|
|
67
|
+
type InputFieldProps<T> = {
|
|
57
68
|
label: string;
|
|
58
69
|
createDeleteText?: (value: string) => string;
|
|
59
70
|
helpText?: string;
|
|
60
71
|
error?: string;
|
|
61
72
|
testId?: string;
|
|
62
73
|
id?: string;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
children?: ReactElement;
|
|
75
|
+
placeholder?: string;
|
|
76
|
+
onChange?: (newValue: Option<T>[]) => void;
|
|
77
|
+
value?: Option<T>[] | null;
|
|
78
|
+
onKeyDown?: KeyboardEventHandler<HTMLInputElement>;
|
|
79
|
+
readOnly?: boolean;
|
|
80
|
+
disabled?: boolean;
|
|
81
|
+
className?: string;
|
|
82
|
+
isLoading?: boolean;
|
|
83
|
+
isNewItemDuplicate?: (existingItem: Option<T>, newItem: Option<T>) => boolean;
|
|
84
|
+
};
|
|
66
85
|
|
|
67
86
|
/**
|
|
68
87
|
* @beta
|
|
69
88
|
* @since 2.44.0
|
|
70
89
|
*/
|
|
71
|
-
const ChipInputField =
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
90
|
+
const ChipInputField = function ChipInputField<T>(
|
|
91
|
+
{
|
|
92
|
+
label,
|
|
93
|
+
helpText,
|
|
94
|
+
readOnly,
|
|
95
|
+
disabled,
|
|
96
|
+
error,
|
|
97
|
+
createDeleteText,
|
|
98
|
+
onChange,
|
|
99
|
+
placeholder,
|
|
100
|
+
value,
|
|
101
|
+
className,
|
|
102
|
+
testId,
|
|
103
|
+
id,
|
|
104
|
+
children,
|
|
105
|
+
isLoading,
|
|
106
|
+
isNewItemDuplicate,
|
|
107
|
+
...props
|
|
108
|
+
}: InputFieldProps<T>,
|
|
109
|
+
ref: React.ForwardedRef<HTMLInputElement>
|
|
110
|
+
) {
|
|
111
|
+
const [t] = useTranslation("commons", { keyPrefix: "form.chipList" });
|
|
112
|
+
const deleteTextCallback = useCallback(
|
|
113
|
+
(item) => (createDeleteText ? createDeleteText(item) : t("delete", { item })),
|
|
114
|
+
[createDeleteText, t]
|
|
115
|
+
);
|
|
116
|
+
const inputId = useGeneratedId(id ?? testId);
|
|
117
|
+
const labelId = useGeneratedId();
|
|
118
|
+
const inputDescriptionId = useGeneratedId();
|
|
119
|
+
const variant = error ? "danger" : undefined;
|
|
120
|
+
return (
|
|
121
|
+
<Field className={className} aria-owns={inputId}>
|
|
122
|
+
<Label id={labelId}>
|
|
123
|
+
{label}
|
|
124
|
+
{helpText ? <Help className="ml-1" text={helpText} /> : null}
|
|
125
|
+
</Label>
|
|
126
|
+
<div className={classNames("control", { "is-loading": isLoading })}>
|
|
105
127
|
<StyledChipInput
|
|
106
128
|
value={value}
|
|
107
|
-
onChange={onChange}
|
|
129
|
+
onChange={(e) => onChange && onChange(e ?? [])}
|
|
108
130
|
className="is-flex is-flex-wrap-wrap input"
|
|
109
131
|
aria-labelledby={labelId}
|
|
110
132
|
disabled={readOnly || disabled}
|
|
133
|
+
isNewItemDuplicate={isNewItemDuplicate}
|
|
111
134
|
>
|
|
112
|
-
{value?.map((
|
|
113
|
-
<ChipInput.Chip key={
|
|
114
|
-
{
|
|
115
|
-
<
|
|
135
|
+
{value?.map((option, index) => (
|
|
136
|
+
<ChipInput.Chip key={option.label} className="tag is-light is-overflow-hidden">
|
|
137
|
+
<span className="is-ellipsis-overflow">{option.label}</span>
|
|
138
|
+
<StyledDelete aria-label={deleteTextCallback(option.label)} index={index} className="delete is-small" />
|
|
116
139
|
</ChipInput.Chip>
|
|
117
140
|
))}
|
|
118
141
|
<StyledInput
|
|
119
142
|
{...props}
|
|
120
143
|
className={classNames(
|
|
121
144
|
"is-borderless",
|
|
122
|
-
"is-flex-grow-1",
|
|
123
145
|
"has-background-transparent",
|
|
146
|
+
"is-shadowless",
|
|
147
|
+
"input",
|
|
148
|
+
"is-ellipsis-overflow",
|
|
124
149
|
createVariantClass(variant)
|
|
125
150
|
)}
|
|
126
151
|
placeholder={!readOnly && !disabled ? placeholder : ""}
|
|
@@ -128,14 +153,16 @@ const ChipInputField = React.forwardRef<HTMLInputElement, InputFieldProps>(
|
|
|
128
153
|
ref={ref}
|
|
129
154
|
aria-describedby={inputDescriptionId}
|
|
130
155
|
{...createAttributesForTesting(testId)}
|
|
131
|
-
|
|
156
|
+
>
|
|
157
|
+
{children ? children : null}
|
|
158
|
+
</StyledInput>
|
|
132
159
|
</StyledChipInput>
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
</
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
export default ChipInputField;
|
|
160
|
+
</div>
|
|
161
|
+
<VisuallyHidden aria-hidden id={inputDescriptionId}>
|
|
162
|
+
{t("input.description")}
|
|
163
|
+
</VisuallyHidden>
|
|
164
|
+
{error ? <FieldMessage variant={variant}>{error}</FieldMessage> : null}
|
|
165
|
+
</Field>
|
|
166
|
+
);
|
|
167
|
+
};
|
|
168
|
+
export default withForwardRef(ChipInputField);
|
|
@@ -26,12 +26,13 @@ import React, { ComponentProps } from "react";
|
|
|
26
26
|
import { Controller, ControllerRenderProps, Path } from "react-hook-form";
|
|
27
27
|
import { useScmFormContext } from "../ScmFormContext";
|
|
28
28
|
import { useScmFormPathContext } from "../FormPathContext";
|
|
29
|
-
import { prefixWithoutIndices } from "../helpers";
|
|
29
|
+
import { defaultOptionFactory, prefixWithoutIndices } from "../helpers";
|
|
30
30
|
import classNames from "classnames";
|
|
31
31
|
import ChipInputField from "./ChipInputField";
|
|
32
|
+
import { Option } from "@scm-manager/ui-types";
|
|
32
33
|
|
|
33
34
|
type Props<T extends Record<string, unknown>> = Omit<
|
|
34
|
-
|
|
35
|
+
Parameters<typeof ChipInputField>[0],
|
|
35
36
|
"error" | "createDeleteText" | "label" | "defaultChecked" | "required" | keyof ControllerRenderProps
|
|
36
37
|
> & {
|
|
37
38
|
rules?: ComponentProps<typeof Controller>["rules"];
|
|
@@ -39,6 +40,7 @@ type Props<T extends Record<string, unknown>> = Omit<
|
|
|
39
40
|
label?: string;
|
|
40
41
|
defaultValue?: string[];
|
|
41
42
|
createDeleteText?: (value: string) => string;
|
|
43
|
+
optionFactory?: (val: any) => Option<unknown>;
|
|
42
44
|
};
|
|
43
45
|
|
|
44
46
|
/**
|
|
@@ -56,6 +58,8 @@ function ControlledChipInputField<T extends Record<string, unknown>>({
|
|
|
56
58
|
placeholder,
|
|
57
59
|
className,
|
|
58
60
|
createDeleteText,
|
|
61
|
+
children,
|
|
62
|
+
optionFactory = defaultOptionFactory,
|
|
59
63
|
...props
|
|
60
64
|
}: Props<T>) {
|
|
61
65
|
const { control, t, readOnly: formReadonly } = useScmFormContext();
|
|
@@ -73,12 +77,14 @@ function ControlledChipInputField<T extends Record<string, unknown>>({
|
|
|
73
77
|
name={nameWithPrefix}
|
|
74
78
|
rules={rules}
|
|
75
79
|
defaultValue={defaultValue}
|
|
76
|
-
render={({ field, fieldState }) => (
|
|
80
|
+
render={({ field: { value, onChange, ...field }, fieldState }) => (
|
|
77
81
|
<ChipInputField
|
|
78
82
|
label={labelTranslation}
|
|
79
83
|
helpText={helpTextTranslation}
|
|
80
84
|
placeholder={placeholderTranslation}
|
|
81
85
|
aria-label={ariaLabelTranslation}
|
|
86
|
+
value={value ? value.map(optionFactory) : []}
|
|
87
|
+
onChange={(selectedOptions) => onChange(selectedOptions.map((item) => item.value))}
|
|
82
88
|
{...props}
|
|
83
89
|
{...field}
|
|
84
90
|
readOnly={readOnly ?? formReadonly}
|
|
@@ -89,7 +95,9 @@ function ControlledChipInputField<T extends Record<string, unknown>>({
|
|
|
89
95
|
: undefined
|
|
90
96
|
}
|
|
91
97
|
testId={testId ?? `input-${nameWithPrefix}`}
|
|
92
|
-
|
|
98
|
+
>
|
|
99
|
+
{children}
|
|
100
|
+
</ChipInputField>
|
|
93
101
|
)}
|
|
94
102
|
/>
|
|
95
103
|
);
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { storiesOf } from "@storybook/react";
|
|
26
|
+
import React, { Fragment, useState } from "react";
|
|
27
|
+
import Combobox from "./Combobox";
|
|
28
|
+
import { Combobox as HeadlessCombobox } from "@headlessui/react";
|
|
29
|
+
import { Option } from "@scm-manager/ui-types";
|
|
30
|
+
|
|
31
|
+
const waitFor = (ms: number) =>
|
|
32
|
+
function <T>(result: T) {
|
|
33
|
+
return new Promise<T>((resolve) => setTimeout(() => resolve(result), ms));
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const data = [
|
|
37
|
+
{ label: "Trillian", value: "1" },
|
|
38
|
+
{ label: "Arthur", value: "2" },
|
|
39
|
+
{ label: "Zaphod", value: "3" },
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
storiesOf("Combobox", module)
|
|
43
|
+
.add("Options array", () => {
|
|
44
|
+
const [value, setValue] = useState<Option<string>>();
|
|
45
|
+
return <Combobox options={data} value={value} onChange={setValue} />;
|
|
46
|
+
})
|
|
47
|
+
.add("Options function", () => {
|
|
48
|
+
const [value, setValue] = useState<Option<string>>();
|
|
49
|
+
return <Combobox options={() => data} value={value} onChange={setValue} />;
|
|
50
|
+
})
|
|
51
|
+
.add("Options function as promise", () => {
|
|
52
|
+
const [value, setValue] = useState<Option<string>>();
|
|
53
|
+
return (
|
|
54
|
+
<Combobox
|
|
55
|
+
value={value}
|
|
56
|
+
onChange={setValue}
|
|
57
|
+
options={(query) => Promise.resolve(data.filter((t) => t.label.startsWith(query))).then(waitFor(1000))}
|
|
58
|
+
/>
|
|
59
|
+
);
|
|
60
|
+
})
|
|
61
|
+
.add("Children as Element", () => {
|
|
62
|
+
const [value, setValue] = useState<Option<string>>();
|
|
63
|
+
const [query, setQuery] = useState("");
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Combobox value={value} onChange={setValue} onQueryChange={setQuery}>
|
|
67
|
+
{query ? (
|
|
68
|
+
<HeadlessCombobox.Option value={{ label: query, value: query }} key={query} as={Fragment}>
|
|
69
|
+
{({ active }) => <Combobox.Option isActive={active}>{`Create ${query}`}</Combobox.Option>}
|
|
70
|
+
</HeadlessCombobox.Option>
|
|
71
|
+
) : null}
|
|
72
|
+
<HeadlessCombobox.Option value={{ label: "All", value: "All" }} key="all" as={Fragment}>
|
|
73
|
+
{({ active }) => <Combobox.Option isActive={active}>All</Combobox.Option>}
|
|
74
|
+
</HeadlessCombobox.Option>
|
|
75
|
+
<>
|
|
76
|
+
{data.map((o) => (
|
|
77
|
+
<HeadlessCombobox.Option value={o} key={o.value} as={Fragment}>
|
|
78
|
+
{({ active }) => <Combobox.Option isActive={active}>{o.label}</Combobox.Option>}
|
|
79
|
+
</HeadlessCombobox.Option>
|
|
80
|
+
))}
|
|
81
|
+
</>
|
|
82
|
+
</Combobox>
|
|
83
|
+
);
|
|
84
|
+
})
|
|
85
|
+
.add("Children as render props", () => {
|
|
86
|
+
const [value, setValue] = useState<Option<string>>();
|
|
87
|
+
return (
|
|
88
|
+
<Combobox options={data} value={value} onChange={setValue}>
|
|
89
|
+
{(o) => (
|
|
90
|
+
<HeadlessCombobox.Option value={o} key={o.value} as={Fragment}>
|
|
91
|
+
{({ active }) => <Combobox.Option isActive={active}>{o.label}</Combobox.Option>}
|
|
92
|
+
</HeadlessCombobox.Option>
|
|
93
|
+
)}
|
|
94
|
+
</Combobox>
|
|
95
|
+
);
|
|
96
|
+
});
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import React, {
|
|
26
|
+
ForwardedRef,
|
|
27
|
+
Fragment,
|
|
28
|
+
KeyboardEvent,
|
|
29
|
+
KeyboardEventHandler,
|
|
30
|
+
ReactElement,
|
|
31
|
+
Ref,
|
|
32
|
+
useEffect,
|
|
33
|
+
useRef,
|
|
34
|
+
useState,
|
|
35
|
+
} from "react";
|
|
36
|
+
import { Combobox as HeadlessCombobox } from "@headlessui/react";
|
|
37
|
+
import classNames from "classnames";
|
|
38
|
+
import styled from "styled-components";
|
|
39
|
+
import { withForwardRef } from "../helpers";
|
|
40
|
+
import { createAttributesForTesting } from "@scm-manager/ui-components";
|
|
41
|
+
import { Option } from "@scm-manager/ui-types";
|
|
42
|
+
|
|
43
|
+
const OptionsWrapper = styled(HeadlessCombobox.Options).attrs({
|
|
44
|
+
className: "is-flex is-flex-direction-column has-rounded-border has-box-shadow is-overflow-hidden",
|
|
45
|
+
})`
|
|
46
|
+
z-index: 400;
|
|
47
|
+
top: 2.5rem;
|
|
48
|
+
border: var(--scm-border);
|
|
49
|
+
background-color: var(--scm-secondary-background);
|
|
50
|
+
max-width: 35ch;
|
|
51
|
+
|
|
52
|
+
&:empty {
|
|
53
|
+
border: 0;
|
|
54
|
+
clip: rect(0 0 0 0);
|
|
55
|
+
height: 1px;
|
|
56
|
+
margin: -1px;
|
|
57
|
+
overflow: hidden;
|
|
58
|
+
padding: 0;
|
|
59
|
+
position: absolute;
|
|
60
|
+
white-space: nowrap;
|
|
61
|
+
width: 1px;
|
|
62
|
+
}
|
|
63
|
+
`;
|
|
64
|
+
|
|
65
|
+
const StyledComboboxOption = styled.li.attrs({
|
|
66
|
+
className: "px-3 py-2 has-text-inherit is-clickable is-size-6 is-borderless has-background-transparent",
|
|
67
|
+
})<{ isActive: boolean }>`
|
|
68
|
+
line-height: inherit;
|
|
69
|
+
background-color: ${({ isActive }) => (isActive ? "var(--scm-column-selection)" : "")};
|
|
70
|
+
word-break: break-all;
|
|
71
|
+
&[data-disabled] {
|
|
72
|
+
color: unset !important;
|
|
73
|
+
opacity: 40%;
|
|
74
|
+
cursor: unset !important;
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
|
|
78
|
+
type BaseProps<T> = {
|
|
79
|
+
className?: string;
|
|
80
|
+
onKeyDown?: KeyboardEventHandler<HTMLElement>;
|
|
81
|
+
value?: Option<T> | null;
|
|
82
|
+
onChange?: (value?: Option<T>) => void;
|
|
83
|
+
onBlur?: () => void;
|
|
84
|
+
disabled?: boolean;
|
|
85
|
+
defaultValue?: Option<T>;
|
|
86
|
+
nullable?: boolean;
|
|
87
|
+
readOnly?: boolean;
|
|
88
|
+
onQueryChange?: (value: string) => void;
|
|
89
|
+
id?: string;
|
|
90
|
+
placeholder?: string;
|
|
91
|
+
"aria-describedby"?: string;
|
|
92
|
+
"aria-labelledby"?: string;
|
|
93
|
+
"aria-label"?: string;
|
|
94
|
+
testId?: string;
|
|
95
|
+
ref?: Ref<HTMLInputElement>;
|
|
96
|
+
form?: string;
|
|
97
|
+
name?: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type ComboboxProps<T> =
|
|
101
|
+
| (BaseProps<T> & {
|
|
102
|
+
options: Array<Option<T>> | ((query: string) => Array<Option<T>> | Promise<Array<Option<T>>>);
|
|
103
|
+
children?: (option: Option<T>, index: number) => ReactElement;
|
|
104
|
+
})
|
|
105
|
+
| (BaseProps<T> & { children: Array<ReactElement | null>; options?: never });
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @beta
|
|
109
|
+
* @since 2.45.0
|
|
110
|
+
*/
|
|
111
|
+
function ComboboxComponent<T>(props: ComboboxProps<T>, ref: ForwardedRef<HTMLInputElement>) {
|
|
112
|
+
const [query, setQuery] = useState("");
|
|
113
|
+
const { onQueryChange } = props;
|
|
114
|
+
|
|
115
|
+
useEffect(() => onQueryChange && onQueryChange(query), [onQueryChange, query]);
|
|
116
|
+
|
|
117
|
+
let options;
|
|
118
|
+
|
|
119
|
+
if (Array.isArray(props.children)) {
|
|
120
|
+
options = props.children;
|
|
121
|
+
} else if (typeof props.options === "function") {
|
|
122
|
+
options = <ComboboxOptions<T> children={props.children} options={props.options} query={query} />;
|
|
123
|
+
} else {
|
|
124
|
+
options = props.options?.map((o, index) =>
|
|
125
|
+
typeof props.children === "function" ? (
|
|
126
|
+
props.children(o, index)
|
|
127
|
+
) : (
|
|
128
|
+
<HeadlessCombobox.Option value={o} key={o.label} as={Fragment}>
|
|
129
|
+
{({ active }) => <StyledComboboxOption isActive={active}>{o.displayValue ?? o.label}</StyledComboboxOption>}
|
|
130
|
+
</HeadlessCombobox.Option>
|
|
131
|
+
)
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<HeadlessCombobox
|
|
137
|
+
as="div"
|
|
138
|
+
value={props.value}
|
|
139
|
+
onChange={(e?: Option<T>) => props.onChange && props.onChange(e)}
|
|
140
|
+
disabled={props.disabled || props.readOnly}
|
|
141
|
+
onKeyDown={(e: KeyboardEvent<HTMLElement>) => props.onKeyDown && props.onKeyDown(e)}
|
|
142
|
+
name={props.name}
|
|
143
|
+
form={props.form}
|
|
144
|
+
defaultValue={props.defaultValue}
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
nullable={props.nullable}
|
|
147
|
+
className="is-relative is-flex-grow-1 is-flex"
|
|
148
|
+
by="value"
|
|
149
|
+
>
|
|
150
|
+
<HeadlessCombobox.Input<Option<T>>
|
|
151
|
+
displayValue={(o) => o?.label}
|
|
152
|
+
ref={ref}
|
|
153
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
154
|
+
className={classNames(props.className, "is-full-width", "input", "is-ellipsis-overflow")}
|
|
155
|
+
aria-describedby={props["aria-describedby"]}
|
|
156
|
+
aria-labelledby={props["aria-labelledby"]}
|
|
157
|
+
aria-label={props["aria-label"]}
|
|
158
|
+
id={props.id}
|
|
159
|
+
placeholder={props.placeholder}
|
|
160
|
+
onBlur={props.onBlur}
|
|
161
|
+
{...createAttributesForTesting(props.testId)}
|
|
162
|
+
/>
|
|
163
|
+
<OptionsWrapper className="is-absolute">{options}</OptionsWrapper>
|
|
164
|
+
</HeadlessCombobox>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
type ComboboxOptionsProps<T> = {
|
|
169
|
+
options: (query: string) => Array<Option<T>> | Promise<Array<Option<T>>>;
|
|
170
|
+
children?: (option: Option<T>, index: number) => ReactElement;
|
|
171
|
+
query: string;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
function ComboboxOptions<T>({ query, options, children }: ComboboxOptionsProps<T>) {
|
|
175
|
+
const [optionsData, setOptionsData] = useState<Array<Option<T>>>([]);
|
|
176
|
+
const activePromise = useRef<Promise<Array<Option<T>>>>();
|
|
177
|
+
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
const optionsExec = options(query);
|
|
180
|
+
if (optionsExec instanceof Promise) {
|
|
181
|
+
activePromise.current = optionsExec;
|
|
182
|
+
optionsExec
|
|
183
|
+
.then((newOptions) => {
|
|
184
|
+
if (activePromise.current === optionsExec) {
|
|
185
|
+
setOptionsData(newOptions);
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
.catch(() => {
|
|
189
|
+
if (activePromise.current === optionsExec) {
|
|
190
|
+
setOptionsData([]);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
} else {
|
|
194
|
+
setOptionsData(optionsExec);
|
|
195
|
+
}
|
|
196
|
+
}, [options, query]);
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<>
|
|
200
|
+
{optionsData?.map((o, index) =>
|
|
201
|
+
typeof children === "function" ? (
|
|
202
|
+
children(o, index)
|
|
203
|
+
) : (
|
|
204
|
+
<HeadlessCombobox.Option value={o} key={o.label} as={Fragment}>
|
|
205
|
+
{({ active }) => <StyledComboboxOption isActive={active}>{o.displayValue ?? o.label}</StyledComboboxOption>}
|
|
206
|
+
</HeadlessCombobox.Option>
|
|
207
|
+
)
|
|
208
|
+
)}
|
|
209
|
+
</>
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const Combobox = Object.assign(withForwardRef(ComboboxComponent), {
|
|
214
|
+
Option: StyledComboboxOption,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
export default Combobox;
|