-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.jsx
150 lines (141 loc) · 4.73 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Form from '../../src/form';
import { isAlpha, isEmail } from 'validator';
import styles from './styles.scss';
class Root extends Component {
constructor(props) {
super(props);
this.state = {
defaultValue: '',
resetForm: false,
disableInputs: false,
serverErrors: {}
};
}
componentDidMount() {
setTimeout(() => this.setState({ defaultValue: 'aruj.damian@gmail.com' }), 500);
}
_onSubmit(formData) {
console.log('Form ready to be submitted', formData);
this.setState({ disableInputs: true, serverErrors: {} });
// Disable the inputs for 5 seconds emulating an async requests.
setTimeout(() => {
const serverErrors = {};
if (formData.email == 'aruj.damian@gmail.com') {
serverErrors.email = 'This email exist, please use another one';
}
this.setState({
disableInputs: false,
serverErrors
});
}, 100);
}
_onReset() {
alert('The form was resetted');
}
_renderServerError() {
if (Object.keys(this.state.serverErrors).length) {
return (
<div className={styles.errorWrapper}>We found Errors!!</div>
);
} else {
return '';
}
}
render() {
return (
<Form
resetForm={this.state.resetForm}
formWasReset={() => this.setState({ resetForm: false })}
onSubmit={(formData) => this._onSubmit(formData)}
onReset={() => this._onReset()}
clearValuesOnReset
serverErrors={this.state.serverErrors}
disableInputs={this.state.disableInputs}
>
{this._renderServerError()}
<h2>Basic Form</h2>
<Form.Wrapper className={styles.wrapper}>
<h3>Basic Information</h3>
<Form.Input
name='name'
placeHolder='Type your name here...'
type='text'
label='Name * (this field will will start beeing validated from the moment you start typing)'
startValidatingWhenIsPristine
value='Damian'
validate={(value) => {
let valid = true;
let errorMessage = '';
if (!value.length) {
valid = false;
errorMessage = 'This field is required';
} else if (!isAlpha(value.replace(/\s/g, ''))) {
valid = false;
errorMessage = 'You must enter only characters';
}
return { valid, errorMessage };
}}
fieldClassName={styles.inputField}
/>
<Form.Input
name='lastName'
placeHolder='Type your lastName here...'
label='Last Name * (this field will will start beeing validated when the user leaves the input (onBlur))'
type='text'
value='Aruj'
validate={(value) => {
let valid = true;
let errorMessage = '';
if (!value.length) {
valid = false;
errorMessage = 'This field is required';
} else if (!isAlpha(value.replace(/\s/g, ''))) {
valid = false;
errorMessage = 'You must enter only characters';
}
return { valid, errorMessage };
}}
fieldClassName={styles.inputField}
/>
</Form.Wrapper>
<Form.Wrapper className={styles.wrapper}>
<h3>Advance Information</h3>
<Form.Input
name='email'
placeHolder='Type your email here...'
label='Email *'
type='email'
serverValue={this.state.defaultValue}
validate={(value) => {
let valid = true;
let errorMessage = '';
if (!value.length) {
valid = false;
errorMessage = 'This field is required';
} else if (!isEmail(value.replace(/\s/g, ''))) {
valid = false;
errorMessage = 'You must enter a valid email';
}
return { valid, errorMessage };
}}
fieldClassName={styles.inputField}
/>
</Form.Wrapper>
<Form.CustomSubmitButton>
<a href='#'>Custom Submit Form</a>
</Form.CustomSubmitButton>
<span> - </span>
<Form.CustomResetButton>
<a href='#'>Custom Reset Form</a>
</Form.CustomResetButton>
<span> - </span>
<Form.CustomResetButton clearValues>
<a href='#'>Custom Clear All Values Form</a>
</Form.CustomResetButton>
</Form>
);
}
}
ReactDOM.render(<Root/>, document.getElementById('reactApplication'));