@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,60 @@
|
|
|
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 Label from "../base/label/Label";
|
|
29
|
+
import FieldMessage from "../base/field-message/FieldMessage";
|
|
30
|
+
import Input from "./Input";
|
|
31
|
+
import Help from "../base/help/Help";
|
|
32
|
+
|
|
33
|
+
type InputFieldProps = {
|
|
34
|
+
label: string;
|
|
35
|
+
helpText?: string;
|
|
36
|
+
error?: string;
|
|
37
|
+
type?: "text" | "password" | "email" | "tel";
|
|
38
|
+
} & Omit<React.ComponentProps<typeof Input>, "type">;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @see https://bulma.io/documentation/form/input/
|
|
42
|
+
*/
|
|
43
|
+
const InputField = React.forwardRef<HTMLInputElement, InputFieldProps>(
|
|
44
|
+
({ label, helpText, error, className, ...props }, ref) => {
|
|
45
|
+
const variant = error ? "danger" : undefined;
|
|
46
|
+
return (
|
|
47
|
+
<Field className={className}>
|
|
48
|
+
<Label>
|
|
49
|
+
{label}
|
|
50
|
+
{helpText ? <Help className="ml-1" text={helpText} /> : null}
|
|
51
|
+
</Label>
|
|
52
|
+
<Control>
|
|
53
|
+
<Input variant={variant} ref={ref} {...props}></Input>
|
|
54
|
+
</Control>
|
|
55
|
+
{error ? <FieldMessage variant={variant}>{error}</FieldMessage> : null}
|
|
56
|
+
</Field>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
export default InputField;
|
|
@@ -0,0 +1,155 @@
|
|
|
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 { apiClient, requiredLink } from "@scm-manager/ui-api";
|
|
26
|
+
import { useMutation, useQueryClient } from "react-query";
|
|
27
|
+
import { HalRepresentation, Link } from "@scm-manager/ui-types";
|
|
28
|
+
|
|
29
|
+
type QueryKeyPair = [singular: string, plural: string];
|
|
30
|
+
type LinkOrHalLink = string | [entity: HalRepresentation, link: string] | HalRepresentation;
|
|
31
|
+
const unwrapLink = (input: LinkOrHalLink, linkName: string) => {
|
|
32
|
+
if (Array.isArray(input)) {
|
|
33
|
+
return requiredLink(input[0], input[1]);
|
|
34
|
+
} else if (typeof input === "string") {
|
|
35
|
+
return input;
|
|
36
|
+
} else {
|
|
37
|
+
return (input._links[linkName] as Link).href;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type MutationResult<I, O = unknown> = {
|
|
42
|
+
submit: (resource: I) => Promise<O>;
|
|
43
|
+
isLoading: boolean;
|
|
44
|
+
error: Error | null;
|
|
45
|
+
submissionResult?: O;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type MutatingResourceOptions = {
|
|
49
|
+
contentType?: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const createResource = <I, O = never>(link: string, contentType: string) => {
|
|
53
|
+
return (payload: I): Promise<O> => {
|
|
54
|
+
return apiClient
|
|
55
|
+
.post(link, payload, contentType)
|
|
56
|
+
.then((response) => {
|
|
57
|
+
const location = response.headers.get("Location");
|
|
58
|
+
if (!location) {
|
|
59
|
+
throw new Error("Server does not return required Location header");
|
|
60
|
+
}
|
|
61
|
+
return apiClient.get(location);
|
|
62
|
+
})
|
|
63
|
+
.then((response) => response.json());
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type CreateResourceOptions = MutatingResourceOptions;
|
|
68
|
+
|
|
69
|
+
/** @Beta */
|
|
70
|
+
export const useCreateResource = <I, O>(
|
|
71
|
+
link: string,
|
|
72
|
+
[entityKey, collectionName]: QueryKeyPair,
|
|
73
|
+
idFactory: (createdResource: O) => string,
|
|
74
|
+
{ contentType = "application/json" }: CreateResourceOptions = {}
|
|
75
|
+
): MutationResult<I, O> => {
|
|
76
|
+
const queryClient = useQueryClient();
|
|
77
|
+
const { mutateAsync, data, isLoading, error } = useMutation<O, Error, I>(createResource<I, O>(link, contentType), {
|
|
78
|
+
onSuccess: (result) => {
|
|
79
|
+
queryClient.setQueryData([entityKey, idFactory(result)], result);
|
|
80
|
+
return queryClient.invalidateQueries(collectionName);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
submit: (payload: I) => mutateAsync(payload),
|
|
85
|
+
isLoading,
|
|
86
|
+
error,
|
|
87
|
+
submissionResult: data,
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
type UpdateResourceOptions = MutatingResourceOptions & {
|
|
92
|
+
collectionName?: QueryKeyPair;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** @Beta */
|
|
96
|
+
export const useUpdateResource = <T>(
|
|
97
|
+
link: LinkOrHalLink,
|
|
98
|
+
idFactory: (createdResource: T) => string,
|
|
99
|
+
{
|
|
100
|
+
contentType = "application/json",
|
|
101
|
+
collectionName: [entityQueryKey, collectionName] = ["", ""],
|
|
102
|
+
}: UpdateResourceOptions = {}
|
|
103
|
+
): MutationResult<T> => {
|
|
104
|
+
const queryClient = useQueryClient();
|
|
105
|
+
const { mutateAsync, isLoading, error, data } = useMutation<unknown, Error, T>(
|
|
106
|
+
(resource) => apiClient.put(unwrapLink(link, "update"), resource, contentType),
|
|
107
|
+
{
|
|
108
|
+
onSuccess: async (_, payload) => {
|
|
109
|
+
await queryClient.invalidateQueries(entityQueryKey ? [entityQueryKey, idFactory(payload)] : idFactory(payload));
|
|
110
|
+
if (collectionName) {
|
|
111
|
+
await queryClient.invalidateQueries(collectionName);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
return {
|
|
117
|
+
submit: (resource: T) => mutateAsync(resource),
|
|
118
|
+
isLoading,
|
|
119
|
+
error,
|
|
120
|
+
submissionResult: data,
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
type DeleteResourceOptions = {
|
|
125
|
+
collectionName?: QueryKeyPair;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** @Beta */
|
|
129
|
+
export const useDeleteResource = <T extends HalRepresentation>(
|
|
130
|
+
idFactory: (createdResource: T) => string,
|
|
131
|
+
{ collectionName: [entityQueryKey, collectionName] = ["", ""] }: DeleteResourceOptions = {}
|
|
132
|
+
): MutationResult<T> => {
|
|
133
|
+
const queryClient = useQueryClient();
|
|
134
|
+
const { mutateAsync, isLoading, error, data } = useMutation<unknown, Error, T>(
|
|
135
|
+
(resource) => {
|
|
136
|
+
const deleteUrl = (resource._links.delete as Link).href;
|
|
137
|
+
return apiClient.delete(deleteUrl);
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
onSuccess: async (_, resource) => {
|
|
141
|
+
const id = idFactory(resource);
|
|
142
|
+
await queryClient.removeQueries(entityQueryKey ? [entityQueryKey, id] : id);
|
|
143
|
+
if (collectionName) {
|
|
144
|
+
await queryClient.invalidateQueries(collectionName);
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
return {
|
|
150
|
+
submit: (resource: T) => mutateAsync(resource),
|
|
151
|
+
isLoading,
|
|
152
|
+
error,
|
|
153
|
+
submissionResult: data,
|
|
154
|
+
};
|
|
155
|
+
};
|
|
@@ -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 SelectField from "./SelectField";
|
|
30
|
+
|
|
31
|
+
type Props<T extends Record<string, unknown>> = Omit<
|
|
32
|
+
ComponentProps<typeof SelectField>,
|
|
33
|
+
"error" | "label" | "required" | keyof ControllerRenderProps
|
|
34
|
+
> & {
|
|
35
|
+
rules?: ComponentProps<typeof Controller>["rules"];
|
|
36
|
+
name: Path<T>;
|
|
37
|
+
label?: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function ControlledSelectField<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
|
+
<SelectField
|
|
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 ?? `select-${name}`}
|
|
71
|
+
/>
|
|
72
|
+
)}
|
|
73
|
+
/>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default ControlledSelectField;
|
|
@@ -0,0 +1,43 @@
|
|
|
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<HTMLSelectElement>;
|
|
34
|
+
|
|
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
|
+
));
|
|
42
|
+
|
|
43
|
+
export default Select;
|
|
@@ -0,0 +1,59 @@
|
|
|
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 Label from "../base/label/Label";
|
|
29
|
+
import FieldMessage from "../base/field-message/FieldMessage";
|
|
30
|
+
import Help from "../base/help/Help";
|
|
31
|
+
import Select from "./Select";
|
|
32
|
+
|
|
33
|
+
type Props = {
|
|
34
|
+
label: string;
|
|
35
|
+
helpText?: string;
|
|
36
|
+
error?: string;
|
|
37
|
+
} & React.ComponentProps<typeof Select>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @see https://bulma.io/documentation/form/select/
|
|
41
|
+
*/
|
|
42
|
+
const SelectField = React.forwardRef<HTMLSelectElement, Props>(
|
|
43
|
+
({ label, helpText, error, className, ...props }, ref) => {
|
|
44
|
+
const variant = error ? "danger" : undefined;
|
|
45
|
+
return (
|
|
46
|
+
<Field className={className}>
|
|
47
|
+
<Label>
|
|
48
|
+
{label}
|
|
49
|
+
{helpText ? <Help className="ml-1" text={helpText} /> : null}
|
|
50
|
+
</Label>
|
|
51
|
+
<Control>
|
|
52
|
+
<Select variant={variant} ref={ref} {...props}></Select>
|
|
53
|
+
</Control>
|
|
54
|
+
{error ? <FieldMessage variant={variant}>{error}</FieldMessage> : null}
|
|
55
|
+
</Field>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
export default SelectField;
|
package/src/variants.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 const variants = ["danger"] as const;
|
|
26
|
+
export type Variant = typeof variants[number];
|
|
27
|
+
export const createVariantClass = (variant?: Variant) => (variant ? `is-${variant}` : undefined);
|
package/tsconfig.json
ADDED