@scm-manager/ui-forms 2.42.3 → 2.42.4-20230213-150253
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 +105 -38
- package/build/index.js +447 -207
- package/build/index.mjs +435 -195
- package/package.json +8 -6
- package/src/AddListEntryForm.tsx +123 -0
- package/src/Form.stories.tsx +295 -0
- package/src/Form.tsx +39 -49
- package/src/FormPathContext.tsx +73 -0
- package/src/FormRow.tsx +15 -2
- package/src/ScmFormContext.tsx +1 -0
- package/src/ScmFormListContext.tsx +65 -0
- package/src/base/help/Help.tsx +4 -6
- package/src/checkbox/ControlledCheckboxField.tsx +11 -8
- package/src/helpers.ts +27 -0
- package/src/index.ts +28 -1
- package/src/input/ControlledInputField.tsx +16 -9
- package/src/input/ControlledSecretConfirmationField.tsx +28 -17
- package/src/list/ControlledList.tsx +88 -0
- package/src/select/ControlledSelectField.tsx +16 -9
- package/src/select/Select.tsx +18 -8
- package/src/table/ControlledColumn.tsx +49 -0
- package/src/table/ControlledTable.tsx +100 -0
- package/src/Form.stories.mdx +0 -160
package/src/ScmFormContext.tsx
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
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, { FC, useContext, useMemo } from "react";
|
|
26
|
+
import { FieldValues, useFieldArray, UseFieldArrayReturn } from "react-hook-form";
|
|
27
|
+
import { ScmFormPathContextProvider, useScmFormPathContext } from "./FormPathContext";
|
|
28
|
+
import { useScmFormContext } from "./ScmFormContext";
|
|
29
|
+
|
|
30
|
+
type ContextType<T extends FieldValues = any> = UseFieldArrayReturn<T> & { isNested: boolean };
|
|
31
|
+
|
|
32
|
+
const ScmFormListContext = React.createContext<ContextType>(null as unknown as ContextType);
|
|
33
|
+
|
|
34
|
+
type Props = {
|
|
35
|
+
name: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Sets up an array field to be displayed in an enclosed *Form.Table* or *Form.List*.
|
|
40
|
+
* A *Form.AddListEntryForm* can be used to implement a sub-form for adding new entries to the list.
|
|
41
|
+
*
|
|
42
|
+
* @beta
|
|
43
|
+
* @since 2.43.0
|
|
44
|
+
*/
|
|
45
|
+
export const ScmFormListContextProvider: FC<Props> = ({ name, children }) => {
|
|
46
|
+
const { control } = useScmFormContext();
|
|
47
|
+
const prefix = useScmFormPathContext();
|
|
48
|
+
const parentForm = useScmFormListContext();
|
|
49
|
+
const nameWithPrefix = useMemo(() => (prefix ? `${prefix}.${name}` : name), [name, prefix]);
|
|
50
|
+
const fieldArray = useFieldArray({
|
|
51
|
+
control,
|
|
52
|
+
name: nameWithPrefix,
|
|
53
|
+
});
|
|
54
|
+
const value = useMemo(() => ({ ...fieldArray, isNested: !!parentForm }), [fieldArray, parentForm]);
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<ScmFormPathContextProvider path={nameWithPrefix}>
|
|
58
|
+
<ScmFormListContext.Provider value={value}>{children}</ScmFormListContext.Provider>
|
|
59
|
+
</ScmFormPathContextProvider>
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function useScmFormListContext() {
|
|
64
|
+
return useContext(ScmFormListContext);
|
|
65
|
+
}
|
package/src/base/help/Help.tsx
CHANGED
|
@@ -23,14 +23,12 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import React from "react";
|
|
26
|
-
import
|
|
26
|
+
import { Tooltip } from "@scm-manager/ui-overlays";
|
|
27
27
|
|
|
28
28
|
type Props = { text?: string; className?: string };
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* TODO: Implement tooltip
|
|
32
|
-
*/
|
|
33
29
|
const Help = ({ text, className }: Props) => (
|
|
34
|
-
<
|
|
30
|
+
<Tooltip className={className} message={text}>
|
|
31
|
+
<i className="fas fa-fw fa-question-circle has-text-blue-light" />
|
|
32
|
+
</Tooltip>
|
|
35
33
|
);
|
|
36
34
|
export default Help;
|
|
@@ -24,9 +24,10 @@
|
|
|
24
24
|
|
|
25
25
|
import React, { ComponentProps } from "react";
|
|
26
26
|
import { Controller, ControllerRenderProps, Path, RegisterOptions } from "react-hook-form";
|
|
27
|
-
import classNames from "classnames";
|
|
28
27
|
import { useScmFormContext } from "../ScmFormContext";
|
|
29
28
|
import CheckboxField from "./CheckboxField";
|
|
29
|
+
import { useScmFormPathContext } from "../FormPathContext";
|
|
30
|
+
import { prefixWithoutIndices } from "../helpers";
|
|
30
31
|
|
|
31
32
|
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
33
|
ComponentProps<typeof CheckboxField>,
|
|
@@ -42,31 +43,33 @@ function ControlledInputField<T extends Record<string, unknown>>({
|
|
|
42
43
|
label,
|
|
43
44
|
helpText,
|
|
44
45
|
rules,
|
|
45
|
-
className,
|
|
46
46
|
testId,
|
|
47
47
|
defaultChecked,
|
|
48
48
|
readOnly,
|
|
49
49
|
...props
|
|
50
50
|
}: Props<T>) {
|
|
51
|
-
const { control, t, readOnly: formReadonly } = useScmFormContext();
|
|
52
|
-
const
|
|
53
|
-
const
|
|
51
|
+
const { control, t, readOnly: formReadonly, formId } = useScmFormContext();
|
|
52
|
+
const formPathPrefix = useScmFormPathContext();
|
|
53
|
+
const nameWithPrefix = formPathPrefix ? `${formPathPrefix}.${name}` : name;
|
|
54
|
+
const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
|
|
55
|
+
const labelTranslation = label || t(`${prefixedNameWithoutIndices}.label`) || "";
|
|
56
|
+
const helpTextTranslation = helpText || t(`${prefixedNameWithoutIndices}.helpText`);
|
|
54
57
|
return (
|
|
55
58
|
<Controller
|
|
56
59
|
control={control}
|
|
57
|
-
name={
|
|
60
|
+
name={nameWithPrefix}
|
|
58
61
|
rules={rules}
|
|
59
62
|
defaultValue={defaultChecked as never}
|
|
60
63
|
render={({ field }) => (
|
|
61
64
|
<CheckboxField
|
|
62
|
-
|
|
65
|
+
form={formId}
|
|
63
66
|
readOnly={readOnly ?? formReadonly}
|
|
64
67
|
defaultChecked={field.value}
|
|
65
68
|
{...props}
|
|
66
69
|
{...field}
|
|
67
70
|
label={labelTranslation}
|
|
68
71
|
helpText={helpTextTranslation}
|
|
69
|
-
testId={testId ?? `checkbox-${
|
|
72
|
+
testId={testId ?? `checkbox-${nameWithPrefix}`}
|
|
70
73
|
/>
|
|
71
74
|
)}
|
|
72
75
|
/>
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
export function prefixWithoutIndices(path: string): string {
|
|
26
|
+
return path.replace(/(\.\d+)/g, "");
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -22,6 +22,33 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import FormCmp from "./Form";
|
|
26
|
+
import FormRow from "./FormRow";
|
|
27
|
+
import ControlledInputField from "./input/ControlledInputField";
|
|
28
|
+
import ControlledCheckboxField from "./checkbox/ControlledCheckboxField";
|
|
29
|
+
import ControlledSecretConfirmationField from "./input/ControlledSecretConfirmationField";
|
|
30
|
+
import ControlledSelectField from "./select/ControlledSelectField";
|
|
31
|
+
import { ScmFormListContextProvider } from "./ScmFormListContext";
|
|
32
|
+
import ControlledList from "./list/ControlledList";
|
|
33
|
+
import ControlledTable from "./table/ControlledTable";
|
|
34
|
+
import ControlledColumn from "./table/ControlledColumn";
|
|
35
|
+
import AddListEntryForm from "./AddListEntryForm";
|
|
36
|
+
import { ScmNestedFormPathContextProvider } from "./FormPathContext";
|
|
37
|
+
|
|
26
38
|
export { default as ConfigurationForm } from "./ConfigurationForm";
|
|
27
39
|
export * from "./resourceHooks";
|
|
40
|
+
|
|
41
|
+
export const Form = Object.assign(FormCmp, {
|
|
42
|
+
Row: FormRow,
|
|
43
|
+
Input: ControlledInputField,
|
|
44
|
+
Checkbox: ControlledCheckboxField,
|
|
45
|
+
SecretConfirmation: ControlledSecretConfirmationField,
|
|
46
|
+
Select: ControlledSelectField,
|
|
47
|
+
PathContext: ScmNestedFormPathContextProvider,
|
|
48
|
+
ListContext: ScmFormListContextProvider,
|
|
49
|
+
List: ControlledList,
|
|
50
|
+
AddListEntryForm: AddListEntryForm,
|
|
51
|
+
Table: Object.assign(ControlledTable, {
|
|
52
|
+
Column: ControlledColumn,
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
@@ -24,9 +24,10 @@
|
|
|
24
24
|
|
|
25
25
|
import React, { ComponentProps } from "react";
|
|
26
26
|
import { Controller, ControllerRenderProps, Path } from "react-hook-form";
|
|
27
|
-
import classNames from "classnames";
|
|
28
27
|
import { useScmFormContext } from "../ScmFormContext";
|
|
29
28
|
import InputField from "./InputField";
|
|
29
|
+
import { useScmFormPathContext } from "../FormPathContext";
|
|
30
|
+
import { prefixWithoutIndices } from "../helpers";
|
|
30
31
|
|
|
31
32
|
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
33
|
ComponentProps<typeof InputField>,
|
|
@@ -42,32 +43,38 @@ function ControlledInputField<T extends Record<string, unknown>>({
|
|
|
42
43
|
label,
|
|
43
44
|
helpText,
|
|
44
45
|
rules,
|
|
45
|
-
className,
|
|
46
46
|
testId,
|
|
47
47
|
defaultValue,
|
|
48
48
|
readOnly,
|
|
49
49
|
...props
|
|
50
50
|
}: Props<T>) {
|
|
51
|
-
const { control, t, readOnly: formReadonly } = useScmFormContext();
|
|
52
|
-
const
|
|
53
|
-
const
|
|
51
|
+
const { control, t, readOnly: formReadonly, formId } = useScmFormContext();
|
|
52
|
+
const formPathPrefix = useScmFormPathContext();
|
|
53
|
+
const nameWithPrefix = formPathPrefix ? `${formPathPrefix}.${name}` : name;
|
|
54
|
+
const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
|
|
55
|
+
const labelTranslation = label || t(`${prefixedNameWithoutIndices}.label`) || "";
|
|
56
|
+
const helpTextTranslation = helpText || t(`${prefixedNameWithoutIndices}.helpText`);
|
|
54
57
|
return (
|
|
55
58
|
<Controller
|
|
56
59
|
control={control}
|
|
57
|
-
name={
|
|
60
|
+
name={nameWithPrefix}
|
|
58
61
|
rules={rules}
|
|
59
62
|
defaultValue={defaultValue as never}
|
|
60
63
|
render={({ field, fieldState }) => (
|
|
61
64
|
<InputField
|
|
62
|
-
className={classNames("column", className)}
|
|
63
65
|
readOnly={readOnly ?? formReadonly}
|
|
64
66
|
required={rules?.required as boolean}
|
|
65
67
|
{...props}
|
|
66
68
|
{...field}
|
|
69
|
+
form={formId}
|
|
67
70
|
label={labelTranslation}
|
|
68
71
|
helpText={helpTextTranslation}
|
|
69
|
-
error={
|
|
70
|
-
|
|
72
|
+
error={
|
|
73
|
+
fieldState.error
|
|
74
|
+
? fieldState.error.message || t(`${prefixedNameWithoutIndices}.error.${fieldState.error.type}`)
|
|
75
|
+
: undefined
|
|
76
|
+
}
|
|
77
|
+
testId={testId ?? `input-${nameWithPrefix}`}
|
|
71
78
|
/>
|
|
72
79
|
)}
|
|
73
80
|
/>
|
|
@@ -24,9 +24,10 @@
|
|
|
24
24
|
|
|
25
25
|
import React, { ComponentProps } from "react";
|
|
26
26
|
import { Controller, ControllerRenderProps, Path } from "react-hook-form";
|
|
27
|
-
import classNames from "classnames";
|
|
28
27
|
import { useScmFormContext } from "../ScmFormContext";
|
|
29
28
|
import InputField from "./InputField";
|
|
29
|
+
import { useScmFormPathContext } from "../FormPathContext";
|
|
30
|
+
import { prefixWithoutIndices } from "../helpers";
|
|
30
31
|
|
|
31
32
|
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
33
|
ComponentProps<typeof InputField>,
|
|
@@ -56,60 +57,70 @@ export default function ControlledSecretConfirmationField<T extends Record<strin
|
|
|
56
57
|
readOnly,
|
|
57
58
|
...props
|
|
58
59
|
}: Props<T>) {
|
|
59
|
-
const { control, watch, t, readOnly: formReadonly } = useScmFormContext();
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
const
|
|
60
|
+
const { control, watch, 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
|
+
const confirmationLabelTranslation = confirmationLabel || t(`${prefixedNameWithoutIndices}.confirmation.label`) || "";
|
|
67
|
+
const confirmationHelpTextTranslation =
|
|
68
|
+
confirmationHelpText || t(`${prefixedNameWithoutIndices}.confirmation.helpText`);
|
|
69
|
+
const confirmationErrorMessageTranslation =
|
|
70
|
+
confirmationErrorMessage || t(`${prefixedNameWithoutIndices}.confirmation.errorMessage`);
|
|
71
|
+
const secretValue = watch(nameWithPrefix);
|
|
66
72
|
|
|
67
73
|
return (
|
|
68
74
|
<>
|
|
69
75
|
<Controller
|
|
70
76
|
control={control}
|
|
71
|
-
name={
|
|
77
|
+
name={nameWithPrefix}
|
|
72
78
|
defaultValue={defaultValue as never}
|
|
73
79
|
rules={{
|
|
74
80
|
...rules,
|
|
75
|
-
deps: [`${
|
|
81
|
+
deps: [`${nameWithPrefix}Confirmation`],
|
|
76
82
|
}}
|
|
77
83
|
render={({ field, fieldState }) => (
|
|
78
84
|
<InputField
|
|
79
|
-
className={
|
|
85
|
+
className={className}
|
|
80
86
|
readOnly={readOnly ?? formReadonly}
|
|
81
87
|
{...props}
|
|
82
88
|
{...field}
|
|
89
|
+
form={formId}
|
|
83
90
|
required={rules?.required as boolean}
|
|
84
91
|
type="password"
|
|
85
92
|
label={labelTranslation}
|
|
86
93
|
helpText={helpTextTranslation}
|
|
87
94
|
error={
|
|
88
|
-
fieldState.error
|
|
95
|
+
fieldState.error
|
|
96
|
+
? fieldState.error.message || t(`${prefixedNameWithoutIndices}.error.${fieldState.error.type}`)
|
|
97
|
+
: undefined
|
|
89
98
|
}
|
|
90
|
-
testId={testId ?? `input-${
|
|
99
|
+
testId={testId ?? `input-${nameWithPrefix}`}
|
|
91
100
|
/>
|
|
92
101
|
)}
|
|
93
102
|
/>
|
|
94
103
|
<Controller
|
|
95
104
|
control={control}
|
|
96
|
-
name={`${
|
|
105
|
+
name={`${nameWithPrefix}Confirmation`}
|
|
97
106
|
defaultValue={defaultValue as never}
|
|
98
107
|
render={({ field, fieldState }) => (
|
|
99
108
|
<InputField
|
|
100
|
-
className={
|
|
109
|
+
className={className}
|
|
101
110
|
type="password"
|
|
102
111
|
readOnly={readOnly ?? formReadonly}
|
|
103
112
|
disabled={props.disabled}
|
|
104
113
|
{...field}
|
|
114
|
+
form={formId}
|
|
105
115
|
label={confirmationLabelTranslation}
|
|
106
116
|
helpText={confirmationHelpTextTranslation}
|
|
107
117
|
error={
|
|
108
118
|
fieldState.error
|
|
109
|
-
? fieldState.error.message ||
|
|
119
|
+
? fieldState.error.message ||
|
|
120
|
+
t(`${prefixedNameWithoutIndices}.confirmation.error.${fieldState.error.type}`)
|
|
110
121
|
: undefined
|
|
111
122
|
}
|
|
112
|
-
testId={confirmationTestId ?? `input-${
|
|
123
|
+
testId={confirmationTestId ?? `input-${nameWithPrefix}-confirmation`}
|
|
113
124
|
/>
|
|
114
125
|
)}
|
|
115
126
|
rules={{
|
|
@@ -0,0 +1,88 @@
|
|
|
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 from "react";
|
|
26
|
+
import { Path, PathValue } from "react-hook-form";
|
|
27
|
+
import { useScmFormContext } from "../ScmFormContext";
|
|
28
|
+
import { ScmFormPathContextProvider, useScmFormPathContext } from "../FormPathContext";
|
|
29
|
+
import { Button } from "@scm-manager/ui-buttons";
|
|
30
|
+
import { prefixWithoutIndices } from "../helpers";
|
|
31
|
+
import { useScmFormListContext } from "../ScmFormListContext";
|
|
32
|
+
import { useTranslation } from "react-i18next";
|
|
33
|
+
|
|
34
|
+
type ArrayItemType<T> = T extends Array<infer U> ? U : unknown;
|
|
35
|
+
|
|
36
|
+
type RenderProps<T extends Record<string, unknown>, PATH extends Path<T>> = {
|
|
37
|
+
value: ArrayItemType<PathValue<T, PATH>>;
|
|
38
|
+
index: number;
|
|
39
|
+
remove: () => void;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type Props<T extends Record<string, unknown>, PATH extends Path<T>> = {
|
|
43
|
+
withDelete?: boolean;
|
|
44
|
+
children: ((renderProps: RenderProps<T, PATH>) => React.ReactNode | React.ReactNode[]) | React.ReactNode;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @beta
|
|
49
|
+
* @since 2.43.0
|
|
50
|
+
*/
|
|
51
|
+
function ControlledList<T extends Record<string, unknown>, PATH extends Path<T>>({
|
|
52
|
+
withDelete,
|
|
53
|
+
children,
|
|
54
|
+
}: Props<T, PATH>) {
|
|
55
|
+
const [defaultTranslate] = useTranslation("commons", { keyPrefix: "form" });
|
|
56
|
+
const { readOnly, t } = useScmFormContext();
|
|
57
|
+
const nameWithPrefix = useScmFormPathContext();
|
|
58
|
+
const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
|
|
59
|
+
const { fields, remove } = useScmFormListContext();
|
|
60
|
+
|
|
61
|
+
const deleteButtonTranslation = t(`${prefixedNameWithoutIndices}.delete`, {
|
|
62
|
+
defaultValue: defaultTranslate("list.delete.label", {
|
|
63
|
+
entity: t(`${prefixedNameWithoutIndices}.entity`),
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<>
|
|
69
|
+
{fields.map((value, index) => (
|
|
70
|
+
<ScmFormPathContextProvider key={value.id} path={`${nameWithPrefix}.${index}`}>
|
|
71
|
+
{typeof children === "function"
|
|
72
|
+
? children({ value: value as never, index, remove: () => remove(index) })
|
|
73
|
+
: children}
|
|
74
|
+
{withDelete && !readOnly ? (
|
|
75
|
+
<div className="level-right">
|
|
76
|
+
<Button variant="signal" onClick={() => remove(index)}>
|
|
77
|
+
{deleteButtonTranslation}
|
|
78
|
+
</Button>
|
|
79
|
+
</div>
|
|
80
|
+
) : null}
|
|
81
|
+
<hr />
|
|
82
|
+
</ScmFormPathContextProvider>
|
|
83
|
+
))}
|
|
84
|
+
</>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default ControlledList;
|
|
@@ -24,9 +24,10 @@
|
|
|
24
24
|
|
|
25
25
|
import React, { ComponentProps } from "react";
|
|
26
26
|
import { Controller, ControllerRenderProps, Path } from "react-hook-form";
|
|
27
|
-
import classNames from "classnames";
|
|
28
27
|
import { useScmFormContext } from "../ScmFormContext";
|
|
29
28
|
import SelectField from "./SelectField";
|
|
29
|
+
import { useScmFormPathContext } from "../FormPathContext";
|
|
30
|
+
import { prefixWithoutIndices } from "../helpers";
|
|
30
31
|
|
|
31
32
|
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
33
|
ComponentProps<typeof SelectField>,
|
|
@@ -42,32 +43,38 @@ function ControlledSelectField<T extends Record<string, unknown>>({
|
|
|
42
43
|
label,
|
|
43
44
|
helpText,
|
|
44
45
|
rules,
|
|
45
|
-
className,
|
|
46
46
|
testId,
|
|
47
47
|
defaultValue,
|
|
48
48
|
readOnly,
|
|
49
49
|
...props
|
|
50
50
|
}: Props<T>) {
|
|
51
|
-
const { control, t, readOnly: formReadonly } = useScmFormContext();
|
|
52
|
-
const
|
|
53
|
-
const
|
|
51
|
+
const { control, t, readOnly: formReadonly, formId } = useScmFormContext();
|
|
52
|
+
const formPathPrefix = useScmFormPathContext();
|
|
53
|
+
const nameWithPrefix = formPathPrefix ? `${formPathPrefix}.${name}` : name;
|
|
54
|
+
const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
|
|
55
|
+
const labelTranslation = label || t(`${prefixedNameWithoutIndices}.label`) || "";
|
|
56
|
+
const helpTextTranslation = helpText || t(`${prefixedNameWithoutIndices}.helpText`);
|
|
54
57
|
return (
|
|
55
58
|
<Controller
|
|
56
59
|
control={control}
|
|
57
|
-
name={
|
|
60
|
+
name={nameWithPrefix}
|
|
58
61
|
rules={rules}
|
|
59
62
|
defaultValue={defaultValue as never}
|
|
60
63
|
render={({ field, fieldState }) => (
|
|
61
64
|
<SelectField
|
|
62
|
-
|
|
65
|
+
form={formId}
|
|
63
66
|
readOnly={readOnly ?? formReadonly}
|
|
64
67
|
required={rules?.required as boolean}
|
|
65
68
|
{...props}
|
|
66
69
|
{...field}
|
|
67
70
|
label={labelTranslation}
|
|
68
71
|
helpText={helpTextTranslation}
|
|
69
|
-
error={
|
|
70
|
-
|
|
72
|
+
error={
|
|
73
|
+
fieldState.error
|
|
74
|
+
? fieldState.error.message || t(`${prefixedNameWithoutIndices}.error.${fieldState.error.type}`)
|
|
75
|
+
: undefined
|
|
76
|
+
}
|
|
77
|
+
testId={testId ?? `select-${nameWithPrefix}`}
|
|
71
78
|
/>
|
|
72
79
|
)}
|
|
73
80
|
/>
|
package/src/select/Select.tsx
CHANGED
|
@@ -22,22 +22,32 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import React, { InputHTMLAttributes } from "react";
|
|
25
|
+
import React, { InputHTMLAttributes, Key, OptionHTMLAttributes } from "react";
|
|
26
26
|
import classNames from "classnames";
|
|
27
27
|
import { createVariantClass, Variant } from "../variants";
|
|
28
28
|
import { createAttributesForTesting } from "@scm-manager/ui-components";
|
|
29
29
|
|
|
30
30
|
type Props = {
|
|
31
31
|
variant?: Variant;
|
|
32
|
+
options?: Array<OptionHTMLAttributes<HTMLOptionElement> & { label: string }>;
|
|
32
33
|
testId?: string;
|
|
33
34
|
} & InputHTMLAttributes<HTMLSelectElement>;
|
|
34
35
|
|
|
35
|
-
const Select = React.forwardRef<HTMLSelectElement, Props>(
|
|
36
|
-
|
|
37
|
-
<
|
|
38
|
-
{
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
const Select = React.forwardRef<HTMLSelectElement, Props>(
|
|
37
|
+
({ variant, children, className, options, testId, ...props }, ref) => (
|
|
38
|
+
<div className={classNames("select", { "is-multiple": props.multiple }, createVariantClass(variant), className)}>
|
|
39
|
+
<select ref={ref} {...props} {...createAttributesForTesting(testId)}>
|
|
40
|
+
{options
|
|
41
|
+
? options.map((option) => (
|
|
42
|
+
<option {...option} key={option.value as Key}>
|
|
43
|
+
{option.label}
|
|
44
|
+
{option.children}
|
|
45
|
+
</option>
|
|
46
|
+
))
|
|
47
|
+
: children}
|
|
48
|
+
</select>
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
);
|
|
42
52
|
|
|
43
53
|
export default Select;
|
|
@@ -0,0 +1,49 @@
|
|
|
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, { FC, HTMLAttributes, useMemo } from "react";
|
|
26
|
+
import { useScmFormPathContext } from "../FormPathContext";
|
|
27
|
+
import { useScmFormContext } from "../ScmFormContext";
|
|
28
|
+
import { useWatch } from "react-hook-form";
|
|
29
|
+
|
|
30
|
+
type Props = {
|
|
31
|
+
name: string;
|
|
32
|
+
children?: (value: unknown) => React.ReactNode | React.ReactNode[];
|
|
33
|
+
} & HTMLAttributes<HTMLTableCellElement>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @beta
|
|
37
|
+
* @since 2.43.0
|
|
38
|
+
*/
|
|
39
|
+
const ControlledColumn: FC<Props> = ({ name, children, ...props }) => {
|
|
40
|
+
const { control } = useScmFormContext();
|
|
41
|
+
const formPathPrefix = useScmFormPathContext();
|
|
42
|
+
const nameWithPrefix = useMemo(() => (formPathPrefix ? `${formPathPrefix}.${name}` : name), [formPathPrefix, name]);
|
|
43
|
+
const value = useWatch({ control, name: nameWithPrefix, disabled: typeof children === "function" });
|
|
44
|
+
const allValues = useWatch({ control, name: formPathPrefix, disabled: typeof children !== "function" });
|
|
45
|
+
|
|
46
|
+
return <td {...props}>{typeof children === "function" ? children(allValues) : value}</td>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export default ControlledColumn;
|