@scm-manager/ui-forms 2.40.2-SNAPSHOT-1

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.
Files changed (39) hide show
  1. package/.storybook/RemoveThemesPlugin.js +57 -0
  2. package/.storybook/main.js +92 -0
  3. package/.storybook/preview-head.html +26 -0
  4. package/.storybook/preview.js +72 -0
  5. package/.turbo/turbo-build.log +15 -0
  6. package/build/index.d.ts +124 -0
  7. package/build/index.js +639 -0
  8. package/build/index.mjs +602 -0
  9. package/package.json +50 -0
  10. package/src/ConfigurationForm.tsx +54 -0
  11. package/src/Form.stories.mdx +160 -0
  12. package/src/Form.tsx +164 -0
  13. package/src/FormRow.tsx +37 -0
  14. package/src/ScmFormContext.tsx +42 -0
  15. package/src/base/Control.tsx +34 -0
  16. package/src/base/Field.tsx +33 -0
  17. package/src/base/field-message/FieldMessage.tsx +34 -0
  18. package/src/base/help/Help.tsx +36 -0
  19. package/src/base/label/Label.tsx +33 -0
  20. package/src/checkbox/Checkbox.stories.mdx +26 -0
  21. package/src/checkbox/Checkbox.tsx +85 -0
  22. package/src/checkbox/CheckboxField.tsx +39 -0
  23. package/src/checkbox/ControlledCheckboxField.stories.mdx +36 -0
  24. package/src/checkbox/ControlledCheckboxField.tsx +76 -0
  25. package/src/index.ts +27 -0
  26. package/src/input/ControlledInputField.stories.mdx +36 -0
  27. package/src/input/ControlledInputField.tsx +77 -0
  28. package/src/input/ControlledSecretConfirmationField.stories.mdx +39 -0
  29. package/src/input/ControlledSecretConfirmationField.tsx +121 -0
  30. package/src/input/Input.stories.mdx +22 -0
  31. package/src/input/Input.tsx +46 -0
  32. package/src/input/InputField.stories.mdx +22 -0
  33. package/src/input/InputField.tsx +60 -0
  34. package/src/resourceHooks.ts +155 -0
  35. package/src/select/ControlledSelectField.tsx +77 -0
  36. package/src/select/Select.tsx +43 -0
  37. package/src/select/SelectField.tsx +59 -0
  38. package/src/variants.ts +27 -0
  39. package/tsconfig.json +3 -0
