@raketa-builder/next-form 0.1.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/.turbo/turbo-build.log +8 -0
- package/.turbo/turbo-lint.log +13 -0
- package/CHANGELOG.md +7 -0
- package/dist/index.js +1316 -0
- package/package.json +30 -0
- package/src/Context.js +3 -0
- package/src/Fields.js +138 -0
- package/src/Fieldset.js +60 -0
- package/src/Form.js +56 -0
- package/src/FormNavigation.js +44 -0
- package/src/Submit.js +15 -0
- package/src/index.js +15 -0
- package/src/utils/slugify.js +7 -0
- package/tsconfig.json +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@raketa-builder/next-form",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"lint": "TIMING=1 eslint \"**/*.js\"",
|
|
9
|
+
"build": "tsup src/index.js --format cjs --loader \".js=jsx\"",
|
|
10
|
+
"dev": "tsup src/index.js --format cjs --loader \".js=jsx\" --watch"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"styled-components": "^5.1.0",
|
|
14
|
+
"react": "18.2.0",
|
|
15
|
+
"react-dom": "18.2.0"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"ui": "*"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/react": "17.0.37",
|
|
22
|
+
"@types/react-dom": "17.0.11",
|
|
23
|
+
"eslint": "7.32.0",
|
|
24
|
+
"eslint-config-custom": "*",
|
|
25
|
+
"react": "18.2.0",
|
|
26
|
+
"tsconfig": "*",
|
|
27
|
+
"tsup": "6.5.0",
|
|
28
|
+
"typescript": "4.5.2"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/Context.js
ADDED
package/src/Fields.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import {
|
|
3
|
+
FormGroup,
|
|
4
|
+
Label,
|
|
5
|
+
Input,
|
|
6
|
+
Textarea,
|
|
7
|
+
Select,
|
|
8
|
+
Hint,
|
|
9
|
+
Stack,
|
|
10
|
+
} from "@raketa-builder/ui";
|
|
11
|
+
import Context from "./Context";
|
|
12
|
+
|
|
13
|
+
const HiddenInput = ({ value, name }) => {
|
|
14
|
+
if (typeof value === "string") {
|
|
15
|
+
return <Input type="hidden" defaultValue={value} name={name} />;
|
|
16
|
+
} else {
|
|
17
|
+
return (
|
|
18
|
+
<Input type="hidden" defaultValue={JSON.stringify(value)} name={name} />
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const CustomField = ({ component, name }) => {
|
|
24
|
+
const { defaultValues, errors } = React.useContext(Context);
|
|
25
|
+
const [value, setValue] = React.useState(defaultValues[name]);
|
|
26
|
+
const Component = component;
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<React.Fragment>
|
|
30
|
+
<Component onChange={(newValue) => setValue(newValue)} value={value} />
|
|
31
|
+
|
|
32
|
+
<HiddenInput name={name} value={value} />
|
|
33
|
+
|
|
34
|
+
{errors[name] && <Hint type="error">{errors[name]}</Hint>}
|
|
35
|
+
</React.Fragment>
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const CheckboxGroup = ({ name, label, hint = "", entities }) => {
|
|
40
|
+
const { defaultValues, errors } = React.useContext(Context);
|
|
41
|
+
|
|
42
|
+
let defaultChecked = [];
|
|
43
|
+
|
|
44
|
+
if (defaultValues[name]) {
|
|
45
|
+
defaultChecked = Array.isArray(defaultValues[name])
|
|
46
|
+
? defaultValues[name]
|
|
47
|
+
: [defaultValues[name]];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<FormGroup>
|
|
52
|
+
<Label htmlFor={name}>{label}</Label>
|
|
53
|
+
|
|
54
|
+
{entities.map((entity, idx) => (
|
|
55
|
+
<label key={idx}>
|
|
56
|
+
<div>
|
|
57
|
+
<input
|
|
58
|
+
type="checkbox"
|
|
59
|
+
name={name}
|
|
60
|
+
value={`${entity.value}`}
|
|
61
|
+
defaultChecked={
|
|
62
|
+
!!defaultChecked.find((dv) => dv === entity.value)
|
|
63
|
+
}
|
|
64
|
+
/>
|
|
65
|
+
|
|
66
|
+
{entity.label}
|
|
67
|
+
</div>
|
|
68
|
+
</label>
|
|
69
|
+
))}
|
|
70
|
+
|
|
71
|
+
{hint && <Hint>{hint}</Hint>}
|
|
72
|
+
{errors[name] && <Hint type="error">{errors[name]}</Hint>}
|
|
73
|
+
</FormGroup>
|
|
74
|
+
);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const Checkbox = ({ name, label, hint = "", ...rest }) => {
|
|
78
|
+
const { defaultValues, errors } = React.useContext(Context);
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<FormGroup>
|
|
82
|
+
<Label>
|
|
83
|
+
<Stack v="center">
|
|
84
|
+
<input
|
|
85
|
+
type="checkbox"
|
|
86
|
+
name={name}
|
|
87
|
+
defaultChecked={defaultValues[name]}
|
|
88
|
+
{...rest}
|
|
89
|
+
/>
|
|
90
|
+
|
|
91
|
+
{label}
|
|
92
|
+
</Stack>
|
|
93
|
+
</Label>
|
|
94
|
+
|
|
95
|
+
{hint && <Hint>{hint}</Hint>}
|
|
96
|
+
{errors[name] && <Hint type="error">{errors[name]}</Hint>}
|
|
97
|
+
</FormGroup>
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const Field = ({
|
|
102
|
+
as = "input",
|
|
103
|
+
hint = "",
|
|
104
|
+
name,
|
|
105
|
+
label,
|
|
106
|
+
children,
|
|
107
|
+
...props
|
|
108
|
+
}) => {
|
|
109
|
+
const { defaultValues, errors } = React.useContext(Context);
|
|
110
|
+
|
|
111
|
+
const types = {
|
|
112
|
+
input: Input,
|
|
113
|
+
select: Select,
|
|
114
|
+
textarea: Textarea,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const Component = types[as] || Input;
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<FormGroup>
|
|
121
|
+
{label && <Label htmlFor={name}>{label}</Label>}
|
|
122
|
+
|
|
123
|
+
<Component
|
|
124
|
+
id={name}
|
|
125
|
+
name={name}
|
|
126
|
+
defaultValue={defaultValues[name]}
|
|
127
|
+
{...props}
|
|
128
|
+
>
|
|
129
|
+
{children}
|
|
130
|
+
</Component>
|
|
131
|
+
|
|
132
|
+
{hint && <Hint>{hint}</Hint>}
|
|
133
|
+
{errors[name] && <Hint type="error">{errors[name]}</Hint>}
|
|
134
|
+
</FormGroup>
|
|
135
|
+
);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export { Field, CustomField, Checkbox, CheckboxGroup };
|
package/src/Fieldset.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import styled from "styled-components";
|
|
3
|
+
import { reset } from "@raketa-builder/ui";
|
|
4
|
+
import slugify from "./utils/slugify";
|
|
5
|
+
|
|
6
|
+
const Wrapper = styled.fieldset`
|
|
7
|
+
${reset};
|
|
8
|
+
margin: 0;
|
|
9
|
+
margin-bottom: 2em;
|
|
10
|
+
padding: 0;
|
|
11
|
+
border: 0;
|
|
12
|
+
|
|
13
|
+
@media (min-width: 1184px) {
|
|
14
|
+
display: flex;
|
|
15
|
+
gap: 1em;
|
|
16
|
+
}
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
const LegendWrapper = styled.div`
|
|
20
|
+
${reset};
|
|
21
|
+
|
|
22
|
+
@media (max-width: 1183px) {
|
|
23
|
+
margin-bottom: 1em;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@media (min-width: 1184px) {
|
|
27
|
+
width: 250px;
|
|
28
|
+
}
|
|
29
|
+
`;
|
|
30
|
+
|
|
31
|
+
const Legend = styled.legend`
|
|
32
|
+
${reset};
|
|
33
|
+
margin-bottom: 1em;
|
|
34
|
+
font-weight: 700;
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
const Description = styled.small`
|
|
38
|
+
${reset};
|
|
39
|
+
opacity: 0.5;
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
const FieldWrapper = styled.div`
|
|
43
|
+
${reset};
|
|
44
|
+
width: 100%;
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const Fieldset = ({ label, description, children }) => (
|
|
48
|
+
<Wrapper id={slugify(label)}>
|
|
49
|
+
<LegendWrapper>
|
|
50
|
+
<Legend>{label}</Legend>
|
|
51
|
+
{description && (
|
|
52
|
+
<Description dangerouslySetInnerHTML={{ __html: description }} />
|
|
53
|
+
)}
|
|
54
|
+
</LegendWrapper>
|
|
55
|
+
|
|
56
|
+
<FieldWrapper>{children}</FieldWrapper>
|
|
57
|
+
</Wrapper>
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export default Fieldset;
|
package/src/Form.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import Context from "./Context";
|
|
3
|
+
|
|
4
|
+
const Form = ({ children, defaultValues = {}, onSubmit, ...rest }) => {
|
|
5
|
+
const [state, setState] = React.useState({ submitting: false, errors: {} });
|
|
6
|
+
|
|
7
|
+
const setErrors = (errors) => {
|
|
8
|
+
let newErrors = {};
|
|
9
|
+
|
|
10
|
+
errors.map((error) => (newErrors[error.key] = error.message));
|
|
11
|
+
|
|
12
|
+
if (state.submitting) {
|
|
13
|
+
setState({
|
|
14
|
+
submitting: false,
|
|
15
|
+
errors: newErrors,
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
setState({
|
|
19
|
+
submitting: state.submitting,
|
|
20
|
+
errors: newErrors,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<form
|
|
27
|
+
onSubmit={(e) => {
|
|
28
|
+
// Note: When custom submit event is missing just act as regular form.
|
|
29
|
+
if (!onSubmit) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
|
|
35
|
+
const formData = new FormData(e.target);
|
|
36
|
+
|
|
37
|
+
setState({ ...state, submitting: true });
|
|
38
|
+
|
|
39
|
+
onSubmit(formData, setErrors);
|
|
40
|
+
}}
|
|
41
|
+
{...rest}
|
|
42
|
+
>
|
|
43
|
+
<Context.Provider
|
|
44
|
+
value={{
|
|
45
|
+
defaultValues,
|
|
46
|
+
errors: state.errors,
|
|
47
|
+
submitting: state.submitting,
|
|
48
|
+
}}
|
|
49
|
+
>
|
|
50
|
+
{children}
|
|
51
|
+
</Context.Provider>
|
|
52
|
+
</form>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export default Form;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import styled from "styled-components";
|
|
3
|
+
import { reset } from "@raketa-builder/ui";
|
|
4
|
+
import slugify from "./utils/slugify";
|
|
5
|
+
|
|
6
|
+
const Wrapper = styled.div`
|
|
7
|
+
${reset};
|
|
8
|
+
display: flex;
|
|
9
|
+
gap: 1em;
|
|
10
|
+
flex-direction: column;
|
|
11
|
+
|
|
12
|
+
@media (max-width: 1183px) {
|
|
13
|
+
margin-bottom: 2em;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@media (min-width: 1184px) {
|
|
17
|
+
position: sticky;
|
|
18
|
+
top: 80px;
|
|
19
|
+
}
|
|
20
|
+
`;
|
|
21
|
+
|
|
22
|
+
const Link = styled.a`
|
|
23
|
+
${reset};
|
|
24
|
+
color: ${(props) => props.theme.colors.primary};
|
|
25
|
+
font-weight: 500;
|
|
26
|
+
text-decoration: none;
|
|
27
|
+
|
|
28
|
+
&:hover {
|
|
29
|
+
text-decoration: underline;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
const FormNavigation = ({ sections, children }) => (
|
|
34
|
+
<Wrapper>
|
|
35
|
+
{sections.map((section, idx) => (
|
|
36
|
+
<Link key={`${section}-${idx}`} href={`#${slugify(section)}`}>
|
|
37
|
+
{section}
|
|
38
|
+
</Link>
|
|
39
|
+
))}
|
|
40
|
+
{children}
|
|
41
|
+
</Wrapper>
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
export default FormNavigation;
|
package/src/Submit.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Button } from "@raketa-builder/ui";
|
|
3
|
+
import Context from "./Context";
|
|
4
|
+
|
|
5
|
+
const Submit = ({ children }) => {
|
|
6
|
+
const { submitting } = React.useContext(Context);
|
|
7
|
+
|
|
8
|
+
return (
|
|
9
|
+
<Button disabled={submitting} type="submit" variant="primary">
|
|
10
|
+
{children}
|
|
11
|
+
</Button>
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default Submit;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Form from "./Form";
|
|
2
|
+
import { Field, CustomField, Checkbox, CheckboxGroup } from "./Fields";
|
|
3
|
+
import Submit from "./Submit";
|
|
4
|
+
import Fieldset from "./Fieldset";
|
|
5
|
+
import FormNavigation from "./FormNavigation";
|
|
6
|
+
|
|
7
|
+
Form.Field = Field;
|
|
8
|
+
Form.CustomField = CustomField;
|
|
9
|
+
Form.CheckboxGroup = CheckboxGroup;
|
|
10
|
+
Form.Checkbox = Checkbox;
|
|
11
|
+
Form.Submit = Submit;
|
|
12
|
+
Form.Fieldset = Fieldset;
|
|
13
|
+
Form.FormNavigation = FormNavigation;
|
|
14
|
+
|
|
15
|
+
export default Form;
|