@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.
@@ -29,6 +29,7 @@ import type { TFunction } from "i18next";
29
29
  type ContextType<T = any> = UseFormReturn<T> & {
30
30
  t: TFunction;
31
31
  readOnly?: boolean;
32
+ formId: string;
32
33
  };
33
34
 
34
35
  const ScmFormContext = React.createContext<ContextType>(null as unknown as ContextType);
@@ -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
+ }
@@ -23,14 +23,12 @@
23
23
  */
24
24
 
25
25
  import React from "react";
26
- import classNames from "classnames";
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
- <span className={classNames("fas fa-fw fa-question-circle has-text-blue-light", className)} title={text} />
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 labelTranslation = label || t(`${name}.label`) || "";
53
- const helpTextTranslation = helpText || t(`${name}.helpText`);
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={name}
60
+ name={nameWithPrefix}
58
61
  rules={rules}
59
62
  defaultValue={defaultChecked as never}
60
63
  render={({ field }) => (
61
64
  <CheckboxField
62
- className={classNames("column", className)}
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-${name}`}
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
- export { default as Form } from "./Form";
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 labelTranslation = label || t(`${name}.label`) || "";
53
- const helpTextTranslation = helpText || t(`${name}.helpText`);
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={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={fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : undefined}
70
- testId={testId ?? `input-${name}`}
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 labelTranslation = label || t(`${name}.label`) || "";
61
- const helpTextTranslation = helpText || t(`${name}.helpText`);
62
- const confirmationLabelTranslation = confirmationLabel || t(`${name}.confirmation.label`) || "";
63
- const confirmationHelpTextTranslation = confirmationHelpText || t(`${name}.confirmation.helpText`);
64
- const confirmationErrorMessageTranslation = confirmationErrorMessage || t(`${name}.confirmation.errorMessage`);
65
- const secretValue = watch(name);
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={name}
77
+ name={nameWithPrefix}
72
78
  defaultValue={defaultValue as never}
73
79
  rules={{
74
80
  ...rules,
75
- deps: [`${name}Confirmation`],
81
+ deps: [`${nameWithPrefix}Confirmation`],
76
82
  }}
77
83
  render={({ field, fieldState }) => (
78
84
  <InputField
79
- className={classNames("column", 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 ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : undefined
95
+ fieldState.error
96
+ ? fieldState.error.message || t(`${prefixedNameWithoutIndices}.error.${fieldState.error.type}`)
97
+ : undefined
89
98
  }
90
- testId={testId ?? `input-${name}`}
99
+ testId={testId ?? `input-${nameWithPrefix}`}
91
100
  />
92
101
  )}
93
102
  />
94
103
  <Controller
95
104
  control={control}
96
- name={`${name}Confirmation`}
105
+ name={`${nameWithPrefix}Confirmation`}
97
106
  defaultValue={defaultValue as never}
98
107
  render={({ field, fieldState }) => (
99
108
  <InputField
100
- className={classNames("column", 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 || t(`${name}.confirmation.error.${fieldState.error.type}`)
119
+ ? fieldState.error.message ||
120
+ t(`${prefixedNameWithoutIndices}.confirmation.error.${fieldState.error.type}`)
110
121
  : undefined
111
122
  }
112
- testId={confirmationTestId ?? `input-${name}-confirmation`}
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 labelTranslation = label || t(`${name}.label`) || "";
53
- const helpTextTranslation = helpText || t(`${name}.helpText`);
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={name}
60
+ name={nameWithPrefix}
58
61
  rules={rules}
59
62
  defaultValue={defaultValue as never}
60
63
  render={({ field, fieldState }) => (
61
64
  <SelectField
62
- className={classNames("column", className)}
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={fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : undefined}
70
- testId={testId ?? `select-${name}`}
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
  />
@@ -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>(({ variant, children, className, testId, ...props }, ref) => (
36
- <div className={classNames("select", { "is-multiple": props.multiple }, createVariantClass(variant), className)}>
37
- <select ref={ref} {...props} {...createAttributesForTesting(testId)}>
38
- {children}
39
- </select>
40
- </div>
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;