@scm-manager/ui-forms 2.47.0 → 2.48.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@scm-manager/ui-forms",
3
3
  "private": false,
4
- "version": "2.47.0",
4
+ "version": "2.48.0",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
7
7
  "module": "build/index.mjs",
@@ -16,8 +16,9 @@
16
16
  "@scm-manager/eslint-config": "^2.16.0",
17
17
  "@scm-manager/prettier-config": "^2.10.1",
18
18
  "@scm-manager/tsconfig": "^2.13.0",
19
- "@scm-manager/ui-styles": "2.47.0",
19
+ "@scm-manager/ui-styles": "2.48.0",
20
20
  "@storybook/addon-actions": "^6.5.10",
21
+ "@storybook/addon-docs": "^6.5.14",
21
22
  "@storybook/addon-essentials": "^6.5.10",
22
23
  "@storybook/addon-interactions": "^6.5.10",
23
24
  "@storybook/addon-links": "^6.5.10",
@@ -25,14 +26,13 @@
25
26
  "@storybook/manager-webpack5": "^6.5.10",
26
27
  "@storybook/react": "^6.5.10",
27
28
  "@storybook/testing-library": "^0.0.13",
28
- "@storybook/addon-docs": "^6.5.14",
29
29
  "babel-loader": "^8.2.5",
30
30
  "storybook-addon-mock": "^3.2.0",
31
31
  "storybook-addon-themes": "^6.1.0",
32
32
  "tsup": "^6.2.3"
33
33
  },
34
34
  "peerDependencies": {
35
- "@scm-manager/ui-components": "2.47.0",
35
+ "@scm-manager/ui-components": "2.48.0",
36
36
  "classnames": "^2.3.1",
37
37
  "react": "17",
38
38
  "react-hook-form": "7",
@@ -41,12 +41,13 @@
41
41
  "styled-components": "5"
42
42
  },
43
43
  "dependencies": {
44
- "@scm-manager/ui-buttons": "2.47.0",
45
- "@scm-manager/ui-overlays": "2.47.0",
46
- "@scm-manager/ui-api": "2.47.0",
47
44
  "@headlessui/react": "^1.7.15",
45
+ "@radix-ui/react-radio-group": "^1.1.3",
48
46
  "@radix-ui/react-slot": "^1.0.1",
49
- "@radix-ui/react-visually-hidden": "^1.0.3"
47
+ "@radix-ui/react-visually-hidden": "^1.0.3",
48
+ "@scm-manager/ui-api": "2.48.0",
49
+ "@scm-manager/ui-buttons": "2.48.0",
50
+ "@scm-manager/ui-overlays": "2.48.0"
50
51
  },
51
52
  "prettier": "@scm-manager/prettier-config",
52
53
  "eslintConfig": {
@@ -40,6 +40,7 @@ import ControlledChipInputField from "./chip-input/ControlledChipInputField";
40
40
  import Combobox from "./combobox/Combobox";
41
41
  import ControlledComboboxField from "./combobox/ControlledComboboxField";
42
42
  import { defaultOptionFactory } from "./helpers";
43
+ import ChipInput from "./headless-chip-input/ChipInput";
43
44
 
44
45
  export type SimpleWebHookConfiguration = {
45
46
  urlPattern: string;
@@ -430,4 +431,23 @@ storiesOf("Forms", module)
430
431
  </ControlledList>
431
432
  </ScmFormListContextProvider>
432
433
  </Form>
433
- ));
434
+ ))
435
+ .add("Controlled Chip Input with add", () => {
436
+ const ref = useRef<HTMLInputElement>(null);
437
+ return (
438
+ <Form
439
+ onSubmit={console.log}
440
+ translationPath={["sample", "form"]}
441
+ defaultValues={{
442
+ branches: ["main", "develop"],
443
+ }}
444
+ >
445
+ <FormRow>
446
+ <ControlledChipInputField name="branches" ref={ref} />
447
+ </FormRow>
448
+ <FormRow>
449
+ <ChipInput.AddButton inputRef={ref}>Add</ChipInput.AddButton>
450
+ </FormRow>
451
+ </Form>
452
+ );
453
+ });
@@ -22,13 +22,13 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
- import React, { FC, HTMLProps } from "react";
25
+ import React, { HTMLProps } from "react";
26
26
  import classNames from "classnames";
