Skip to content

Commit b5e052a

Browse files
added formik form to the components
1 parent a9c0473 commit b5e052a

File tree

3 files changed

+153
-4
lines changed

3 files changed

+153
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"build": "cross-env REACT_APP_BACKEND=true cross-env PUBLIC_URL='/light-blue-react' node scripts/build.js",
77
"start": "node scripts/start.js",
8-
"start:backend": "REACT_APP_BACKEND=true node scripts/start.js",
8+
"start:backend": "cross-env REACT_APP_BACKEND=true node scripts/start.js",
99
"test": "node scripts/test.js"
1010
},
1111
"browserslist": [
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { Formik } from 'formik';
2+
import React, { Component } from 'react';
3+
import Loader from 'components/Loader';
4+
import InputFormItem from 'components/FormItems/items/InputFormItem';
5+
import SwitchFormItem from 'components/FormItems/items/SwitchFormItem';
6+
import RadioFormItem from 'components/FormItems/items/RadioFormItem';
7+
import ImagesFormItem from 'components/FormItems/items/ImagesFormItem';
8+
import usersFields from 'components/Users/usersFields';
9+
import IniValues from 'components/FormItems/iniValues';
10+
import FormValidations from 'components/FormItems/formValidations';
11+
12+
class UsersForm extends Component {
13+
iniValues = () => {
14+
return IniValues(usersFields, this.props.record || {});
15+
}
16+
17+
formValidations = () => {
18+
return FormValidations(usersFields, this.props.record || {});
19+
}
20+
21+
handleSubmit = () => {
22+
return null;
23+
};
24+
25+
passwordSchema = {
26+
currentPassword: { type: 'string', label: 'Current Password' },
27+
newPassword: { type: 'string', label: 'New Password' },
28+
confirmNewPassword: { type: 'string', label: 'Confirm new Password' },
29+
};
30+
31+
renderForm() {
32+
const { saveLoading } = this.props;
33+
34+
return (
35+
<Formik
36+
onSubmit={this.handleSubmit}
37+
initialValues={this.iniValues()}
38+
validationSchema={this.formValidations()}
39+
render={(form) => {
40+
return (
41+
<form onSubmit={form.handleSubmit}>
42+
43+
<InputFormItem
44+
name={'firstName'}
45+
schema={usersFields}
46+
/>
47+
48+
<InputFormItem
49+
name={'lastName'}
50+
schema={usersFields}
51+
/>
52+
53+
<InputFormItem
54+
name={'phoneNumber'}
55+
schema={usersFields}
56+
/>
57+
58+
<InputFormItem
59+
name={'email'}
60+
schema={usersFields}
61+
/>
62+
63+
<InputFormItem
64+
name={'currentPassword'}
65+
password
66+
schema={this.passwordSchema}
67+
/>
68+
69+
<InputFormItem
70+
name={'newPassword'}
71+
schema={this.passwordSchema}
72+
password
73+
/>
74+
75+
<InputFormItem
76+
name={'confirmNewPassword'}
77+
schema={this.passwordSchema}
78+
password
79+
/>
80+
81+
<>
82+
<RadioFormItem
83+
name={'role'}
84+
schema={usersFields}
85+
/>
86+
87+
<SwitchFormItem
88+
name={'disabled'}
89+
schema={usersFields}
90+
/>
91+
</>
92+
93+
<ImagesFormItem
94+
name={'avatar'}
95+
schema={usersFields}
96+
path={'users/avatar'}
97+
fileProps={{
98+
size: undefined,
99+
formats: undefined,
100+
}}
101+
max={undefined}
102+
/>
103+
104+
<div className="form-buttons">
105+
<button
106+
className="btn btn-primary"
107+
disabled={saveLoading}
108+
type="button"
109+
onClick={form.handleSubmit}
110+
>
111+
Save
112+
</button>{' '}{' '}
113+
114+
<button
115+
className="btn btn-light"
116+
type="button"
117+
disabled={saveLoading}
118+
onClick={form.handleReset}
119+
>
120+
Reset
121+
</button>{' '}{' '}
122+
</div>
123+
</form>
124+
);
125+
}}
126+
/>
127+
);
128+
}
129+
130+
render() {
131+
const { isEditing, findLoading, record } = this.props;
132+
133+
if (findLoading) {
134+
return <Loader />;
135+
}
136+
137+
if (isEditing && !record) {
138+
return <Loader />;
139+
}
140+
141+
return this.renderForm();
142+
}
143+
}
144+
145+
export default UsersForm;

src/pages/forms/validation/Validation.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Formsy from 'formsy-react';
1010

1111
import InputValidation from '../../../components/InputValidation';
1212
import Widget from '../../../components/Widget';
13-
13+
import Formik from './Formik';
1414

1515
class Validation extends React.Component {
1616

@@ -21,8 +21,7 @@ class Validation extends React.Component {
2121
</h1>
2222

2323
<Row>
24-
<Col xs={0} lg={1} />
25-
<Col lg={8} xs={12}>
24+
<Col lg={{size: 8, offset: 1}} xs={{size: 12, offset: 0}}>
2625
<Widget
2726
title={<h5> Dead simple validation
2827
<small> No JS needed to tune-up</small>
@@ -175,6 +174,11 @@ class Validation extends React.Component {
175174
</Formsy.Form>
176175
</Widget>
177176
</Col>
177+
<Col lg={{size: 8, offset: 1}} xs={{size: 12, offset: 0}}>
178+
<Widget title={<h5> Dead simple formik</h5>} close collapse>
179+
<Formik />
180+
</Widget>
181+
</Col>
178182
</Row>
179183
</div>
180184
);

0 commit comments

Comments
 (0)