@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.
- package/.storybook/RemoveThemesPlugin.js +57 -0
- package/.storybook/main.js +92 -0
- package/.storybook/preview-head.html +26 -0
- package/.storybook/preview.js +72 -0
- package/.turbo/turbo-build.log +15 -0
- package/build/index.d.ts +124 -0
- package/build/index.js +639 -0
- package/build/index.mjs +602 -0
- package/package.json +50 -0
- package/src/ConfigurationForm.tsx +54 -0
- package/src/Form.stories.mdx +160 -0
- package/src/Form.tsx +164 -0
- package/src/FormRow.tsx +37 -0
- package/src/ScmFormContext.tsx +42 -0
- package/src/base/Control.tsx +34 -0
- package/src/base/Field.tsx +33 -0
- package/src/base/field-message/FieldMessage.tsx +34 -0
- package/src/base/help/Help.tsx +36 -0
- package/src/base/label/Label.tsx +33 -0
- package/src/checkbox/Checkbox.stories.mdx +26 -0
- package/src/checkbox/Checkbox.tsx +85 -0
- package/src/checkbox/CheckboxField.tsx +39 -0
- package/src/checkbox/ControlledCheckboxField.stories.mdx +36 -0
- package/src/checkbox/ControlledCheckboxField.tsx +76 -0
- package/src/index.ts +27 -0
- package/src/input/ControlledInputField.stories.mdx +36 -0
- package/src/input/ControlledInputField.tsx +77 -0
- package/src/input/ControlledSecretConfirmationField.stories.mdx +39 -0
- package/src/input/ControlledSecretConfirmationField.tsx +121 -0
- package/src/input/Input.stories.mdx +22 -0
- package/src/input/Input.tsx +46 -0
- package/src/input/InputField.stories.mdx +22 -0
- package/src/input/InputField.tsx +60 -0
- package/src/resourceHooks.ts +155 -0
- package/src/select/ControlledSelectField.tsx +77 -0
- package/src/select/Select.tsx +43 -0
- package/src/select/SelectField.tsx +59 -0
- package/src/variants.ts +27 -0
- package/tsconfig.json +3 -0
|
@@ -0,0 +1,85 @@
|
|
|
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, { InputHTMLAttributes } from "react";
|
|
26
|
+
import { createAttributesForTesting } from "@scm-manager/ui-components";
|
|
27
|
+
import Help from "../base/help/Help";
|
|
28
|
+
|
|
29
|
+
type InputFieldProps = {
|
|
30
|
+
label: string;
|
|
31
|
+
helpText?: string;
|
|
32
|
+
testId?: string;
|
|
33
|
+
} & Omit<InputHTMLAttributes<HTMLInputElement>, "type">;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @see https://bulma.io/documentation/form/checkbox/
|
|
37
|
+
*/
|
|
38
|
+
const Checkbox = React.forwardRef<HTMLInputElement, InputFieldProps>(
|
|
39
|
+
({ readOnly, label, value, name, checked, defaultChecked, defaultValue, testId, helpText, ...props }, ref) => (
|
|
40
|
+
// @ts-ignore bulma uses the disabled attribute on labels, although it is not part of the html spec
|
|
41
|
+
<label className="checkbox" disabled={readOnly || props.disabled}>
|
|
42
|
+
{readOnly ? (
|
|
43
|
+
<>
|
|
44
|
+
<input
|
|
45
|
+
type="hidden"
|
|
46
|
+
name={name}
|
|
47
|
+
value={value}
|
|
48
|
+
defaultValue={defaultValue}
|
|
49
|
+
checked={checked}
|
|
50
|
+
defaultChecked={defaultChecked}
|
|
51
|
+
readOnly
|
|
52
|
+
/>
|
|
53
|
+
<input
|
|
54
|
+
type="checkbox"
|
|
55
|
+
className="mr-1"
|
|
56
|
+
ref={ref}
|
|
57
|
+
value={value}
|
|
58
|
+
defaultValue={defaultValue}
|
|
59
|
+
checked={checked}
|
|
60
|
+
defaultChecked={defaultChecked}
|
|
61
|
+
{...props}
|
|
62
|
+
{...createAttributesForTesting(testId)}
|
|
63
|
+
disabled
|
|
64
|
+
/>
|
|
65
|
+
</>
|
|
66
|
+
) : (
|
|
67
|
+
<input
|
|
68
|
+
type="checkbox"
|
|
69
|
+
className="mr-1"
|
|
70
|
+
ref={ref}
|
|
71
|
+
name={name}
|
|
72
|
+
value={value}
|
|
73
|
+
defaultValue={defaultValue}
|
|
74
|
+
checked={checked}
|
|
75
|
+
defaultChecked={defaultChecked}
|
|
76
|
+
{...props}
|
|
77
|
+
{...createAttributesForTesting(testId)}
|
|
78
|
+
/>
|
|
79
|
+
)}
|
|
80
|
+
{label}
|
|
81
|
+
{helpText ? <Help className="ml-1" text={helpText} /> : null}
|
|
82
|
+
</label>
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
export default Checkbox;
|
|
@@ -0,0 +1,39 @@
|
|
|
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 Field from "../base/Field";
|
|
27
|
+
import Control from "../base/Control";
|
|
28
|
+
import Checkbox from "./Checkbox";
|
|
29
|
+
|
|
30
|
+
type Props = React.ComponentProps<typeof Checkbox>;
|
|
31
|
+
|
|
32
|
+
const CheckboxField = React.forwardRef<HTMLInputElement, Props>(({ className, ...props }, ref) => (
|
|
33
|
+
<Field className={className}>
|
|
34
|
+
<Control>
|
|
35
|
+
<Checkbox ref={ref} {...props} />
|
|
36
|
+
</Control>
|
|
37
|
+
</Field>
|
|
38
|
+
));
|
|
39
|
+
export default CheckboxField;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Meta, Story } from "@storybook/addon-docs";
|
|
2
|
+
import Form from "../Form";
|
|
3
|
+
import ControlledCheckboxField from "./ControlledCheckboxField";
|
|
4
|
+
|
|
5
|
+
<Meta
|
|
6
|
+
title="ControlledCheckboxField"
|
|
7
|
+
decorators={[
|
|
8
|
+
(Story) => (
|
|
9
|
+
<Form onSubmit={console.log} defaultValues={{ checkOne: false, checkTwo: true, checkThree: false }} translationPath={["sample", "form"]}>
|
|
10
|
+
<Story />
|
|
11
|
+
</Form>
|
|
12
|
+
),
|
|
13
|
+
]}
|
|
14
|
+
/>
|
|
15
|
+
|
|
16
|
+
<Story name="Default">
|
|
17
|
+
<ControlledCheckboxField name="checkOne" />
|
|
18
|
+
</Story>
|
|
19
|
+
|
|
20
|
+
<Story name="WithHardcodedText">
|
|
21
|
+
<ControlledCheckboxField name="checkOne" label="Name" helpText="A help text" />
|
|
22
|
+
</Story>
|
|
23
|
+
|
|
24
|
+
<Story name="WithStyling">
|
|
25
|
+
<ControlledCheckboxField name="checkOne" className="has-background-blue-light" />
|
|
26
|
+
</Story>
|
|
27
|
+
|
|
28
|
+
<Story name="WithInitialFocus">
|
|
29
|
+
<ControlledCheckboxField name="checkOne" autoFocus={true} />
|
|
30
|
+
</Story>
|
|
31
|
+
|
|
32
|
+
<Story name="WithReadonly">
|
|
33
|
+
<ControlledCheckboxField name="checkOne" readOnly />
|
|
34
|
+
<ControlledCheckboxField name="checkTwo" disabled />
|
|
35
|
+
<ControlledCheckboxField name="checkThree" />
|
|
36
|
+
</Story>
|
|
@@ -0,0 +1,76 @@
|
|
|
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, RegisterOptions } from "react-hook-form";
|
|
27
|
+
import classNames from "classnames";
|
|
28
|
+
import { useScmFormContext } from "../ScmFormContext";
|
|
29
|
+
import CheckboxField from "./CheckboxField";
|
|
30
|
+
|
|
31
|
+
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
|
+
ComponentProps<typeof CheckboxField>,
|
|
33
|
+
"label" | "defaultValue" | "required" | keyof ControllerRenderProps
|
|
34
|
+
> & {
|
|
35
|
+
name: Path<T>;
|
|
36
|
+
label?: string;
|
|
37
|
+
rules?: Pick<RegisterOptions, "deps">;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function ControlledInputField<T extends Record<string, unknown>>({
|
|
41
|
+
name,
|
|
42
|
+
label,
|
|
43
|
+
helpText,
|
|
44
|
+
rules,
|
|
45
|
+
className,
|
|
46
|
+
testId,
|
|
47
|
+
defaultChecked,
|
|
48
|
+
readOnly,
|
|
49
|
+
...props
|
|
50
|
+
}: Props<T>) {
|
|
51
|
+
const { control, t, readOnly: formReadonly } = useScmFormContext();
|
|
52
|
+
const labelTranslation = label || t(`${name}.label`) || "";
|
|
53
|
+
const helpTextTranslation = helpText || t(`${name}.helpText`);
|
|
54
|
+
return (
|
|
55
|
+
<Controller
|
|
56
|
+
control={control}
|
|
57
|
+
name={name}
|
|
58
|
+
rules={rules}
|
|
59
|
+
defaultValue={defaultChecked as never}
|
|
60
|
+
render={({ field }) => (
|
|
61
|
+
<CheckboxField
|
|
62
|
+
className={classNames("column", className)}
|
|
63
|
+
readOnly={readOnly ?? formReadonly}
|
|
64
|
+
defaultChecked={field.value}
|
|
65
|
+
{...props}
|
|
66
|
+
{...field}
|
|
67
|
+
label={labelTranslation}
|
|
68
|
+
helpText={helpTextTranslation}
|
|
69
|
+
testId={testId ?? `checkbox-${name}`}
|
|
70
|
+
/>
|
|
71
|
+
)}
|
|
72
|
+
/>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default ControlledInputField;
|
package/src/index.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 { default as Form } from "./Form";
|
|
26
|
+
export { default as ConfigurationForm } from "./ConfigurationForm";
|
|
27
|
+
export * from "./resourceHooks";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Meta, Story } from "@storybook/addon-docs";
|
|
2
|
+
import Form from "../Form";
|
|
3
|
+
import ControlledInputField from "./ControlledInputField";
|
|
4
|
+
|
|
5
|
+
<Meta
|
|
6
|
+
title="ControlledInputField"
|
|
7
|
+
decorators={[
|
|
8
|
+
(Story) => (
|
|
9
|
+
<Form onSubmit={console.log} defaultValues={{name: "Initial value"}} translationPath={["sample", "form"]}>
|
|
10
|
+
<Story />
|
|
11
|
+
</Form>
|
|
12
|
+
),
|
|
13
|
+
]}
|
|
14
|
+
/>
|
|
15
|
+
|
|
16
|
+
<Story name="Default">
|
|
17
|
+
<ControlledInputField name="name" />
|
|
18
|
+
</Story>
|
|
19
|
+
|
|
20
|
+
<Story name="WithHardcodedText">
|
|
21
|
+
<ControlledInputField name="name" label="Name" helpText="A help text" />
|
|
22
|
+
</Story>
|
|
23
|
+
|
|
24
|
+
<Story name="WithStyling">
|
|
25
|
+
<ControlledInputField name="name" className="has-background-blue-light" />
|
|
26
|
+
</Story>
|
|
27
|
+
|
|
28
|
+
<Story name="WithCheck">
|
|
29
|
+
<ControlledInputField name="name" rules={{
|
|
30
|
+
required: true
|
|
31
|
+
}} />
|
|
32
|
+
</Story>
|
|
33
|
+
|
|
34
|
+
<Story name="WithInitialFocus">
|
|
35
|
+
<ControlledInputField name="name" autoFocus={true} />
|
|
36
|
+
</Story>
|
|
@@ -0,0 +1,77 @@
|
|
|
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 classNames from "classnames";
|
|
28
|
+
import { useScmFormContext } from "../ScmFormContext";
|
|
29
|
+
import InputField from "./InputField";
|
|
30
|
+
|
|
31
|
+
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
|
+
ComponentProps<typeof InputField>,
|
|
33
|
+
"error" | "label" | "defaultChecked" | "required" | keyof ControllerRenderProps
|
|
34
|
+
> & {
|
|
35
|
+
rules?: ComponentProps<typeof Controller>["rules"];
|
|
36
|
+
name: Path<T>;
|
|
37
|
+
label?: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function ControlledInputField<T extends Record<string, unknown>>({
|
|
41
|
+
name,
|
|
42
|
+
label,
|
|
43
|
+
helpText,
|
|
44
|
+
rules,
|
|
45
|
+
className,
|
|
46
|
+
testId,
|
|
47
|
+
defaultValue,
|
|
48
|
+
readOnly,
|
|
49
|
+
...props
|
|
50
|
+
}: Props<T>) {
|
|
51
|
+
const { control, t, readOnly: formReadonly } = useScmFormContext();
|
|
52
|
+
const labelTranslation = label || t(`${name}.label`) || "";
|
|
53
|
+
const helpTextTranslation = helpText || t(`${name}.helpText`);
|
|
54
|
+
return (
|
|
55
|
+
<Controller
|
|
56
|
+
control={control}
|
|
57
|
+
name={name}
|
|
58
|
+
rules={rules}
|
|
59
|
+
defaultValue={defaultValue as never}
|
|
60
|
+
render={({ field, fieldState }) => (
|
|
61
|
+
<InputField
|
|
62
|
+
className={classNames("column", className)}
|
|
63
|
+
readOnly={readOnly ?? formReadonly}
|
|
64
|
+
required={rules?.required as boolean}
|
|
65
|
+
{...props}
|
|
66
|
+
{...field}
|
|
67
|
+
label={labelTranslation}
|
|
68
|
+
helpText={helpTextTranslation}
|
|
69
|
+
error={fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : undefined}
|
|
70
|
+
testId={testId ?? `input-${name}`}
|
|
71
|
+
/>
|
|
72
|
+
)}
|
|
73
|
+
/>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default ControlledInputField;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Meta, Story } from "@storybook/addon-docs";
|
|
2
|
+
import Form from "../Form";
|
|
3
|
+
import ControlledSecretConfirmationField from "./ControlledSecretConfirmationField";
|
|
4
|
+
|
|
5
|
+
<Meta
|
|
6
|
+
title="ControlledSecretConfirmationField"
|
|
7
|
+
decorators={[
|
|
8
|
+
(Story) => (
|
|
9
|
+
<Form onSubmit={console.log} defaultValues={{ password: "", passwordConfirmation: "" }} translationPath={["sample", "form"]}>
|
|
10
|
+
<Story />
|
|
11
|
+
</Form>
|
|
12
|
+
),
|
|
13
|
+
]}
|
|
14
|
+
/>
|
|
15
|
+
|
|
16
|
+
<Story name="Default">
|
|
17
|
+
<ControlledSecretConfirmationField name="password" />
|
|
18
|
+
</Story>
|
|
19
|
+
|
|
20
|
+
<Story name="WithHardcodedText">
|
|
21
|
+
<ControlledSecretConfirmationField name="password" label="Password" confirmationLabel="Password Confirmation" helpText="A help text" confirmationHelpText="Another help text" />
|
|
22
|
+
</Story>
|
|
23
|
+
|
|
24
|
+
<Story name="WithStyling">
|
|
25
|
+
<ControlledSecretConfirmationField name="password" className="has-background-blue-light" />
|
|
26
|
+
</Story>
|
|
27
|
+
|
|
28
|
+
<Story name="WithCheck">
|
|
29
|
+
<ControlledSecretConfirmationField
|
|
30
|
+
name="password"
|
|
31
|
+
rules={{
|
|
32
|
+
required: true,
|
|
33
|
+
}}
|
|
34
|
+
/>
|
|
35
|
+
</Story>
|
|
36
|
+
|
|
37
|
+
<Story name="WithInitialFocus">
|
|
38
|
+
<ControlledSecretConfirmationField name="password" autoFocus={true} />
|
|
39
|
+
</Story>
|
|
@@ -0,0 +1,121 @@
|
|
|
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 classNames from "classnames";
|
|
28
|
+
import { useScmFormContext } from "../ScmFormContext";
|
|
29
|
+
import InputField from "./InputField";
|
|
30
|
+
|
|
31
|
+
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
|
+
ComponentProps<typeof InputField>,
|
|
33
|
+
"error" | "label" | "defaultChecked" | "required" | keyof ControllerRenderProps
|
|
34
|
+
> & {
|
|
35
|
+
rules?: ComponentProps<typeof Controller>["rules"];
|
|
36
|
+
name: Path<T>;
|
|
37
|
+
label?: string;
|
|
38
|
+
confirmationLabel?: string;
|
|
39
|
+
confirmationHelpText?: string;
|
|
40
|
+
confirmationErrorMessage?: string;
|
|
41
|
+
confirmationTestId?: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default function ControlledSecretConfirmationField<T extends Record<string, unknown>>({
|
|
45
|
+
name,
|
|
46
|
+
label,
|
|
47
|
+
confirmationLabel,
|
|
48
|
+
helpText,
|
|
49
|
+
confirmationHelpText,
|
|
50
|
+
rules,
|
|
51
|
+
confirmationErrorMessage,
|
|
52
|
+
className,
|
|
53
|
+
testId,
|
|
54
|
+
confirmationTestId,
|
|
55
|
+
defaultValue,
|
|
56
|
+
readOnly,
|
|
57
|
+
...props
|
|
58
|
+
}: 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);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<>
|
|
69
|
+
<Controller
|
|
70
|
+
control={control}
|
|
71
|
+
name={name}
|
|
72
|
+
defaultValue={defaultValue as never}
|
|
73
|
+
rules={{
|
|
74
|
+
...rules,
|
|
75
|
+
deps: [`${name}Confirmation`],
|
|
76
|
+
}}
|
|
77
|
+
render={({ field, fieldState }) => (
|
|
78
|
+
<InputField
|
|
79
|
+
className={classNames("column", className)}
|
|
80
|
+
readOnly={readOnly ?? formReadonly}
|
|
81
|
+
{...props}
|
|
82
|
+
{...field}
|
|
83
|
+
required={rules?.required as boolean}
|
|
84
|
+
type="password"
|
|
85
|
+
label={labelTranslation}
|
|
86
|
+
helpText={helpTextTranslation}
|
|
87
|
+
error={
|
|
88
|
+
fieldState.error ? fieldState.error.message || t(`${name}.error.${fieldState.error.type}`) : undefined
|
|
89
|
+
}
|
|
90
|
+
testId={testId ?? `input-${name}`}
|
|
91
|
+
/>
|
|
92
|
+
)}
|
|
93
|
+
/>
|
|
94
|
+
<Controller
|
|
95
|
+
control={control}
|
|
96
|
+
name={`${name}Confirmation`}
|
|
97
|
+
defaultValue={defaultValue as never}
|
|
98
|
+
render={({ field, fieldState }) => (
|
|
99
|
+
<InputField
|
|
100
|
+
className={classNames("column", className)}
|
|
101
|
+
type="password"
|
|
102
|
+
readOnly={readOnly ?? formReadonly}
|
|
103
|
+
disabled={props.disabled}
|
|
104
|
+
{...field}
|
|
105
|
+
label={confirmationLabelTranslation}
|
|
106
|
+
helpText={confirmationHelpTextTranslation}
|
|
107
|
+
error={
|
|
108
|
+
fieldState.error
|
|
109
|
+
? fieldState.error.message || t(`${name}.confirmation.error.${fieldState.error.type}`)
|
|
110
|
+
: undefined
|
|
111
|
+
}
|
|
112
|
+
testId={confirmationTestId ?? `input-${name}-confirmation`}
|
|
113
|
+
/>
|
|
114
|
+
)}
|
|
115
|
+
rules={{
|
|
116
|
+
validate: (value) => secretValue === value || confirmationErrorMessageTranslation,
|
|
117
|
+
}}
|
|
118
|
+
/>
|
|
119
|
+
</>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Meta, Story } from "@storybook/addon-docs";
|
|
2
|
+
import Input from "./Input"
|
|
3
|
+
|
|
4
|
+
<Meta title="Input" />
|
|
5
|
+
|
|
6
|
+
This will be our latest input component
|
|
7
|
+
|
|
8
|
+
<Story name="Default">
|
|
9
|
+
<Input />
|
|
10
|
+
</Story>
|
|
11
|
+
|
|
12
|
+
<Story name="With Variant">
|
|
13
|
+
<Input variant="danger" />
|
|
14
|
+
</Story>
|
|
15
|
+
|
|
16
|
+
<Story name="With Custom Class">
|
|
17
|
+
<Input className="is-warning" />
|
|
18
|
+
</Story>
|
|
19
|
+
|
|
20
|
+
<Story name="With Ref">
|
|
21
|
+
<Input ref={r => {r.focus()}} />
|
|
22
|
+
</Story>
|
|
@@ -0,0 +1,46 @@
|
|
|
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, { InputHTMLAttributes } from "react";
|
|
26
|
+
import classNames from "classnames";
|
|
27
|
+
import { createVariantClass, Variant } from "../variants";
|
|
28
|
+
import { createAttributesForTesting } from "@scm-manager/ui-components";
|
|
29
|
+
|
|
30
|
+
type Props = {
|
|
31
|
+
variant?: Variant;
|
|
32
|
+
testId?: string;
|
|
33
|
+
} & InputHTMLAttributes<HTMLInputElement>;
|
|
34
|
+
|
|
35
|
+
const Input = React.forwardRef<HTMLInputElement, Props>(({ variant, className, testId, ...props }, ref) => {
|
|
36
|
+
return (
|
|
37
|
+
<input
|
|
38
|
+
ref={ref}
|
|
39
|
+
className={classNames("input", createVariantClass(variant), className)}
|
|
40
|
+
{...props}
|
|
41
|
+
{...createAttributesForTesting(testId)}
|
|
42
|
+
/>
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export default Input;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Meta, Story } from "@storybook/addon-docs";
|
|
2
|
+
import InputField from "./InputField";
|
|
3
|
+
|
|
4
|
+
<Meta title="InputField" />
|
|
5
|
+
|
|
6
|
+
This will be our first form field molecule
|
|
7
|
+
|
|
8
|
+
<Story name="Default">
|
|
9
|
+
<InputField label="MyInput" />
|
|
10
|
+
</Story>
|
|
11
|
+
|
|
12
|
+
<Story name="WithHelp">
|
|
13
|
+
<InputField label="MyInput" helpText="You can do all sorts of things with this input" />
|
|
14
|
+
</Story>
|
|
15
|
+
|
|
16
|
+
<Story name="WithError">
|
|
17
|
+
<InputField label="MyInput" error="This field is super required" />
|
|
18
|
+
</Story>
|
|
19
|
+
|
|
20
|
+
<Story name="WithWidth">
|
|
21
|
+
<InputField label="MyInput" className="column is-half" />
|
|
22
|
+
</Story>
|