@@ -0,0 +1,602 @@
1
+ // src/Form.tsx
2
+ import React18, { useCallback, useEffect, useState } from "react";
3
+ import { useForm } from "react-hook-form";
4
+ import { ErrorNotification, Level } from "@scm-manager/ui-components";
5
+
6
+ // src/ScmFormContext.tsx
7
+ import React, { useContext } from "react";
8
+ var ScmFormContext = React.createContext(null);
9
+ function ScmFormContextProvider({ children, ...props }) {
10
+ return /* @__PURE__ */ React.createElement(ScmFormContext.Provider, {
11
+ value: props
12
+ }, children);
13
+ }
14
+ function useScmFormContext() {
15
+ return useContext(ScmFormContext);
16
+ }
17
+
18
+ // src/Form.tsx
19
+ import { useTranslation } from "react-i18next";
20
+ import { Button } from "@scm-manager/ui-buttons";
21
+
22
+ // src/FormRow.tsx
23
+ import React2 from "react";
24
+ import classNames from "classnames";
25
+ var FormRow = React2.forwardRef(
26
+ ({ className, children, hidden, ...rest }, ref) => hidden ? null : /* @__PURE__ */ React2.createElement("div", {
27
+ ref,
28
+ className: classNames("columns", className),
29
+ ...rest
30
+ }, children)
31
+ );
32
+ var FormRow_default = FormRow;
33
+
34
+ // src/input/ControlledInputField.tsx
35
+ import React10 from "react";
36
+ import { Controller } from "react-hook-form";
37
+ import classNames8 from "classnames";
38
+
39
+ // src/input/InputField.tsx
40
+ import React9 from "react";
41
+
42
+ // src/base/Field.tsx
43
+ import React3 from "react";
44
+ import classNames2 from "classnames";
45
+ var Field = ({ className, children, ...rest }) => /* @__PURE__ */ React3.createElement("div", {
46
+ className: classNames2("field", className),
47
+ ...rest
48
+ }, children);
49
+ var Field_default = Field;
50
+
51
+ // src/base/Control.tsx
52
+ import React4 from "react";
53
+ import classNames3 from "classnames";
54
+ var Control = ({ className, children, ...rest }) => /* @__PURE__ */ React4.createElement("div", {
55
+ className: classNames3("control", className),
56
+ ...rest
57
+ }, children);
58
+ var Control_default = Control;
59
+
60
+ // src/base/label/Label.tsx
61
+ import React5 from "react";
62
+ import classNames4 from "classnames";
63
+ var Label = ({ className, children, ...rest }) => /* @__PURE__ */ React5.createElement("label", {
64
+ className: classNames4("label", className),
65
+ ...rest
66
+ }, children);
67
+ var Label_default = Label;
68
+
69
+ // src/base/field-message/FieldMessage.tsx
70
+ import React6 from "react";
71
+ import classNames5 from "classnames";
72
+
73
+ // src/variants.ts
74
+ var createVariantClass = (variant) => variant ? `is-${variant}` : void 0;
75
+
76
+ // src/base/field-message/FieldMessage.tsx
77
+ var FieldMessage = ({ variant, className, children }) => /* @__PURE__ */ React6.createElement("p", {
78
+ className: classNames5("help", createVariantClass(variant), className)
79
+ }, children);
80
+ var FieldMessage_default = FieldMessage;
81
+
82
+ // src/input/Input.tsx
83
+ import React7 from "react";
84
+ import classNames6 from "classnames";
85
+ import { createAttributesForTesting } from "@scm-manager/ui-components";
86
+ var Input = React7.forwardRef(({ variant, className, testId, ...props }, ref) => {
87
+ return /* @__PURE__ */ React7.createElement("input", {
88
+ ref,
89
+ className: classNames6("input", createVariantClass(variant), className),
90
+ ...props,
91
+ ...createAttributesForTesting(testId)
92
+ });
93
+ });
94
+ var Input_default = Input;
95
+
96
+ // src/base/help/Help.tsx
97
+ import React8 from "react";
98
+ import classNames7 from "classnames";
99
+ var Help = ({ text, className }) => /* @__PURE__ */ React8.createElement("span", {
100
+ className: classNames7("fas fa-fw fa-question-circle has-text-blue-light", className),
101
+ title: text
102
+ });
103
+ var Help_default = Help;
104
+
105
+ // src/input/InputField.tsx
106
+ var InputField = React9.forwardRef(
107
+ ({ label, helpText, error, className, ...props }, ref) => {
108
+ const variant = error ? "danger" : void 0;
109
+ return /* @__PURE__ */ React9.createElement(Field_default, {
110
+ className
111
+ }, /* @__PURE__ */ React9.createElement(Label_default, null, label, helpText ? /* @__PURE__ */ React9.createElement(Help_default, {
112
+ className: "ml-1",
113
+ text: helpText
114
+ }) : null), /* @__PURE__ */ React9.createElement(Control_default, null, /* @__PURE__ */ React9.createElement(Input_default, {
115
+ variant,
116
+ ref,
117
+ ...props
118
+ })), error ? /* @__PURE__ */ React9.createElement(FieldMessage_default, {
119
+ variant
120
+ }, error) : null);
121
+ }
122
+ );
123
+ var InputField_default = InputField;
124
+
125
+ // src/input/ControlledInputField.tsx
126
+ function ControlledInputField({
127
+ name,
128
+ label,
129
+ helpText,
130
+ rules,
131
+ className,
132
+ testId,
133
+ defaultValue,
134
+ readOnly,
135
+ ...props
136
+ }) {
137
+ const { control, t, readOnly: formReadonly } = useScmFormContext();
138
+ const labelTranslation = label || t(`${name}.label`) || "";
139
+ const helpTextTranslation = helpText || t(`${name}.helpText`);
140
+ return /* @__PURE__ */ React10.createElement(Controller, {
141
+ control,
142
+ name,
143
+ rules,
144
+ defaultValue,
145
+ render: ({ field, fieldState }) => /* @__PURE__ */ React10.createElement(InputField_default, {
146
+ className: classNames8("column", className),
147
+ readOnly: readOnly ?? formReadonly,
148
+ required: rules == null ? void 0 : rules.required,
149
+ ...props,
150
+ ...field,
151
+ label: labelTranslation,
152
+ helpText: helpTextTranslation,
153
+ error: fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : void 0,
154
+ testId: testId ?? `input-${name}`
155
+ })
156
+ });
157
+ }
158
+ var ControlledInputField_default = ControlledInputField;
159
+
160
+ // src/checkbox/ControlledCheckboxField.tsx
161
+ import React13 from "react";
162
+ import { Controller as Controller2 } from "react-hook-form";
163
+ import classNames9 from "classnames";
164
+
165
+ // src/checkbox/CheckboxField.tsx
166
+ import React12 from "react";
167
+
168
+ // src/checkbox/Checkbox.tsx
169
+ import React11 from "react";
170
+ import { createAttributesForTesting as createAttributesForTesting2 } from "@scm-manager/ui-components";
171
+ var Checkbox = React11.forwardRef(
172
+ ({ readOnly, label, value, name, checked, defaultChecked, defaultValue, testId, helpText, ...props }, ref) => /* @__PURE__ */ React11.createElement("label", {
173
+ className: "checkbox",
174
+ disabled: readOnly || props.disabled
175
+ }, readOnly ? /* @__PURE__ */ React11.createElement(React11.Fragment, null, /* @__PURE__ */ React11.createElement("input", {
176
+ type: "hidden",
177
+ name,
178
+ value,
179
+ defaultValue,
180
+ checked,
181
+ defaultChecked,
182
+ readOnly: true
183
+ }), /* @__PURE__ */ React11.createElement("input", {
184
+ type: "checkbox",
185
+ className: "mr-1",
186
+ ref,
187
+ value,
188
+ defaultValue,
189
+ checked,
190
+ defaultChecked,
191
+ ...props,
192
+ ...createAttributesForTesting2(testId),
193
+ disabled: true
194
+ })) : /* @__PURE__ */ React11.createElement("input", {
195
+ type: "checkbox",
196
+ className: "mr-1",
197
+ ref,
198
+ name,
199
+ value,
200
+ defaultValue,
201
+ checked,
202
+ defaultChecked,
203
+ ...props,
204
+ ...createAttributesForTesting2(testId)
205
+ }), label, helpText ? /* @__PURE__ */ React11.createElement(Help_default, {
206
+ className: "ml-1",
207
+ text: helpText
208
+ }) : null)
209
+ );
210
+ var Checkbox_default = Checkbox;
211
+
212
+ // src/checkbox/CheckboxField.tsx
213
+ var CheckboxField = React12.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ React12.createElement(Field_default, {
214
+ className
215
+ }, /* @__PURE__ */ React12.createElement(Control_default, null, /* @__PURE__ */ React12.createElement(Checkbox_default, {
216
+ ref,
217
+ ...props
218
+ }))));
219
+ var CheckboxField_default = CheckboxField;
220
+
221
+ // src/checkbox/ControlledCheckboxField.tsx
222
+ function ControlledInputField2({
223
+ name,
224
+ label,
225
+ helpText,
226
+ rules,
227
+ className,
228
+ testId,
229
+ defaultChecked,
230
+ readOnly,
231
+ ...props
232
+ }) {
233
+ const { control, t, readOnly: formReadonly } = useScmFormContext();
234
+ const labelTranslation = label || t(`${name}.label`) || "";
235
+ const helpTextTranslation = helpText || t(`${name}.helpText`);
236
+ return /* @__PURE__ */ React13.createElement(Controller2, {
237
+ control,
238
+ name,
239
+ rules,
240
+ defaultValue: defaultChecked,
241
+ render: ({ field }) => /* @__PURE__ */ React13.createElement(CheckboxField_default, {
242
+ className: classNames9("column", className),
243
+ readOnly: readOnly ?? formReadonly,
244
+ defaultChecked: field.value,
245
+ ...props,
246
+ ...field,
247
+ label: labelTranslation,
248
+ helpText: helpTextTranslation,
249
+ testId: testId ?? `checkbox-${name}`
250
+ })
251
+ });
252
+ }
253
+ var ControlledCheckboxField_default = ControlledInputField2;
254
+
255
+ // src/input/ControlledSecretConfirmationField.tsx
256
+ import React14 from "react";
257
+ import { Controller as Controller3 } from "react-hook-form";
258
+ import classNames10 from "classnames";
259
+ function ControlledSecretConfirmationField({
260
+ name,
261
+ label,
262
+ confirmationLabel,
263
+ helpText,
264
+ confirmationHelpText,
265
+ rules,
266
+ confirmationErrorMessage,
267
+ className,
268
+ testId,
269
+ confirmationTestId,
270
+ defaultValue,
271
+ readOnly,
272
+ ...props
273
+ }) {
274
+ const { control, watch, t, readOnly: formReadonly } = useScmFormContext();
275
+ const labelTranslation = label || t(`${name}.label`) || "";
276
+ const helpTextTranslation = helpText || t(`${name}.helpText`);
277
+ const confirmationLabelTranslation = confirmationLabel || t(`${name}.confirmation.label`) || "";
278
+ const confirmationHelpTextTranslation = confirmationHelpText || t(`${name}.confirmation.helpText`);
279
+ const confirmationErrorMessageTranslation = confirmationErrorMessage || t(`${name}.confirmation.errorMessage`);
280
+ const secretValue = watch(name);
281
+ return /* @__PURE__ */ React14.createElement(React14.Fragment, null, /* @__PURE__ */ React14.createElement(Controller3, {
282
+ control,
283
+ name,
284
+ defaultValue,
285
+ rules: {
286
+ ...rules,
287
+ deps: [`${name}Confirmation`]
288
+ },
289
+ render: ({ field, fieldState }) => /* @__PURE__ */ React14.createElement(InputField_default, {
290
+ className: classNames10("column", className),
291
+ readOnly: readOnly ?? formReadonly,
292
+ ...props,
293
+ ...field,
294
+ required: rules == null ? void 0 : rules.required,
295
+ type: "password",
296
+ label: labelTranslation,
297
+ helpText: helpTextTranslation,
298
+ error: fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : void 0,
299
+ testId: testId ?? `input-${name}`
300
+ })
301
+ }), /* @__PURE__ */ React14.createElement(Controller3, {
302
+ control,
303
+ name: `${name}Confirmation`,
304
+ defaultValue,
305
+ render: ({ field, fieldState }) => /* @__PURE__ */ React14.createElement(InputField_default, {
306
+ className: classNames10("column", className),
307
+ type: "password",
308
+ readOnly: readOnly ?? formReadonly,
309
+ disabled: props.disabled,
310
+ ...field,
311
+ label: confirmationLabelTranslation,
312
+ helpText: confirmationHelpTextTranslation,
313
+ error: fieldState.error ? fieldState.error.message || t(`${name}.confirmation.error.${fieldState.error.type}`) : void 0,
314
+ testId: confirmationTestId ?? `input-${name}-confirmation`
315
+ }),
316
+ rules: {
317
+ validate: (value) => secretValue === value || confirmationErrorMessageTranslation
318
+ }
319
+ }));
320
+ }
321
+
322
+ // src/select/ControlledSelectField.tsx
323
+ import React17 from "react";
324
+ import { Controller as Controller4 } from "react-hook-form";
325
+ import classNames12 from "classnames";
326
+
327
+ // src/select/SelectField.tsx
328
+ import React16 from "react";
329
+
330
+ // src/select/Select.tsx
331
+ import React15 from "react";
332
+ import classNames11 from "classnames";
333
+ import { createAttributesForTesting as createAttributesForTesting3 } from "@scm-manager/ui-components";
334
+ var Select = React15.forwardRef(({ variant, children, className, testId, ...props }, ref) => /* @__PURE__ */ React15.createElement("div", {
335
+ className: classNames11("select", { "is-multiple": props.multiple }, createVariantClass(variant), className)
336
+ }, /* @__PURE__ */ React15.createElement("select", {
337
+ ref,
338
+ ...props,
339
+ ...createAttributesForTesting3(testId)
340
+ }, children)));
341
+ var Select_default = Select;
342
+
343
+ // src/select/SelectField.tsx
344
+ var SelectField = React16.forwardRef(
345
+ ({ label, helpText, error, className, ...props }, ref) => {
346
+ const variant = error ? "danger" : void 0;
347
+ return /* @__PURE__ */ React16.createElement(Field_default, {
348
+ className
349
+ }, /* @__PURE__ */ React16.createElement(Label_default, null, label, helpText ? /* @__PURE__ */ React16.createElement(Help_default, {
350
+ className: "ml-1",
351
+ text: helpText
352
+ }) : null), /* @__PURE__ */ React16.createElement(Control_default, null, /* @__PURE__ */ React16.createElement(Select_default, {
353
+ variant,
354
+ ref,
355
+ ...props
356
+ })), error ? /* @__PURE__ */ React16.createElement(FieldMessage_default, {
357
+ variant
358
+ }, error) : null);
359
+ }
360
+ );
361
+ var SelectField_default = SelectField;
362
+
363
+ // src/select/ControlledSelectField.tsx
364
+ function ControlledSelectField({
365
+ name,
366
+ label,
367
+ helpText,
368
+ rules,
369
+ className,
370
+ testId,
371
+ defaultValue,
372
+ readOnly,
373
+ ...props
374
+ }) {
375
+ const { control, t, readOnly: formReadonly } = useScmFormContext();
376
+ const labelTranslation = label || t(`${name}.label`) || "";
377
+ const helpTextTranslation = helpText || t(`${name}.helpText`);
378
+ return /* @__PURE__ */ React17.createElement(Controller4, {
379
+ control,
380
+ name,
381
+ rules,
382
+ defaultValue,
383
+ render: ({ field, fieldState }) => /* @__PURE__ */ React17.createElement(SelectField_default, {
384
+ className: classNames12("column", className),
385
+ readOnly: readOnly ?? formReadonly,
386
+ required: rules == null ? void 0 : rules.required,
387
+ ...props,
388
+ ...field,
389
+ label: labelTranslation,
390
+ helpText: helpTextTranslation,
391
+ error: fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : void 0,
392
+ testId: testId ?? `select-${name}`
393
+ })
394
+ });
395
+ }
396
+ var ControlledSelectField_default = ControlledSelectField;
397
+
398
+ // src/Form.tsx
399
+ var SuccessNotification = ({ label, hide }) => {
400
+ if (!label) {
401
+ return null;
402
+ }
403
+ return /* @__PURE__ */ React18.createElement("div", {
404
+ className: "notification is-success"
405
+ }, /* @__PURE__ */ React18.createElement("button", {
406
+ className: "delete",
407
+ onClick: hide
408
+ }), label);
409
+ };
410
+ function Form({
411
+ children,
412
+ onSubmit,
413
+ defaultValues,
414
+ translationPath,
415
+ readOnly,
416
+ submitButtonTestId
417
+ }) {
418
+ const form = useForm({
419
+ mode: "onChange",
420
+ defaultValues
421
+ });
422
+ const { formState, handleSubmit, reset } = form;
423
+ const [ns, prefix] = translationPath;
424
+ const { t } = useTranslation(ns, { keyPrefix: prefix });
425
+ const { isDirty, isValid, isSubmitting, isSubmitSuccessful } = formState;
426
+ const [error, setError] = useState();
427
+ const [showSuccessNotification, setShowSuccessNotification] = useState(false);
428
+ useEffect(() => {
429
+ if (isSubmitSuccessful) {
430
+ setShowSuccessNotification(true);
431
+ }
432
+ }, [isSubmitSuccessful]);
433
+ useEffect(() => reset(defaultValues), [defaultValues, reset]);
434
+ useEffect(() => {
435
+ if (isDirty) {
436
+ setShowSuccessNotification(false);
437
+ }
438
+ }, [isDirty]);
439
+ const translateWithFallback = useCallback(
440
+ (key, ...args) => {
441
+ const translation = t(key, ...args);
442
+ if (translation === `${prefix}.${key}`) {
443
+ return "";
444
+ }
445
+ return translation;
446
+ },
447
+ [prefix, t]
448
+ );
449
+ const submit = useCallback(
450
+ async (data) => {
451
+ setError(null);
452
+ try {
453
+ return await onSubmit(data);
454
+ } catch (e) {
455
+ if (e instanceof Error) {
456
+ setError(e);
457
+ } else {
458
+ throw e;
459
+ }
460
+ }
461
+ },
462
+ [onSubmit]
463
+ );
464
+ return /* @__PURE__ */ React18.createElement(ScmFormContextProvider, {
465
+ ...form,
466
+ readOnly: isSubmitting || readOnly,
467
+ t: translateWithFallback
468
+ }, /* @__PURE__ */ React18.createElement("form", {
469
+ onSubmit: handleSubmit(submit)
470
+ }, showSuccessNotification ? /* @__PURE__ */ React18.createElement(SuccessNotification, {
471
+ label: translateWithFallback("submit-success-notification"),
472
+ hide: () => setShowSuccessNotification(false)
473
+ }) : null, typeof children === "function" ? children(form) : children, error ? /* @__PURE__ */ React18.createElement(ErrorNotification, {
474
+ error
475
+ }) : null, !readOnly ? /* @__PURE__ */ React18.createElement(Level, {
476
+ right: /* @__PURE__ */ React18.createElement(Button, {
477
+ type: "submit",
478
+ variant: "primary",
479
+ testId: submitButtonTestId ?? "submit-button",
480
+ disabled: !isDirty || !isValid,
481
+ isLoading: isSubmitting
482
+ }, t("submit"))
483
+ }) : null));
484
+ }
485
+ var Form_default = Object.assign(Form, {
486
+ Row: FormRow_default,
487
+ Input: ControlledInputField_default,
488
+ Checkbox: ControlledCheckboxField_default,
489
+ SecretConfirmation: ControlledSecretConfirmationField,
490
+ Select: ControlledSelectField_default
491
+ });
492
+
493
+ // src/ConfigurationForm.tsx
494
+ import { useConfigLink } from "@scm-manager/ui-api";
495
+ import { Loading } from "@scm-manager/ui-components";
496
+ import React19 from "react";
497
+ function ConfigurationForm({ link, translationPath, children }) {
498
+ const { initialConfiguration, isReadOnly, update, isLoading } = useConfigLink(link);
499
+ if (isLoading || !initialConfiguration) {
500
+ return /* @__PURE__ */ React19.createElement(Loading, null);
501
+ }
502
+ return /* @__PURE__ */ React19.createElement(Form_default, {
503
+ onSubmit: update,
504
+ translationPath,
505
+ defaultValues: initialConfiguration,
506
+ readOnly: isReadOnly
507
+ }, children);
508
+ }
509
+ var ConfigurationForm_default = ConfigurationForm;
510
+
511
+ // src/resourceHooks.ts
512
+ import { apiClient, requiredLink } from "@scm-manager/ui-api";
513
+ import { useMutation, useQueryClient } from "react-query";
514
+ var unwrapLink = (input, linkName) => {
515
+ if (Array.isArray(input)) {
516
+ return requiredLink(input[0], input[1]);
517
+ } else if (typeof input === "string") {
518
+ return input;
519
+ } else {
520
+ return input._links[linkName].href;
521
+ }
522
+ };
523
+ var createResource = (link, contentType) => {
524
+ return (payload) => {
525
+ return apiClient.post(link, payload, contentType).then((response) => {
526
+ const location = response.headers.get("Location");
527
+ if (!location) {
528
+ throw new Error("Server does not return required Location header");
529
+ }
530
+ return apiClient.get(location);
531
+ }).then((response) => response.json());
532
+ };
533
+ };
534
+ var useCreateResource = (link, [entityKey, collectionName], idFactory, { contentType = "application/json" } = {}) => {
535
+ const queryClient = useQueryClient();
536
+ const { mutateAsync, data, isLoading, error } = useMutation(createResource(link, contentType), {
537
+ onSuccess: (result) => {
538
+ queryClient.setQueryData([entityKey, idFactory(result)], result);
539
+ return queryClient.invalidateQueries(collectionName);
540
+ }
541
+ });
542
+ return {
543
+ submit: (payload) => mutateAsync(payload),
544
+ isLoading,
545
+ error,
546
+ submissionResult: data
547
+ };
548
+ };
549
+ var useUpdateResource = (link, idFactory, {
550
+ contentType = "application/json",
551
+ collectionName: [entityQueryKey, collectionName] = ["", ""]
552
+ } = {}) => {
553
+ const queryClient = useQueryClient();
554
+ const { mutateAsync, isLoading, error, data } = useMutation(
555
+ (resource) => apiClient.put(unwrapLink(link, "update"), resource, contentType),
556
+ {
557
+ onSuccess: async (_, payload) => {
558
+ await queryClient.invalidateQueries(entityQueryKey ? [entityQueryKey, idFactory(payload)] : idFactory(payload));
559
+ if (collectionName) {
560
+ await queryClient.invalidateQueries(collectionName);
561
+ }
562
+ }
563
+ }
564
+ );
565
+ return {
566
+ submit: (resource) => mutateAsync(resource),
567
+ isLoading,
568
+ error,
569
+ submissionResult: data
570
+ };
571
+ };
572
+ var useDeleteResource = (idFactory, { collectionName: [entityQueryKey, collectionName] = ["", ""] } = {}) => {
573
+ const queryClient = useQueryClient();
574
+ const { mutateAsync, isLoading, error, data } = useMutation(
575
+ (resource) => {
576
+ const deleteUrl = resource._links.delete.href;
577
+ return apiClient.delete(deleteUrl);
578
+ },
579
+ {
580
+ onSuccess: async (_, resource) => {
581
+ const id = idFactory(resource);
582
+ await queryClient.removeQueries(entityQueryKey ? [entityQueryKey, id] : id);
583
+ if (collectionName) {
584
+ await queryClient.invalidateQueries(collectionName);
585
+ }
586
+ }
587
+ }
588
+ );
589
+ return {
590
+ submit: (resource) => mutateAsync(resource),
591
+ isLoading,
592
+ error,
593
+ submissionResult: data
594
+ };
595
+ };
596
+ export {
597
+ ConfigurationForm_default as ConfigurationForm,
598
+ Form_default as Form,
599
+ useCreateResource,
600
+ useDeleteResource,
601
+ useUpdateResource
602
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@scm-manager/ui-forms",
3
+ "private": false,
4
+ "version": "2.40.2-SNAPSHOT-1",
5
+ "main": "build/index.js",
6
+ "types": "build/index.d.ts",
7
+ "module": "build/index.mjs",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "build": "tsup ./src/index.ts -d build --format esm,cjs --dts",
11
+ "storybook": "start-storybook -p 6006",
12
+ "build-storybook": "build-storybook"
13
+ },
14
+ "devDependencies": {
15
+ "@babel/core": "^7.19.0",
16
+ "@scm-manager/eslint-config": "^2.16.0",
17
+ "@scm-manager/prettier-config": "^2.10.1",
18
+ "@scm-manager/tsconfig": "^2.13.0",
19
+ "@scm-manager/ui-styles": "2.40.2-SNAPSHOT",
20
+ "@storybook/addon-actions": "^6.5.10",
21
+ "@storybook/addon-essentials": "^6.5.10",
22
+ "@storybook/addon-interactions": "^6.5.10",
23
+ "@storybook/addon-links": "^6.5.10",
24
+ "@storybook/builder-webpack5": "^6.5.10",
25
+ "@storybook/manager-webpack5": "^6.5.10",
26
+ "@storybook/react": "^6.5.10",
27
+ "@storybook/testing-library": "^0.0.13",
28
+ "@storybook/addon-docs": "^6.5.14",
29
+ "babel-loader": "^8.2.5",
30
+ "storybook-addon-mock": "^3.2.0",
31
+ "storybook-addon-themes": "^6.1.0",
32
+ "tsup": "^6.2.3"
33
+ },
34
+ "peerDependencies": {
35
+ "@scm-manager/ui-components": "^2.40.2-SNAPSHOT",
36
+ "classnames": "^2.3.1",
37
+ "react": "17",
38
+ "react-hook-form": "7",
39
+ "react-i18next": "11",
40
+ "react-query": "3"
41
+ },
42
+ "dependencies": {
43
+ "@scm-manager/ui-buttons": "^2.40.2-SNAPSHOT",
44
+ "@scm-manager/ui-api": "^2.40.2-SNAPSHOT"
45
+ },
46
+ "prettier": "@scm-manager/prettier-config",
47
+ "eslintConfig": {
48
+ "extends": "@scm-manager/eslint-config"
49
+ }
50
+ }
@@ -0,0 +1,54 @@
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
+ import {useConfigLink} from "@scm-manager/ui-api";
25
+ import {Loading} from "@scm-manager/ui-components";
26
+ import React, {ComponentProps} from "react";
27
+ import {HalRepresentation} from "@scm-manager/ui-types";
28
+ import Form from "./Form";
29
+
30
+ type Props<T extends HalRepresentation> = Pick<ComponentProps<typeof Form<T, T>>, "translationPath" | "children"> & {
31
+ link: string;
32
+ };
33
+
34
+ /** @Beta */
35
+ export function ConfigurationForm<T extends HalRepresentation>({link, translationPath, children}: Props<T>) {
36
+ const {initialConfiguration, isReadOnly, update, isLoading} = useConfigLink<T>(link);
37
+
38
+ if (isLoading || !initialConfiguration) {
39
+ return <Loading/>;
40
+ }
41
+
42
+ return (
43
+ <Form<T, T>
44
+ onSubmit={update}
45
+ translationPath={translationPath}
46
+ defaultValues={initialConfiguration}
47
+ readOnly={isReadOnly}
48
+ >
49
+ {children}
50
+ </Form>
51
+ );
52
+ }
53
+
54
+ export default ConfigurationForm;