27
27
 
28
- const Control: FC<HTMLProps<HTMLDivElement>> = ({ className, children, ...rest }) => (
29
- <div className={classNames("control", className)} {...rest}>
28
+ const Control = React.forwardRef<HTMLDivElement, HTMLProps<HTMLDivElement>>(({ className, children, ...rest }, ref) => (
29
+ <div className={classNames("control", className)} {...rest} ref={ref}>
30
30
  {children}
31
31
  </div>
32
- );
32
+ ));
33
33
 
34
34
  export default Control;
@@ -25,9 +25,11 @@
25
25
  import React, { FC, HTMLProps } from "react";
26
26
  import classNames from "classnames";
27
27
 
28
- const Field: FC<HTMLProps<HTMLDivElement>> = ({ className, children, ...rest }) => (
29
- <div className={classNames("field", className)} {...rest}>
30
- {children}
31
- </div>
32
- );
28
+ const Field: FC<HTMLProps<HTMLDivElement> | ({ as: keyof JSX.IntrinsicElements } & HTMLProps<HTMLElement>)> = ({
29
+ as = "div",
30
+ className,
31
+ children,
32
+ ...rest
33
+ }) => React.createElement(as, { className: classNames("field", className), ...rest }, children);
34
+
33
35
  export default Field;
@@ -25,9 +25,11 @@
25
25
  import React, { FC, HTMLProps } from "react";
26
26
  import classNames from "classnames";
27
27
 
28
- const Label: FC<HTMLProps<HTMLLabelElement>> = ({ className, children, ...rest }) => (
29
- <label className={classNames("label", className)} {...rest}>
30
- {children}
31
- </label>
32
- );
28
+ const Label: FC<HTMLProps<HTMLLabelElement> | ({ as: keyof JSX.IntrinsicElements } & HTMLProps<HTMLElement>)> = ({
29
+ as = "label",
30
+ className,
31
+ children,
32
+ ...rest
33
+ }) => React.createElement(as, { className: classNames("label", className), ...rest }, children);
34
+
33
35
  export default Label;
@@ -26,7 +26,7 @@ import React, { ComponentProps } from "react";
26
26
  import { Controller, ControllerRenderProps, Path } from "react-hook-form";
27
27
  import { useScmFormContext } from "../ScmFormContext";
28
28
  import { useScmFormPathContext } from "../FormPathContext";
29
- import { defaultOptionFactory, prefixWithoutIndices } from "../helpers";
29
+ import { defaultOptionFactory, prefixWithoutIndices, withForwardRef } from "../helpers";
30
30
  import classNames from "classnames";
31
31
  import ChipInputField from "./ChipInputField";
32
32
  import { Option } from "@scm-manager/ui-types";
@@ -41,27 +41,31 @@ type Props<T extends Record<string, unknown>> = Omit<
41
41
  defaultValue?: string[];
42
42
  createDeleteText?: (value: string) => string;
43
43
  optionFactory?: (val: any) => Option<unknown>;
44
+ ref?: React.ForwardedRef<HTMLInputElement>;
44
45
  };
45
46
 
46
47
  /**
47
48
  * @beta
48
49
  * @since 2.44.0
49
50
  */
