@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.
@@ -0,0 +1,100 @@
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, { ReactElement } 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 classNames from "classnames";
32
+ import { useScmFormListContext } from "../ScmFormListContext";
33
+ import { useTranslation } from "react-i18next";
34
+
35
+ type RenderProps<T extends Record<string, unknown>, PATH extends Path<T>> = {
36
+ value: PathValue<T, PATH>;
37
+ index: number;
38
+ remove: () => void;
39
+ };
40
+
41
+ type Props<T extends Record<string, unknown>, PATH extends Path<T>> = {
42
+ withDelete?: boolean;
43
+ children: ((renderProps: RenderProps<T, PATH>) => React.ReactNode | React.ReactNode[]) | React.ReactNode;
44
+ className?: string;
45
+ };
46
+
47
+ /**
48
+ * @beta
49
+ * @since 2.43.0
50
+ */
51
+ function ControlledTable<T extends Record<string, unknown>, PATH extends Path<T>>({
52
+ withDelete,
53
+ children,
54
+ className,
55
+ }: Props<T, PATH>) {
56
+ const [defaultTranslate] = useTranslation("commons", { keyPrefix: "form.table" });
57
+ const { readOnly, t } = useScmFormContext();
58
+ const nameWithPrefix = useScmFormPathContext();
59
+ const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
60
+ const { fields, remove } = useScmFormListContext();
61
+ const deleteLabel = t(`${prefixedNameWithoutIndices}.delete`) || defaultTranslate("delete.label");
62
+ const actionHeaderLabel = t(`${prefixedNameWithoutIndices}.action.label`) || defaultTranslate("headers.action.label");
63
+
64
+ if (!fields.length) {
65
+ return null;
66
+ }
67
+
68
+ return (
69
+ <table className={classNames("table content is-hoverable", className)}>
70
+ <thead>
71
+ <tr>
72
+ {React.Children.map(children, (child) => (
73
+ <th>{t(`${prefixedNameWithoutIndices}.${(child as ReactElement).props.name}.label`)}</th>
74
+ ))}
75
+ {withDelete && !readOnly ? <th className="has-text-right">{actionHeaderLabel}</th> : null}
76
+ </tr>
77
+ </thead>
78
+ <tbody>
79
+ {fields.map((value, index) => (
80
+ <ScmFormPathContextProvider key={value.id} path={`${nameWithPrefix}.${index}`}>
81
+ <tr>
82
+ {children}
83
+ {withDelete && !readOnly ? (
84
+ <td className="has-text-right">
85
+ <Button className="px-4" onClick={() => remove(index)} aria-label={deleteLabel}>
86
+ <span className="icon is-small">
87
+ <i className="fas fa-trash" />
88
+ </span>
89
+ </Button>
90
+ </td>
91
+ ) : null}
92
+ </tr>
93
+ </ScmFormPathContextProvider>
94
+ ))}
95
+ </tbody>
96
+ </table>
97
+ );
98
+ }
99
+
100
+ export default ControlledTable;
@@ -1,160 +0,0 @@
1
- import { Meta, Story } from "@storybook/addon-docs";
2
- import Form from "./Form";
3
- import FormRow from "./FormRow";
4
- import ControlledInputField from "./input/ControlledInputField";
5
- import ControlledSecretConfirmationField from "./input/ControlledSecretConfirmationField";
6
- import ControlledCheckboxField from "./checkbox/ControlledCheckboxField";
7
-
8
- <Meta title="Form" />
9
-
10
- <Story name="Creation">
11
- <Form
12
- onSubmit={console.log}
13
- translationPath={["sample", "form"]}
14
- defaultValues={{
15
- name: "",
16
- password: "",
17
- active: true,
18
- }}
19
- >
20
- <FormRow>
21
- <ControlledInputField name="name" />
22
- </FormRow>
23
- <FormRow>
24
- <ControlledSecretConfirmationField name="password" />
25
- </FormRow>
26
- <FormRow>
27
- <ControlledCheckboxField name="active" />
28
- </FormRow>
29
- </Form>
30
- </Story>
31
-
32
- <Story name="Editing">
33
- <Form
34
- onSubmit={console.log}
35
- translationPath={["sample", "form"]}
36
- defaultValues={{
37
- name: "trillian",
38
- password: "secret",
39
- active: true,
40
- }}
41
- >
42
- <FormRow>
43
- <ControlledInputField name="name" />
44
- </FormRow>
45
- <FormRow>
46
- <ControlledSecretConfirmationField name="password" />
47
- </FormRow>
48
- <FormRow>
49
- <ControlledCheckboxField name="active" />
50
- </FormRow>
51
- </Form>
52
- </Story>
53
-
54
- <Story name="GlobalConfiguration">
55
- <Form
56
- onSubmit={console.log}
57
- translationPath={["sample", "form"]}
58
- defaultValues={{
59
- url: "",
60
- filter: "",
61
- username: "",
62
- password: "",
63
- roleLevel: "",
64
- updateIssues: false,
65
- disableRepoConfig: false,
66
- }}
67
- >
68
- {({ watch }) => (
69
- <>
70
- <FormRow>
71
- <ControlledInputField name="url" label="URL" helpText="URL of Jira installation (with context path)." />
72
- </FormRow>
73
- <FormRow>
74
- <ControlledInputField name="filter" label="Project Filter" helpText="Filters for jira project key." />
75
- </FormRow>
76
- <FormRow>
77
- <ControlledCheckboxField
78
- name="updateIssues"
79
- label="Update Jira Issues"
80
- helpText="Enable the automatic update function."
81
- />
82
- </FormRow>
83
- <FormRow hide={watch("filter")}>
84
- <ControlledInputField
85
- name="username"
86
- label="Username"
87
- helpText="Jira username for connection."
88
- className="is-half"
89
- />
90
- <ControlledInputField
91
- name="password"
92
- label="Password"
93
- helpText="Jira password for connection."
94
- type="password"
95
- className="is-half"
96
- />
97
- </FormRow>
98
- <FormRow hide={watch("filter")}>
99
- <ControlledInputField
100
- name="roleLevel"
101
- label="Role Visibility"
102
- helpText="Defines for which Project Role the comments are visible."
103
- />
104
- </FormRow>
105
- <FormRow>
106
- <ControlledCheckboxField
107
- name="disableRepoConfig"
108
- label="Do not allow repository configuration"
109
- helpText="Do not allow repository owners to configure jira instances."
110
- />
111
- </FormRow>
112
- </>
113
- )}
114
- </Form>
115
- </Story>
116
-
117
- <Story name="RepoConfiguration">
118
- <Form
119
- onSubmit={console.log}
120
- translationPath={["sample", "form"]}
121
- defaultValues={{
122
- url: "",
123
- option: "",
124
- anotherOption: "",
125
- disableA: false,
126
- disableB: false,
127
- disableC: true,
128
- }}
129
- >
130
- <ControlledInputField name="url" />
131
- <ControlledInputField name="option" />
132
- <ControlledInputField name="anotherOption" />
133
- <ControlledCheckboxField name="disableA" />
134
- <ControlledCheckboxField name="disableB" />
135
- <ControlledCheckboxField name="disableC" />
136
- </Form>
137
- </Story>
138
-
139
- <Story name="ReadOnly">
140
- <Form
141
- onSubmit={console.log}
142
- translationPath={["sample", "form"]}
143
- defaultValues={{
144
- name: "trillian",
145
- password: "secret",
146
- active: true,
147
- }}
148
- readOnly
149
- >
150
- <FormRow>
151
- <ControlledInputField name="name" />
152
- </FormRow>
153
- <FormRow>
154
- <ControlledSecretConfirmationField name="password" />
155
- </FormRow>
156
- <FormRow>
157
- <ControlledCheckboxField name="active" />
158
- </FormRow>
159
- </Form>
160
- </Story>