@scm-manager/ui-core 3.0.0-20240024-101702
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/.storybook/.babelrc +3 -0
- package/.storybook/RemoveThemesPlugin.js +57 -0
- package/.storybook/main.js +92 -0
- package/.storybook/preview-head.html +25 -0
- package/.storybook/preview.js +95 -0
- package/.storybook/withApiProvider.js +46 -0
- package/docs/introduction.stories.mdx +64 -0
- package/docs/usage.stories.mdx +22 -0
- package/package.json +81 -0
- package/src/base/buttons/Button.stories.tsx +89 -0
- package/src/base/buttons/Button.test.stories.mdx +74 -0
- package/src/base/buttons/Button.tsx +143 -0
- package/src/base/buttons/Icon.tsx +58 -0
- package/src/base/buttons/a11y.test.ts +34 -0
- package/src/base/buttons/docs/introduction.stories.mdx +64 -0
- package/src/base/buttons/docs/usage.stories.mdx +22 -0
- package/src/base/buttons/image-snapshot.test.ts +33 -0
- package/src/base/buttons/index.ts +26 -0
- package/src/base/forms/AddListEntryForm.tsx +127 -0
- package/src/base/forms/ConfigurationForm.tsx +59 -0
- package/src/base/forms/Form.stories.tsx +453 -0
- package/src/base/forms/Form.tsx +215 -0
- package/src/base/forms/FormPathContext.tsx +73 -0
- package/src/base/forms/FormRow.tsx +37 -0
- package/src/base/forms/ScmFormContext.tsx +43 -0
- package/src/base/forms/ScmFormListContext.tsx +65 -0
- package/src/base/forms/base/Control.tsx +34 -0
- package/src/base/forms/base/Field.tsx +35 -0
- package/src/base/forms/base/field-message/FieldMessage.tsx +34 -0
- package/src/base/forms/base/help/Help.tsx +34 -0
- package/src/base/forms/base/label/Label.tsx +35 -0
- package/src/base/forms/checkbox/Checkbox.stories.mdx +26 -0
- package/src/base/forms/checkbox/Checkbox.tsx +118 -0
- package/src/base/forms/checkbox/CheckboxField.tsx +39 -0
- package/src/base/forms/checkbox/ControlledCheckboxField.stories.mdx +36 -0
- package/src/base/forms/checkbox/ControlledCheckboxField.tsx +82 -0
- package/src/base/forms/chip-input/ChipInputField.stories.tsx +75 -0
- package/src/base/forms/chip-input/ChipInputField.tsx +169 -0
- package/src/base/forms/chip-input/ControlledChipInputField.tsx +111 -0
- package/src/base/forms/combobox/Combobox.stories.tsx +125 -0
- package/src/base/forms/combobox/Combobox.tsx +223 -0
- package/src/base/forms/combobox/ComboboxField.tsx +62 -0
- package/src/base/forms/combobox/ControlledComboboxField.tsx +96 -0
- package/src/base/forms/headless-chip-input/ChipInput.tsx +237 -0
- package/src/base/forms/helpers.ts +74 -0
- package/src/base/forms/index.ts +85 -0
- package/src/base/forms/input/ControlledInputField.stories.mdx +36 -0
- package/src/base/forms/input/ControlledInputField.tsx +87 -0
- package/src/base/forms/input/ControlledSecretConfirmationField.stories.mdx +39 -0
- package/src/base/forms/input/ControlledSecretConfirmationField.tsx +138 -0
- package/src/base/forms/input/Input.stories.mdx +22 -0
- package/src/base/forms/input/Input.tsx +46 -0
- package/src/base/forms/input/InputField.stories.mdx +22 -0
- package/src/base/forms/input/InputField.tsx +61 -0
- package/src/base/forms/input/Textarea.stories.mdx +28 -0
- package/src/base/forms/input/Textarea.tsx +46 -0
- package/src/base/forms/list/ControlledList.tsx +88 -0
- package/src/base/forms/radio-button/ControlledRadioGroupField.tsx +94 -0
- package/src/base/forms/radio-button/RadioButton.stories.tsx +226 -0
- package/src/base/forms/radio-button/RadioButton.tsx +116 -0
- package/src/base/forms/radio-button/RadioButtonContext.tsx +42 -0
- package/src/base/forms/radio-button/RadioGroup.tsx +49 -0
- package/src/base/forms/radio-button/RadioGroupField.tsx +58 -0
- package/src/base/forms/resourceHooks.ts +164 -0
- package/src/base/forms/select/ControlledSelectField.tsx +87 -0
- package/src/base/forms/select/Select.tsx +57 -0
- package/src/base/forms/select/SelectField.tsx +63 -0
- package/src/base/forms/table/ControlledColumn.tsx +49 -0
- package/src/base/forms/table/ControlledTable.tsx +99 -0
- package/src/base/forms/variants.ts +27 -0
- package/src/base/helpers/devbuild.ts +44 -0
- package/src/base/helpers/index.ts +26 -0
- package/src/base/helpers/useAriaId.tsx +31 -0
- package/src/base/index.ts +34 -0
- package/src/base/layout/_helpers/with-classes.tsx +52 -0
- package/src/base/layout/card/Card.stories.tsx +113 -0
- package/src/base/layout/card/Card.tsx +76 -0
- package/src/base/layout/card/CardDetail.tsx +196 -0
- package/src/base/layout/card/CardRow.tsx +46 -0
- package/src/base/layout/card/CardTitle.tsx +59 -0
- package/src/base/layout/card-list/CardList.stories.tsx +201 -0
- package/src/base/layout/card-list/CardList.tsx +76 -0
- package/src/base/layout/collapsible/Collapsible.stories.tsx +45 -0
- package/src/base/layout/collapsible/Collapsible.tsx +87 -0
- package/src/base/layout/index.ts +93 -0
- package/src/base/layout/tabs/TabTrigger.tsx +46 -0
- package/src/base/layout/tabs/Tabs.stories.tsx +48 -0
- package/src/base/layout/tabs/Tabs.tsx +52 -0
- package/src/base/layout/tabs/TabsContent.tsx +33 -0
- package/src/base/layout/tabs/TabsList.tsx +41 -0
- package/src/base/layout/templates/data-page/DataPage.stories.tsx +201 -0
- package/src/base/layout/templates/data-page/DataPageHeader.tsx +100 -0
- package/src/base/misc/Image.tsx +32 -0
- package/src/base/misc/Level.tsx +40 -0
- package/src/base/misc/Loading.tsx +64 -0
- package/src/base/misc/SubSubtitle.tsx +36 -0
- package/src/base/misc/Subtitle.tsx +37 -0
- package/src/base/misc/Title.tsx +56 -0
- package/src/base/misc/index.ts +30 -0
- package/src/base/notifications/BackendErrorNotification.tsx +160 -0
- package/src/base/notifications/ErrorNotification.tsx +73 -0
- package/src/base/notifications/Notification.tsx +48 -0
- package/src/base/notifications/index.tsx +27 -0
- package/src/base/overlays/dialog/Dialog.stories.tsx +64 -0
- package/src/base/overlays/dialog/Dialog.tsx +85 -0
- package/src/base/overlays/index.ts +44 -0
- package/src/base/overlays/menu/Menu.stories.tsx +78 -0
- package/src/base/overlays/menu/Menu.tsx +213 -0
- package/src/base/overlays/menu/MenuTrigger.tsx +63 -0
- package/src/base/overlays/popover/Popover.stories.tsx +69 -0
- package/src/base/overlays/popover/Popover.tsx +95 -0
- package/src/base/overlays/tooltip/Tooltip.examples.js +41 -0
- package/src/base/overlays/tooltip/Tooltip.stories.mdx +52 -0
- package/src/base/overlays/tooltip/Tooltip.tsx +96 -0
- package/src/base/shortcuts/index.ts +28 -0
- package/src/base/shortcuts/iterator/callbackIterator.ts +220 -0
- package/src/base/shortcuts/iterator/keyboardIterator.test.tsx +431 -0
- package/src/base/shortcuts/iterator/keyboardIterator.tsx +141 -0
- package/src/base/shortcuts/usePauseShortcuts.ts +44 -0
- package/src/base/shortcuts/useShortcut.ts +110 -0
- package/src/base/shortcuts/useShortcutDocs.tsx +54 -0
- package/src/base/text/SplitAndReplace.stories.tsx +83 -0
- package/src/base/text/SplitAndReplace.tsx +65 -0
- package/src/base/text/index.ts +25 -0
- package/src/base/text/textSplitAndReplace.test.ts +134 -0
- package/src/base/text/textSplitAndReplace.ts +86 -0
- package/src/index.ts +25 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,223 @@
|
|
|
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
|
+
KeyboardEventHandler,
|
|
29
|
+
ReactElement,
|
|
30
|
+
Ref,
|
|
31
|
+
useEffect,
|
|
32
|
+
useRef,
|
|
33
|
+
useState,
|
|
34
|
+
} from "react";
|
|
35
|
+
import { Combobox as HeadlessCombobox } from "@headlessui/react";
|
|
36
|
+
import classNames from "classnames";
|
|
37
|
+
import styled from "styled-components";
|
|
38
|
+
import { withForwardRef } from "../helpers";
|
|
39
|
+
import { createAttributesForTesting } from "../../helpers";
|
|
40
|
+
import { Option } from "@scm-manager/ui-types";
|
|
41
|
+
|
|
42
|
+
const OptionsWrapper = styled(HeadlessCombobox.Options).attrs({
|
|
43
|
+
className: "is-flex is-flex-direction-column has-rounded-border has-box-shadow is-overflow-hidden",
|
|
44
|
+
})`
|
|
45
|
+
z-index: 400;
|
|
46
|
+
top: 2.5rem;
|
|
47
|
+
border: var(--scm-border);
|
|
48
|
+
background-color: var(--scm-secondary-background);
|
|
49
|
+
max-width: 35ch;
|
|
50
|
+
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
|
+
> a {
|
|
77
|
+
color: inherit !important;
|
|
78
|
+
}
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
type BaseProps<T> = {
|
|
82
|
+
className?: string;
|
|
83
|
+
onKeyDown?: KeyboardEventHandler<HTMLElement>;
|
|
84
|
+
value?: Option<T> | null;
|
|
85
|
+
onChange?: (value?: Option<T>) => void;
|
|
86
|
+
onBlur?: () => void;
|
|
87
|
+
disabled?: boolean;
|
|
88
|
+
defaultValue?: Option<T>;
|
|
89
|
+
nullable?: boolean;
|
|
90
|
+
readOnly?: boolean;
|
|
91
|
+
onQueryChange?: (value: string) => void;
|
|
92
|
+
id?: string;
|
|
93
|
+
placeholder?: string;
|
|
94
|
+
"aria-describedby"?: string;
|
|
95
|
+
"aria-labelledby"?: string;
|
|
96
|
+
"aria-label"?: string;
|
|
97
|
+
testId?: string;
|
|
98
|
+
ref?: Ref<HTMLInputElement>;
|
|
99
|
+
form?: string;
|
|
100
|
+
name?: string;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export type ComboboxProps<T> =
|
|
104
|
+
| (BaseProps<T> & {
|
|
105
|
+
options: Array<Option<T>> | ((query: string) => Array<Option<T>> | Promise<Array<Option<T>>>);
|
|
106
|
+
children?: (option: Option<T>, index: number) => ReactElement;
|
|
107
|
+
})
|
|
108
|
+
| (BaseProps<T> & { children: Array<ReactElement | null>; options?: never });
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @beta
|
|
112
|
+
* @since 2.45.0
|
|
113
|
+
*/
|
|
114
|
+
function ComboboxComponent<T>(props: ComboboxProps<T>, ref: ForwardedRef<HTMLInputElement>) {
|
|
115
|
+
const [query, setQuery] = useState("");
|
|
116
|
+
const { onQueryChange } = props;
|
|
117
|
+
|
|
118
|
+
useEffect(() => onQueryChange && onQueryChange(query), [onQueryChange, query]);
|
|
119
|
+
|
|
120
|
+
let options;
|
|
121
|
+
|
|
122
|
+
if (Array.isArray(props.children)) {
|
|
123
|
+
options = props.children;
|
|
124
|
+
} else if (typeof props.options === "function") {
|
|
125
|
+
options = <ComboboxOptions<T> children={props.children} options={props.options} query={query} />;
|
|
126
|
+
} else {
|
|
127
|
+
options = props.options?.map((o, index) =>
|
|
128
|
+
typeof props.children === "function" ? (
|
|
129
|
+
props.children(o, index)
|
|
130
|
+
) : (
|
|
131
|
+
<HeadlessCombobox.Option value={o} key={o.label} as={Fragment}>
|
|
132
|
+
{({ active }) => <StyledComboboxOption isActive={active}>{o.displayValue ?? o.label}</StyledComboboxOption>}
|
|
133
|
+
</HeadlessCombobox.Option>
|
|
134
|
+
)
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<HeadlessCombobox
|
|
140
|
+
as="div"
|
|
141
|
+
value={props.value}
|
|
142
|
+
onChange={(e?: Option<T>) => props.onChange && props.onChange(e)}
|
|
143
|
+
disabled={props.disabled || props.readOnly}
|
|
144
|
+
name={props.name}
|
|
145
|
+
form={props.form}
|
|
146
|
+
defaultValue={props.defaultValue}
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
nullable={props.nullable}
|
|
149
|
+
className="is-relative is-flex-grow-1 is-flex"
|
|
150
|
+
by="value"
|
|
151
|
+
>
|
|
152
|
+
<HeadlessCombobox.Input<Option<T>>
|
|
153
|
+
displayValue={(o) => o?.label}
|
|
154
|
+
ref={ref}
|
|
155
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
156
|
+
className={classNames(props.className, "is-full-width", "input", "is-ellipsis-overflow")}
|
|
157
|
+
aria-describedby={props["aria-describedby"]}
|
|
158
|
+
aria-labelledby={props["aria-labelledby"]}
|
|
159
|
+
aria-label={props["aria-label"]}
|
|
160
|
+
id={props.id}
|
|
161
|
+
placeholder={props.placeholder}
|
|
162
|
+
onBlur={props.onBlur}
|
|
163
|
+
autoComplete="off"
|
|
164
|
+
onKeyDown={(e) => {
|
|
165
|
+
props.onKeyDown && props.onKeyDown(e);
|
|
166
|
+
}}
|
|
167
|
+
{...createAttributesForTesting(props.testId)}
|
|
168
|
+
/>
|
|
169
|
+
<OptionsWrapper className="is-absolute">{options}</OptionsWrapper>
|
|
170
|
+
</HeadlessCombobox>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
type ComboboxOptionsProps<T> = {
|
|
175
|
+
options: (query: string) => Array<Option<T>> | Promise<Array<Option<T>>>;
|
|
176
|
+
children?: (option: Option<T>, index: number) => ReactElement;
|
|
177
|
+
query: string;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
function ComboboxOptions<T>({ query, options, children }: ComboboxOptionsProps<T>) {
|
|
181
|
+
const [optionsData, setOptionsData] = useState<Array<Option<T>>>([]);
|
|
182
|
+
const activePromise = useRef<Promise<Array<Option<T>>>>();
|
|
183
|
+
|
|
184
|
+
useEffect(() => {
|
|
185
|
+
const optionsExec = options(query);
|
|
186
|
+
if (optionsExec instanceof Promise) {
|
|
187
|
+
activePromise.current = optionsExec;
|
|
188
|
+
optionsExec
|
|
189
|
+
.then((newOptions) => {
|
|
190
|
+
if (activePromise.current === optionsExec) {
|
|
191
|
+
setOptionsData(newOptions);
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
.catch(() => {
|
|
195
|
+
if (activePromise.current === optionsExec) {
|
|
196
|
+
setOptionsData([]);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
} else {
|
|
200
|
+
setOptionsData(optionsExec);
|
|
201
|
+
}
|
|
202
|
+
}, [options, query]);
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<>
|
|
206
|
+
{optionsData?.map((o, index) =>
|
|
207
|
+
typeof children === "function" ? (
|
|
208
|
+
children(o, index)
|
|
209
|
+
) : (
|
|
210
|
+
<HeadlessCombobox.Option value={o} key={o.label} as={Fragment}>
|
|
211
|
+
{({ active }) => <StyledComboboxOption isActive={active}>{o.displayValue ?? o.label}</StyledComboboxOption>}
|
|
212
|
+
</HeadlessCombobox.Option>
|
|
213
|
+
)
|
|
214
|
+
)}
|
|
215
|
+
</>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const Combobox = Object.assign(withForwardRef(ComboboxComponent), {
|
|
220
|
+
Option: StyledComboboxOption,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
export default Combobox;
|
|
@@ -0,0 +1,62 @@
|
|
|
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 Field from "../base/Field";
|
|
26
|
+
import Label from "../base/label/Label";
|
|
27
|
+
import Help from "../base/help/Help";
|
|
28
|
+
import React from "react";
|
|
29
|
+
import { useAriaId } from "../../helpers";
|
|
30
|
+
import { withForwardRef } from "../helpers";
|
|
31
|
+
import Combobox, { ComboboxProps } from "./Combobox";
|
|
32
|
+
import classNames from "classnames";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @beta
|
|
36
|
+
* @since 2.45.0
|
|
37
|
+
*/
|
|
38
|
+
const ComboboxField = function ComboboxField<T>(
|
|
39
|
+
{
|
|
40
|
+
label,
|
|
41
|
+
helpText,
|
|
42
|
+
error,
|
|
43
|
+
className,
|
|
44
|
+
isLoading,
|
|
45
|
+
...props
|
|
46
|
+
}: ComboboxProps<T> & { label: string; helpText?: string; error?: string; isLoading?: boolean },
|
|
47
|
+
ref: React.ForwardedRef<HTMLInputElement>
|
|
48
|
+
) {
|
|
49
|
+
const labelId = useAriaId();
|
|
50
|
+
return (
|
|
51
|
+
<Field className={className}>
|
|
52
|
+
<Label id={labelId}>
|
|
53
|
+
{label}
|
|
54
|
+
{helpText ? <Help className="ml-1" text={helpText} /> : null}
|
|
55
|
+
</Label>
|
|
56
|
+
<div className={classNames("control", { "is-loading": isLoading })}>
|
|
57
|
+
<Combobox {...props} ref={ref} aria-labelledby={labelId} />
|
|
58
|
+
</div>
|
|
59
|
+
</Field>
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
export default withForwardRef(ComboboxField);
|
|
@@ -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 React, { ComponentProps } from "react";
|
|
26
|
+
import { Controller, ControllerRenderProps, Path } from "react-hook-form";
|
|
27
|
+
import { useScmFormContext } from "../ScmFormContext";
|
|
28
|
+
import { useScmFormPathContext } from "../FormPathContext";
|
|
29
|
+
import { defaultOptionFactory, prefixWithoutIndices } from "../helpers";
|
|
30
|
+
import classNames from "classnames";
|
|
31
|
+
import ComboboxField from "./ComboboxField";
|
|
32
|
+
import { Option } from "@scm-manager/ui-types";
|
|
33
|
+
|
|
34
|
+
type Props<T extends Record<string, unknown>> = Omit<
|
|
35
|
+
Parameters<typeof ComboboxField>[0],
|
|
36
|
+
"error" | "label" | keyof ControllerRenderProps
|
|
37
|
+
> & {
|
|
38
|
+
rules?: ComponentProps<typeof Controller>["rules"];
|
|
39
|
+
name: Path<T>;
|
|
40
|
+
label?: string;
|
|
41
|
+
optionFactory?: (val: any) => Option<unknown>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @beta
|
|
46
|
+
* @since 2.45.0
|
|
47
|
+
*/
|
|
48
|
+
function ControlledComboboxField<T extends Record<string, unknown>>({
|
|
49
|
+
name,
|
|
50
|
+
label,
|
|
51
|
+
helpText,
|
|
52
|
+
rules,
|
|
53
|
+
testId,
|
|
54
|
+
defaultValue,
|
|
55
|
+
readOnly,
|
|
56
|
+
className,
|
|
57
|
+
optionFactory = defaultOptionFactory,
|
|
58
|
+
...props
|
|
59
|
+
}: Props<T>) {
|
|
60
|
+
const { control, t, readOnly: formReadonly, formId } = useScmFormContext();
|
|
61
|
+
const formPathPrefix = useScmFormPathContext();
|
|
62
|
+
const nameWithPrefix = formPathPrefix ? `${formPathPrefix}.${name}` : name;
|
|
63
|
+
const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
|
|
64
|
+
const labelTranslation = label || t(`${prefixedNameWithoutIndices}.label`) || "";
|
|
65
|
+
const helpTextTranslation = helpText || t(`${prefixedNameWithoutIndices}.helpText`);
|
|
66
|
+
return (
|
|
67
|
+
<Controller
|
|
68
|
+
control={control}
|
|
69
|
+
name={nameWithPrefix}
|
|
70
|
+
rules={rules}
|
|
71
|
+
defaultValue={defaultValue as never}
|
|
72
|
+
render={({ field: { onChange, value, ...field }, fieldState }) => (
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
<ComboboxField
|
|
75
|
+
form={formId}
|
|
76
|
+
readOnly={readOnly ?? formReadonly}
|
|
77
|
+
className={classNames("column", className)}
|
|
78
|
+
{...props}
|
|
79
|
+
{...field}
|
|
80
|
+
label={labelTranslation}
|
|
81
|
+
helpText={helpTextTranslation}
|
|
82
|
+
onChange={(e) => onChange(e?.value)}
|
|
83
|
+
value={optionFactory(value)}
|
|
84
|
+
error={
|
|
85
|
+
fieldState.error
|
|
86
|
+
? fieldState.error.message || t(`${prefixedNameWithoutIndices}.error.${fieldState.error.type}`)
|
|
87
|
+
: undefined
|
|
88
|
+
}
|
|
89
|
+
testId={testId ?? `select-${nameWithPrefix}`}
|
|
90
|
+
/>
|
|
91
|
+
)}
|
|
92
|
+
/>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default ControlledComboboxField;
|
|
@@ -0,0 +1,237 @@
|
|
|
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
|
+
ButtonHTMLAttributes,
|
|
27
|
+
ComponentProps,
|
|
28
|
+
ComponentType,
|
|
29
|
+
Context,
|
|
30
|
+
createContext,
|
|
31
|
+
HTMLAttributes,
|
|
32
|
+
KeyboardEventHandler,
|
|
33
|
+
LiHTMLAttributes,
|
|
34
|
+
ReactElement,
|
|
35
|
+
RefObject,
|
|
36
|
+
useCallback,
|
|
37
|
+
useContext,
|
|
38
|
+
useMemo,
|
|
39
|
+
useRef,
|
|
40
|
+
} from "react";
|
|
41
|
+
import { Slot } from "@radix-ui/react-slot";
|
|
42
|
+
import { Option } from "@scm-manager/ui-types";
|
|
43
|
+
import { mergeRefs, withForwardRef } from "../helpers";
|
|
44
|
+
import { Button } from "../../buttons";
|
|
45
|
+
|
|
46
|
+
type ChipInputContextType<T> = {
|
|
47
|
+
add(newValue: Option<T>): void;
|
|
48
|
+
remove(index: number): void;
|
|
49
|
+
inputRef: RefObject<HTMLInputElement>;
|
|
50
|
+
disabled?: boolean;
|
|
51
|
+
readOnly?: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const ChipInputContext = createContext<ChipInputContextType<never>>(null as unknown as ChipInputContextType<never>);
|
|
55
|
+
|
|
56
|
+
function getChipInputContext<T>() {
|
|
57
|
+
return ChipInputContext as unknown as Context<ChipInputContextType<T>>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type CustomNewChipInputProps<T> = {
|
|
61
|
+
onChange: (newValue: Option<T>) => void;
|
|
62
|
+
value?: Option<T> | null;
|
|
63
|
+
readOnly?: boolean;
|
|
64
|
+
disabled?: boolean;
|
|
65
|
+
ref?: React.Ref<HTMLInputElement>;
|
|
66
|
+
className?: string;
|
|
67
|
+
placeholder?: string;
|
|
68
|
+
id?: string;
|
|
69
|
+
"aria-describedby"?: string;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const DefaultNewChipInput = React.forwardRef<HTMLInputElement, CustomNewChipInputProps<string>>(
|
|
73
|
+
({ onChange, value, ...props }, ref) => {
|
|
74
|
+
const handleKeyDown = useCallback<KeyboardEventHandler<HTMLInputElement>>(
|
|
75
|
+
(e) => {
|
|
76
|
+
if (e.key === "Enter") {
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
if (e.currentTarget.value) {
|
|
79
|
+
onChange({ label: e.currentTarget.value, value: e.currentTarget.value });
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
[onChange]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<div className="is-flex-grow-1">
|
|
89
|
+
<input onKeyDown={handleKeyDown} {...props} ref={ref} />
|
|
90
|
+
</div>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
type ChipDeleteProps = {
|
|
96
|
+
asChild?: boolean;
|
|
97
|
+
index: number;
|
|
98
|
+
} & Omit<ButtonHTMLAttributes<HTMLButtonElement>, "type" | "onClick">;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @beta
|
|
102
|
+
* @since 2.44.0
|
|
103
|
+
*/
|
|
104
|
+
export const ChipDelete = React.forwardRef<HTMLButtonElement, ChipDeleteProps>(({ asChild, index, ...props }, ref) => {
|
|
105
|
+
const { remove, disabled } = useContext(ChipInputContext);
|
|
106
|
+
const Comp = asChild ? Slot : "button";
|
|
107
|
+
|
|
108
|
+
if (disabled) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return <Comp {...props} ref={ref} type="button" onClick={() => remove(index)} />;
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
type NewChipInputProps = {
|
|
116
|
+
children?: ReactElement | null;
|
|
117
|
+
ref?: React.Ref<HTMLInputElement>;
|
|
118
|
+
className?: string;
|
|
119
|
+
placeholder?: string;
|
|
120
|
+
id?: string;
|
|
121
|
+
"aria-describedby"?: string;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @beta
|
|
126
|
+
* @since 2.44.0
|
|
127
|
+
*/
|
|
128
|
+
export const NewChipInput = withForwardRef(function NewChipInput<T>(
|
|
129
|
+
props: NewChipInputProps,
|
|
130
|
+
ref: React.ForwardedRef<HTMLInputElement>
|
|
131
|
+
) {
|
|
132
|
+
const { add, disabled, readOnly, inputRef } = useContext(getChipInputContext<T>());
|
|
133
|
+
|
|
134
|
+
const Comp = props.children ? Slot : DefaultNewChipInput;
|
|
135
|
+
return React.createElement(Comp as unknown as ComponentType<CustomNewChipInputProps<T>>, {
|
|
136
|
+
...props,
|
|
137
|
+
onChange: add,
|
|
138
|
+
readOnly,
|
|
139
|
+
disabled,
|
|
140
|
+
value: null,
|
|
141
|
+
ref: mergeRefs(ref, inputRef),
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
type ChipProps = { asChild?: boolean } & LiHTMLAttributes<HTMLLIElement>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @beta
|
|
149
|
+
* @since 2.44.0
|
|
150
|
+
*/
|
|
151
|
+
export const Chip = React.forwardRef<HTMLLIElement, ChipProps>(({ asChild, ...props }, ref) => {
|
|
152
|
+
const Comp = asChild ? Slot : "li";
|
|
153
|
+
|
|
154
|
+
return <Comp {...props} ref={ref} />;
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
type Props<T> = {
|
|
158
|
+
value?: Option<T>[] | null;
|
|
159
|
+
onChange?: (newValue?: Option<T>[]) => void;
|
|
160
|
+
readOnly?: boolean;
|
|
161
|
+
disabled?: boolean;
|
|
162
|
+
isNewItemDuplicate?: (existingItem: Option<T>, newItem: Option<T>) => boolean;
|
|
163
|
+
} & Omit<HTMLAttributes<HTMLUListElement>, "onChange">;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @beta
|
|
167
|
+
* @since 2.44.0
|
|
168
|
+
*/
|
|
169
|
+
const ChipInput = withForwardRef(function ChipInput<T>(
|
|
170
|
+
{ children, value = [], disabled, readOnly, onChange, isNewItemDuplicate, ...props }: Props<T>,
|
|
171
|
+
ref: React.ForwardedRef<HTMLUListElement>
|
|
172
|
+
) {
|
|
173
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
174
|
+
const isInactive = useMemo(() => disabled || readOnly, [disabled, readOnly]);
|
|
175
|
+
const add = useCallback<(newValue: Option<T>) => void>(
|
|
176
|
+
(newItem) => {
|
|
177
|
+
if (
|
|
178
|
+
!isInactive &&
|
|
179
|
+
!value?.some((item) =>
|
|
180
|
+
isNewItemDuplicate
|
|
181
|
+
? isNewItemDuplicate(item, newItem)
|
|
182
|
+
: item.label === newItem.label || item.value === newItem.value
|
|
183
|
+
)
|
|
184
|
+
) {
|
|
185
|
+
if (onChange) {
|
|
186
|
+
onChange([...(value ?? []), newItem]);
|
|
187
|
+
}
|
|
188
|
+
if (inputRef.current) {
|
|
189
|
+
inputRef.current.value = "";
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
[isInactive, isNewItemDuplicate, onChange, value]
|
|
194
|
+
);
|
|
195
|
+
const remove = useCallback(
|
|
196
|
+
(index: number) => !isInactive && onChange && onChange(value?.filter((_, itdx) => itdx !== index)),
|
|
197
|
+
[isInactive, onChange, value]
|
|
198
|
+
);
|
|
199
|
+
const Context = getChipInputContext<T>();
|
|
200
|
+
return (
|
|
201
|
+
<Context.Provider
|
|
202
|
+
value={useMemo(
|
|
203
|
+
() => ({
|
|
204
|
+
disabled,
|
|
205
|
+
readOnly,
|
|
206
|
+
add,
|
|
207
|
+
remove,
|
|
208
|
+
inputRef,
|
|
209
|
+
}),
|
|
210
|
+
[add, disabled, readOnly, remove]
|
|
211
|
+
)}
|
|
212
|
+
>
|
|
213
|
+
<ul {...props} ref={ref}>
|
|
214
|
+
{children}
|
|
215
|
+
</ul>
|
|
216
|
+
</Context.Provider>
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const AddButton = React.forwardRef<
|
|
221
|
+
HTMLButtonElement,
|
|
222
|
+
Omit<ComponentProps<typeof Button>, "onClick"> & { inputRef: RefObject<HTMLInputElement | null> }
|
|
223
|
+
>(({ inputRef, children, ...props }, ref) => (
|
|
224
|
+
<Button
|
|
225
|
+
{...props}
|
|
226
|
+
onClick={() => inputRef.current?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }))}
|
|
227
|
+
>
|
|
228
|
+
{children}
|
|
229
|
+
</Button>
|
|
230
|
+
));
|
|
231
|
+
|
|
232
|
+
export default Object.assign(ChipInput, {
|
|
233
|
+
Chip: Object.assign(Chip, {
|
|
234
|
+
Delete: ChipDelete,
|
|
235
|
+
}),
|
|
236
|
+
AddButton,
|
|
237
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
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 { UseFormReturn } from "react-hook-form";
|
|
26
|
+
import { ForwardedRef, forwardRef, MutableRefObject, Ref, RefCallback } from "react";
|
|
27
|
+
|
|
28
|
+
export function prefixWithoutIndices(path: string): string {
|
|
29
|
+
return path.replace(/(\.\d+)/g, "");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Works like {@link setValue} but recursively applies the whole {@link newValues} object.
|
|
34
|
+
*
|
|
35
|
+
* > *Important Note:* This deeply overwrites input values of fields in arrays,
|
|
36
|
+
* > but does **NOT** add or remove items to existing arrays.
|
|
37
|
+
* > This can therefore not be used to clear lists.
|
|
38
|
+
*/
|
|
39
|
+
export function setValues<T>(newValues: T, setValue: UseFormReturn<T>["setValue"], path = "") {
|
|
40
|
+
for (const [key, val] of Object.entries(newValues)) {
|
|
41
|
+
if (val !== null && typeof val === "object") {
|
|
42
|
+
if (Array.isArray(val)) {
|
|
43
|
+
val.forEach((subVal, idx) => setValues(subVal, setValue, path ? `${path}.${key}.${idx}` : `${key}.${idx}`));
|
|
44
|
+
} else {
|
|
45
|
+
setValues(val, setValue, path ? `${path}.${key}` : key);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
const fullPath = path ? `${path}.${key}` : key;
|
|
49
|
+
setValue(fullPath as any, val, { shouldValidate: !fullPath.endsWith("Confirmation"), shouldDirty: true });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function withForwardRef<T extends { name: string }>(component: T): T {
|
|
55
|
+
return forwardRef(component as unknown as any) as any;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const defaultOptionFactory = (item: any) =>
|
|
59
|
+
typeof item === "object" && item !== null && "value" in item && typeof item["label"] === "string"
|
|
60
|
+
? item
|
|
61
|
+
: { label: item as string, value: item };
|
|
62
|
+
|
|
63
|
+
export function mergeRefs<T>(...refs: Array<RefCallback<T> | MutableRefObject<T> | ForwardedRef<T>>) {
|
|
64
|
+
return (el: T) =>
|
|
65
|
+
refs.forEach((ref) => {
|
|
66
|
+
if (ref) {
|
|
67
|
+
if (typeof ref === "function") {
|
|
68
|
+
ref(el);
|
|
69
|
+
} else {
|
|
70
|
+
ref.current = el;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|