Skip to content

Commit ca74047

Browse files
committed
add FieldCheckbox, add FieldRadio
1 parent 8ba8a3a commit ca74047

5 files changed

Lines changed: 127 additions & 0 deletions

File tree

application/src/App.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ const App: React.FC = () => {
3030
<FormBuilder
3131
{...{
3232
inputs: {
33+
checkbox: {
34+
type: "checkbox",
35+
options: {
36+
a: { text: "checkbox a" },
37+
b: { text: "checkbox b" },
38+
c: { text: "checkbox c" }
39+
}
40+
},
41+
radio: {
42+
type: "radio",
43+
options: {
44+
a: { text: "radio a" },
45+
b: { text: "radio b" },
46+
c: { text: "radio c" }
47+
}
48+
},
49+
3350
dropdown: {
3451
type: "dropdown",
3552
placeholder: "dropdown placehoder",

application/src/components/FormBuilder.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React, { ComponentType } from "react";
22

33
// Import default inputs
4+
import { FieldCheckbox } from "./Inputs/FieldCheckbox";
45
import { FieldDropdown, IOptions } from "./Inputs/FieldDropdown";
6+
import { FieldRadio } from "./Inputs/FieldRadio";
57
import { FieldText } from "./Inputs/FieldText";
68
import { FieldTextarea } from "./Inputs/FieldTextarea";
79

@@ -30,9 +32,11 @@ interface IFormBuilder {
3032
* DATA
3133
*/
3234
const inputsComponentsDefault: Iinputs = {
35+
checkbox: FieldCheckbox,
3336
dropdown: FieldDropdown,
3437
number: FieldText,
3538
password: FieldText,
39+
radio: FieldRadio,
3640
select: FieldDropdown,
3741
text: FieldText,
3842
textarea: FieldTextarea
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from "react";
2+
import { IOptions } from "./FieldDropdown";
3+
4+
/**
5+
* TYPES
6+
**/
7+
type Ivalues = Array<string>;
8+
interface IFieldCheckbox {
9+
value: Ivalues;
10+
options?: IOptions;
11+
onChange: Function;
12+
}
13+
14+
/**
15+
* COMPONENT
16+
**/
17+
export const FieldCheckbox = ({
18+
value: currentValues = [],
19+
options,
20+
onChange: onChangeProps,
21+
...props
22+
}: IFieldCheckbox) => {
23+
if (!options) throw new Error("Options is missing");
24+
25+
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
26+
const value = e.target.value;
27+
28+
if (currentValues.includes(value)) {
29+
currentValues = currentValues.filter(e => e !== value);
30+
} else currentValues = [...currentValues, value];
31+
32+
return onChangeProps(e, { value: currentValues });
33+
};
34+
35+
return (
36+
<div>
37+
{Object.entries(options).map(([value, { text }]) => {
38+
return (
39+
<div {...{ key: value }}>
40+
<label>
41+
<input
42+
{...{
43+
value,
44+
onChange,
45+
checked: currentValues.includes(value),
46+
...props
47+
}}
48+
/>
49+
{text}
50+
</label>
51+
</div>
52+
);
53+
})}
54+
</div>
55+
);
56+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react";
2+
import { IOptions } from "./FieldDropdown";
3+
4+
/**
5+
* TYPES
6+
**/
7+
interface IFieldRadio {
8+
value: string;
9+
options?: IOptions;
10+
onChange: Function;
11+
}
12+
13+
/**
14+
* COMPONENT
15+
**/
16+
export const FieldRadio = ({
17+
value: currentValue,
18+
options,
19+
onChange: onChangeProps,
20+
...props
21+
}: IFieldRadio) => {
22+
if (!options) throw new Error("Options is missing");
23+
24+
const onChange = (e: React.ChangeEvent<HTMLInputElement>) =>
25+
onChangeProps(e, { value: e.target.value });
26+
27+
return (
28+
<div>
29+
{Object.entries(options).map(([value, { text }]) => {
30+
return (
31+
<div {...{ key: value }}>
32+
<label>
33+
<input
34+
{...{
35+
value,
36+
onChange,
37+
checked: currentValue === value,
38+
...props
39+
}}
40+
/>
41+
{text}
42+
</label>
43+
</div>
44+
);
45+
})}
46+
</div>
47+
);
48+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export * from "./FieldCheckbox.tsx";
12
export * from "./FieldDropdown.tsx";
3+
export * from "./FieldRadio.tsx";
24
export * from "./FieldText.tsx";
35
export * from "./FieldTextarea.tsx";

0 commit comments

Comments
 (0)