50
- function ControlledChipInputField<T extends Record<string, unknown>>({
51
- name,
52
- label,
53
- helpText,
54
- rules,
55
- testId,
56
- defaultValue,
57
- readOnly,
58
- placeholder,
59
- className,
60
- createDeleteText,
61
- children,
62
- optionFactory = defaultOptionFactory,
63
- ...props
64
- }: Props<T>) {
51
+ function ControlledChipInputField<T extends Record<string, unknown>>(
52
+ {
53
+ name,
54
+ label,
55
+ helpText,
56
+ rules,
57
+ testId,
58
+ defaultValue,
59
+ readOnly,
60
+ placeholder,
61
+ className,
62
+ createDeleteText,
63
+ children,
64
+ optionFactory = defaultOptionFactory,
65
+ ...props
66
+ }: Props<T>,
67
+ ref: React.ForwardedRef<HTMLInputElement>
68
+ ) {
65
69
  const { control, t, readOnly: formReadonly } = useScmFormContext();
66
70
  const formPathPrefix = useScmFormPathContext();
67
71
 
@@ -95,6 +99,7 @@ function ControlledChipInputField<T extends Record<string, unknown>>({
95
99
  : undefined
96
100
  }
97
101
  testId={testId ?? `input-${nameWithPrefix}`}
102
+ ref={ref}
98
103
  >
99
104
  {children}
100
105
  </ChipInputField>
@@ -103,4 +108,4 @@ function ControlledChipInputField<T extends Record<string, unknown>>({
103
108
  );
104
109
  }
105
110
 
106
- export default ControlledChipInputField;
111
+ export default withForwardRef(ControlledChipInputField);
package/src/index.ts CHANGED
@@ -38,6 +38,10 @@ import { ScmNestedFormPathContextProvider } from "./FormPathContext";
38
38
  import ControlledComboboxField from "./combobox/ControlledComboboxField";
39
39
  import ChipInputFieldComponent from "./chip-input/ChipInputField";
40
40
  import ChipInput from "./headless-chip-input/ChipInput";
41
+ import ControlledRadioGroupField from "./radio-button/ControlledRadioGroupField";
42
+ import RadioGroupComponent from "./radio-button/RadioGroup";
43
+ import RadioButton from "./radio-button/RadioButton";
44
+ import RadioGroupFieldComponent from "./radio-button/RadioGroupField";
41
45
 
42
46
  export { default as Field } from "./base/Field";
43
47
  export { default as Checkbox } from "./checkbox/Checkbox";
@@ -51,6 +55,13 @@ export { default as Select } from "./select/Select";
51
55
  export * from "./resourceHooks";
52
56
  export { default as Label } from "./base/label/Label";
53
57
 
58
+ const RadioGroupExport = {
59
+ Option: RadioButton,
60
+ };
61
+
62
+ export const RadioGroup = Object.assign(RadioGroupComponent, RadioGroupExport);
63
+ export const RadioGroupField = Object.assign(RadioGroupFieldComponent, RadioGroupExport);
64
+
54
65
  export const ChipInputField = Object.assign(ChipInputFieldComponent, {
55
66
  AddButton: ChipInput.AddButton,
56
67
  });
@@ -70,4 +81,5 @@ export const Form = Object.assign(FormCmp, {
70
81
  }),
71
82
  ChipInput: ControlledChipInputField,
72
83
  Combobox: ControlledComboboxField,
84
+ RadioGroup: Object.assign(ControlledRadioGroupField, RadioGroupExport),
73
85
  });
@@ -0,0 +1,94 @@
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 RadioGroupField from "./RadioGroupField";
27
+ import { Controller, ControllerRenderProps, Path } from "react-hook-form";
28
+ import { useScmFormContext } from "../ScmFormContext";
29
+ import { useScmFormPathContext } from "../FormPathContext";
30
+ import { prefixWithoutIndices } from "../helpers";
31
+ import classNames from "classnames";
32
+ import { RadioButtonContextProvider } from "./RadioButtonContext";
33
+
34
+ type Props<T extends Record<string, unknown>> = Omit<
35
+ ComponentProps<typeof RadioGroupField>,
36
+ "label" | keyof ControllerRenderProps
37
+ > & {
38
+ name: Path<T>;
39
+ rules?: ComponentProps<typeof Controller>["rules"];
40
+ label?: string;
41
+ readOnly?: boolean;
42
+ };
43
+
44
+ /**
45
+ * @beta
46
+ * @since 2.48.0
47
+ */
48
+ function ControlledRadioGroupField<T extends Record<string, unknown>>({
49
+ name,
50
+ label,
51
+ helpText,
52
+ rules,
53
+ defaultValue,
54
+ children,
55
+ fieldClassName,
56
+ readOnly,
57
+ ...props
58
+ }: Props<T>) {
59
+ const { control, t, readOnly: formReadOnly, formId } = useScmFormContext();
60
+ const formPathPrefix = useScmFormPathContext();
61
+ const nameWithPrefix = formPathPrefix ? `${formPathPrefix}.${name}` : name;
62
+ const prefixedNameWithoutIndices = prefixWithoutIndices(nameWithPrefix);
63
+ const labelTranslation = label ?? t(`${prefixedNameWithoutIndices}.label`) ?? "";
64
+ const helpTextTranslation = helpText ?? t(`${prefixedNameWithoutIndices}.helpText`);
65
+
66
+ return (
67
+ <Controller
68
+ control={control}
69
+ name={nameWithPrefix}
70
+ rules={rules}
71
+ defaultValue={defaultValue}
72
+ render={({ field }) => (
73
+ <RadioButtonContextProvider t={t} prefix={prefixedNameWithoutIndices} formId={formId}>
74
+ <RadioGroupField
75
+ defaultValue={defaultValue}
76
+ disabled={readOnly ?? formReadOnly}
77
+ required={rules?.required as boolean}
78
+ label={labelTranslation}
79
+ helpText={helpTextTranslation}
80
+ fieldClassName={classNames("column", fieldClassName)}
81
+ onValueChange={field.onChange}
82
+ {...props}
83
+ {...field}
84
+ name={nameWithPrefix}
85
+ >
86
+ {children}
87
+ </RadioGroupField>
88
+ </RadioButtonContextProvider>
89
+ )}
90
+ />
91
+ );
92
+ }
93
+
94
+ export default ControlledRadioGroupField;
@@ -0,0 +1,226 @@
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, { useState } from "react";
26
+ import { storiesOf } from "@storybook/react";
27
+ import RadioButton from "./RadioButton";
28
+ import RadioGroup from "./RadioGroup";
29
+ import RadioGroupField from "./RadioGroupField";
30
+ import Form from "../Form";
31
+ import ControlledRadioGroupField from "./ControlledRadioGroupField";
32
+ import ControlledInputField from "../input/ControlledInputField";
33
+ import FormRow from "../FormRow";
34
+ import { ScmFormListContextProvider } from "../ScmFormListContext";
35
+ import ControlledList from "../list/ControlledList";
36
+ import ControlledTable from "../table/ControlledTable";
37
+ import ControlledColumn from "../table/ControlledColumn";
38
+ import AddListEntryForm from "../AddListEntryForm";
39
+
40
+ storiesOf("Radio Group", module)
41
+ .add("Uncontrolled State", () => {
42
+ return (
43
+ <RadioGroup name="starter_pokemon">
44
+ <RadioButton value="CHARMANDER" label="Charmander" />
45
+ <RadioButton value="SQUIRTLE" label="Squirtle" />
46
+ <RadioButton value="BULBASAUR" label="Bulbasaur" helpText="Dont pick this one" />
47
+ </RadioGroup>
48
+ );
49
+ })
50
+ .add("Controlled State", () => {
51
+ const [value, setValue] = useState<string | undefined>(undefined);
52
+ return (
53
+ <RadioGroup name="starter_pokemon" value={value} onValueChange={setValue}>
54
+ <RadioButton value="CHARMANDER" label="Charmander" />
55
+ <RadioButton value="SQUIRTLE" label="Squirtle" />
56
+ <RadioButton value="BULBASAUR" label="Bulbasaur" helpText="Dont pick this one" />
57
+ </RadioGroup>
58
+ );
59
+ })
60
+ .add("Radio Group Field", () => {
61
+ const [value, setValue] = useState<string | undefined>(undefined);
62
+ return (
63
+ <RadioGroupField
64
+ label="Choose your starter"
65
+ helpText="Choose wisely"
66
+ name="starter_pokemon"
67
+ value={value}
68
+ onValueChange={setValue}
69
+ >
70
+ <RadioButton value="CHARMANDER" label="Charmander" />
71
+ <RadioButton value="SQUIRTLE" label="Squirtle" />
72
+ <RadioButton value="BULBASAUR" label="Bulbasaur" helpText="Dont pick this one" />
73
+ </RadioGroupField>
74
+ );
75
+ })
76
+ .add("Controlled Radio Group Field", () => (
77
+ <Form
78
+ // eslint-disable-next-line no-console
79
+ onSubmit={console.log}
80
+ translationPath={["sample", "form"]}
81
+ defaultValues={{ name: "", starter_pokemon: null }}
82
+ >
83
+ <FormRow>
84
+ <ControlledInputField name="name" />
85
+ </FormRow>
86
+ <FormRow>
87
+ <ControlledRadioGroupField
88
+ name="starter_pokemon"
89
+ label="Starter Pokemon"
90
+ helpText="Choose wisely"
91
+ rules={{ required: true }}
92
+ >
93
+ <RadioButton value="CHARMANDER" label="Charmander" labelClassName="is-block" />
94
+ <RadioButton value="SQUIRTLE" label="Squirtle" />
95
+ <RadioButton value="BULBASAUR" label="Bulbasaur" helpText="Dont pick this one" />
96
+ </ControlledRadioGroupField>
97
+ </FormRow>
98
+ </Form>
99
+ ))
100
+ .add("Readonly Controlled Radio Group Field", () => (
101
+ <Form
102
+ // eslint-disable-next-line no-console
103
+ onSubmit={console.log}
104
+ translationPath={["sample", "form"]}
105
+ defaultValues={{ name: "Red", starter_pokemon: "SQUIRTLE" }}
106
+ >
107
+ <FormRow>
108
+ <ControlledInputField name="name" />
109
+ </FormRow>
110
+ <FormRow>
111
+ <ControlledRadioGroupField
112
+ name="starter_pokemon"
113
+ label="Starter Pokemon"
114
+ helpText="Choose wisely"
115
+ readOnly={true}
116
+ >
117
+ <RadioButton value="CHARMANDER" label="Charmander" labelClassName="is-block" />
118
+ <RadioButton value="SQUIRTLE" label="Squirtle" />
119
+ <RadioButton value="BULBASAUR" label="Bulbasaur" helpText="Dont pick this one" />
120
+ </ControlledRadioGroupField>
121
+ </FormRow>
122
+ </Form>
123
+ ))
124
+ .add("With options", () => (
125
+ <Form
126
+ // eslint-disable-next-line no-console
127
+ onSubmit={console.log}
128
+ translationPath={["sample", "form"]}
129
+ defaultValues={{ starter_pokemon: "CHARMANDER" }}
130
+ >
131
+ <FormRow>
132
+ <ControlledRadioGroupField
133
+ name="starter_pokemon"
134
+ label="Starter Pokemon"
135
+ helpText="Choose wisely"
136
+ rules={{ required: true }}
137
+ options={[
138
+ { value: "CHARMANDER", label: "Charmander" },
139
+ { value: "SQUIRTLE", label: "Squirtle" },
140
+ { value: "BULBASAUR", label: "Bulbasaur", helpText: "Dont pick this one" },
141
+ ]}
142
+ />
143
+ </FormRow>
144
+ </Form>
145
+ ))
146
+ .add("Without label prop", () => (
147
+ <Form
148
+ // eslint-disable-next-line no-console
149
+ onSubmit={console.log}
150
+ translationPath={["sample", "form"]}
151
+ defaultValues={{ starter_pokemon: "CHARMANDER" }}
152
+ >
153
+ <FormRow>
154
+ <ControlledRadioGroupField
155
+ name="starter_pokemon"
156
+ label="Starter Pokemon"
157
+ helpText="Choose wisely"
158
+ rules={{ required: true }}
159
+ options={[
160
+ { value: "CHARMANDER" },
161
+ { value: "SQUIRTLE" },
162
+ { value: "BULBASAUR", helpText: "Dont pick this one" },
163
+ ]}
164
+ />
165
+ </FormRow>
166
+ </Form>
167
+ ))
168
+ .add("Nested", () => (
169
+ <Form
170
+ translationPath={["sample", "form"]}
171
+ // eslint-disable-next-line no-console
172
+ onSubmit={console.log}
173
+ defaultValues={{
174
+ trainers: [
175
+ {
176
+ name: "Red",
177
+ team: [{ nickname: "Charlie", pokemon: "CHARMANDER" }],
178
+ },
179
+ {
180
+ name: "Blue",
181
+ team: [{ nickname: "Squirtle", pokemon: "SQUIRTLE" }],
182
+ },
183
+ {
184
+ name: "Green",
185
+ team: [{ nickname: "Plant", pokemon: "SQUIRTLE" }],
186
+ },
187
+ ],
188
+ }}
189
+ >
190
+ <ScmFormListContextProvider name={"trainers"}>
191
+ <ControlledList withDelete>
192
+ {({ value: trainers }) => (
193
+ <>
194
+ <FormRow>
195
+ <ControlledInputField name={"name"} />
196
+ </FormRow>
197
+ <details className="has-background-dark-25 mb-2 p-2">
198
+ <summary className="is-clickable">Team</summary>
199
+ <div>
200
+ <ScmFormListContextProvider name={"team"}>
201
+ <ControlledTable withDelete>
202
+ <ControlledColumn name="nickname" />
203
+ <ControlledColumn name="pokemon" />
204
+ </ControlledTable>
205
+ <AddListEntryForm defaultValues={{ nickname: "", pokemon: null }}>
206
+ <FormRow>
207
+ <ControlledInputField name="nickname" />
208
+ </FormRow>
209
+ <FormRow>
210
+ <ControlledRadioGroupField
211
+ name="pokemon"
212
+ label="Starter Pokemon"
213
+ rules={{ required: true }}
214
+ options={[{ value: "CHARMANDER" }, { value: "SQUIRTLE" }, { value: "BULBASAUR" }]}
215
+ />
216
+ </FormRow>
217
+ </AddListEntryForm>
218
+ </ScmFormListContextProvider>
219
+ </div>
220
+ </details>
221
+ </>
222
+ )}
223
+ </ControlledList>
224
+ </ScmFormListContextProvider>
225
+ </Form>
226
+ ));
@@ -0,0 +1,116 @@
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 classNames from "classnames";
27
+ import Help from "../base/help/Help";
28
+ import * as RadioGroup from "@radix-ui/react-radio-group";
29
+ import { createAttributesForTesting, useGeneratedId } from "@scm-manager/ui-components";
30
+ import styled from "styled-components";
31
+ import { useRadioButtonContext } from "./RadioButtonContext";
32
+
33
+ const StyledRadioButton = styled(RadioGroup.Item)`
34
+ all: unset;
35
+ width: 1rem;
36
+ height: 1rem;
37
+ border: var(--scm-border);
38
+ border-radius: 100%;
39
+
40
+ :hover {
41
+ border-color: var(--scm-hover-color);
42
+ }
43
+
44
+ :hover *::after {
45
+ background-color: var(--scm-info-hover-color);
46
+ }
47
+
48
+ :disabled {
49
+ background-color: var(--scm-dark-color-25);
50
+ border-color: var(--scm-hover-color);
51
+ }
52
+
53
+ :disabled *::after {
54
+ background-color: var(--scm-info-color);
55
+ }
56
+ `;
57
+
58
+ const StyledIndicator = styled(RadioGroup.Indicator)`
59
+ display: flex;
60
+ align-items: center;
61
+ justify-content: center;
62
+ width: 100%;
63
+ height: 100%;
64
+ position: relative;
65
+
66
+ ::after {
67
+ content: "";
68
+ display: block;
69
+ width: 0.5rem;
70
+ height: 0.5rem;
71
+ border-radius: 50%;
72
+ background-color: var(--scm-info-color);
73
+ }
74
+ `;
75
+
76
+ type Props = {
77
+ value: string;
78
+ id?: string;
79
+ testId?: string;
80
+ indicatorClassName?: string;
81
+ label?: string;
82
+ labelClassName?: string;
83
+ helpText?: string;
84
+ } & ComponentProps<typeof RadioGroup.Item>;
85
+
86
+ /**
87
+ * @beta
88
+ * @since 2.48.0
89
+ */
90
+ const RadioButton = React.forwardRef<HTMLButtonElement, Props>(
91
+ ({ id, testId, indicatorClassName, label, labelClassName, className, helpText, value, ...props }, ref) => {
92
+ const context = useRadioButtonContext();
93
+ const inputId = useGeneratedId(id);
94
+ const labelKey = `${context?.prefix}.radio.${value}`;
95
+
96
+ return (
97
+ <label className={classNames("radio is-flex is-align-items-center", labelClassName)} htmlFor={inputId}>
98
+ <StyledRadioButton
99
+ form={context?.formId}
100
+ id={inputId}
101
+ value={value}
102
+ ref={ref}
103
+ className={classNames("mr-3 mt-3 mb-3", className)}
104
+ {...props}
105
+ {...createAttributesForTesting(testId)}
106
+ >
107
+ <StyledIndicator className={indicatorClassName} />
108
+ </StyledRadioButton>
109
+ {label ?? context?.t(labelKey) ?? value}
110
+ {helpText ? <Help className="ml-3" text={helpText} /> : null}
111
+ </label>
112
+ );
113
+ }
114
+ );
115
+
116
+ export default RadioButton;