Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 2f5fab4

Browse files
committed
added bootstrap styling to examples, implemented registration example
1 parent ace5646 commit 2f5fab4

File tree

7 files changed

+215
-69
lines changed

7 files changed

+215
-69
lines changed

examples/Home/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import React from 'react';
33
export default class App extends React.Component {
44
render() {
55
return (
6-
<div>
7-
<h1>Home</h1>
6+
<div className="jumbotron">
7+
<h1 className="display-3">Welcome!</h1>
8+
<p className="lead">
9+
These are the examples of <a href="https://github.com/cat-react/form">@cat-react/form</a>.
10+
</p>
11+
<hr />
12+
<p>
13+
Use the navigation on the left side to take a closer look at each example.
14+
</p>
815
</div>
916
);
10-
};
17+
}
1118
}

examples/Login/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import React from 'react';
22
import Form from '@cat-react/form/Form';
3-
import TextInput from '../components/TextInput';
3+
import BasicInput from '../components/BasicInput';
44

55
export default class App extends React.Component {
66
render() {
77
return (
88
<Form>
99
<h1>Login</h1>
10-
<div>
11-
<TextInput label="Username" name="username" value="" validations={{isRequired: true}}/>
12-
</div>
13-
<div>
14-
<TextInput label="Email" name="email" value="" validations={{isEmail: true}}/>
15-
</div>
10+
<BasicInput label="Email address"
11+
name="email"
12+
type="email"
13+
value=""
14+
validations={{isRequired: true, isEmail: true}}
15+
placeholder="Enter email"/>
16+
<button type="submit" className="btn btn-primary">Login</button>
1617
</Form>
1718
);
1819
};

examples/Registration/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React from 'react';
2+
import Form from '@cat-react/form/Form';
3+
import BasicInput from '../components/BasicInput';
4+
import autoBind from 'auto-bind';
5+
6+
export default class App extends React.Component {
7+
constructor(props) {
8+
super(props);
9+
10+
autoBind(this);
11+
12+
this.state = {
13+
success: false,
14+
error: false
15+
}
16+
}
17+
18+
onSubmit(values, valid) {
19+
if (valid) {
20+
this.setState({
21+
success: true,
22+
error: false
23+
});
24+
} else {
25+
this.setState({
26+
success: false,
27+
error: true
28+
});
29+
}
30+
}
31+
32+
render() {
33+
let success = null;
34+
if (this.state.success) {
35+
success = (
36+
<div className="alert alert-success" role="alert">
37+
Success!
38+
</div>
39+
);
40+
}
41+
let error = null;
42+
if (this.state.error) {
43+
error = (
44+
<div className="alert alert-danger" role="alert">
45+
Error! Please check your inputs.
46+
</div>
47+
);
48+
}
49+
50+
return (
51+
<Form onSubmit={this.onSubmit}>
52+
<h1>Registration</h1>
53+
{success}
54+
{error}
55+
<BasicInput label="Email address"
56+
name="email"
57+
type="email"
58+
value=""
59+
validations={{isRequired: true, isEmail: true}}
60+
validationErrors={{
61+
isRequired: 'You have to enter your email address.',
62+
isEmail: 'Please enter a valid email address.'
63+
}}
64+
placeholder="Enter email"/>
65+
<BasicInput label="Confirm Email address"
66+
name="confirm_email"
67+
type="email"
68+
value=""
69+
validations={{isRequired: true, equalsField: 'email'}}
70+
validationErrors={{
71+
isRequired: 'Confirm your email address.',
72+
equalsField: 'The email addresses are not matching each other.'
73+
}}
74+
placeholder="Confirm your Email address"/>
75+
<BasicInput label="First Name"
76+
name="first_name"
77+
type="text"
78+
value=""
79+
validations={{minLength: 3}}
80+
validationErrors={{
81+
minLength: 'Your First Name has to be minimum 3 characters long.'
82+
}}
83+
placeholder="Enter your First Name"/>
84+
<button type="submit" className="btn btn-primary">Submit</button>
85+
</Form>
86+
);
87+
};
88+
}

examples/components/BasicInput.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
import Input from '@cat-react/form/Input'
3+
4+
@Input
5+
export default class BasicInput extends React.Component {
6+
onChange(event) {
7+
this.props.setValue(event.target.value);
8+
}
9+
10+
renderErrors() {
11+
let errorMessages = [];
12+
if (!this.props.isPristine()) {
13+
errorMessages = this.props.getErrorMessages();
14+
}
15+
16+
if (!errorMessages || errorMessages.length <= 0) {
17+
return null;
18+
}
19+
20+
return <ul className="errorText">{errorMessages.map((message, i) => <li key={i}>{message}</li>)}</ul>;
21+
}
22+
23+
render() {
24+
let className = 'form-control';
25+
if (!this.props.isPristine()) {
26+
className += this.props.isValid() ? '' : ' error';
27+
}
28+
29+
// TODO: remove onBlur
30+
return (
31+
<div className="form-group">
32+
<label htmlFor={this.props.name}>{this.props.label} {this.props.isRequired() ? '*' : null}</label>
33+
<input type={this.props.type}
34+
className={className}
35+
id={this.props.name}
36+
aria-describedby={this.props.name}
37+
placeholder={this.props.placeholder}
38+
value={this.props.getValue()}
39+
onChange={this.onChange.bind(this)}
40+
onBlur={this.props.onBlur} />
41+
{this.renderErrors()}
42+
</div>
43+
);
44+
}
45+
}

examples/components/TextInput.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

examples/index.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.sidebar {
2+
margin: 7px 0px;
3+
}
4+
.sidebar a {
5+
color: #337ab7;
6+
}
7+
.main {
8+
padding: 10px 5px;
9+
}
10+
.jumbotron {
11+
border-radius: 10px;
12+
padding: 10px 20px;
13+
}
14+
.error {
15+
border-color: red !important;
16+
}
17+
.errorText {
18+
color: darkred;
19+
}

examples/index.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import ReactDOM from 'react-dom';
33
import {HashRouter, Link, Route, Switch} from 'react-router-dom';
44

55
import 'bootstrap/dist/css/bootstrap.min.css';
6+
import './index.css';
67

78
import Home from './Home';
89
import Login from './Login';
10+
import Registration from './Registration';
911

1012
const menuEntries = [
1113
{
@@ -18,28 +20,54 @@ const menuEntries = [
1820
path: '/login',
1921
component: Login,
2022
text: 'Login'
23+
},
24+
{
25+
path: '/registration',
26+
component: Registration,
27+
text: 'Registration'
2128
}
2229
];
2330

31+
class App extends React.Component {
32+
render() {
33+
const currentEntry = menuEntries.find((entry) => entry.path === window.location.hash.replace('#', ''));
34+
35+
return (
36+
<div>
37+
<div className="col-md-2 sidebar">
38+
<div className="list-group">
39+
<span className="list-group-item">
40+
<a href="https://github.com/cat-react/form"><b>@cat-react/form</b></a> Examples
41+
</span>
42+
{menuEntries.map((entry, index) => {
43+
const replace = entry.path === window.location.hash.replace('#', '');
44+
return <Link key={index} className="list-group-item nav-link" to={entry.path}
45+
replace={replace}>{entry.text}</Link>;
46+
})}
47+
</div>
48+
</div>
49+
<div className="col-md-10 main">
50+
<ol className="breadcrumb">
51+
<li className="breadcrumb-item">Examples</li>
52+
<li className="breadcrumb-item active">
53+
<a href={'#' + currentEntry.path}>{currentEntry.text}</a>
54+
</li>
55+
</ol>
56+
<Switch>
57+
{menuEntries.map((entry, index) => {
58+
return <Route key={index} exact={!!entry.exact} path={entry.path}
59+
component={entry.component}/>;
60+
})}
61+
</Switch>
62+
</div>
63+
</div>
64+
);
65+
}
66+
}
67+
2468
ReactDOM.render(
2569
<HashRouter>
26-
<div>
27-
<nav>
28-
<ul>
29-
{menuEntries.map((entry, index) => {
30-
let replace = entry.path === window.location.hash.replace('#', '');
31-
return <li key={index}><Link to={entry.path} replace={replace}>{entry.text}</Link></li>;
32-
})}
33-
</ul>
34-
</nav>
35-
<main>
36-
<Switch>
37-
{menuEntries.map((entry, index) => {
38-
return <Route key={index} exact={!!entry.exact} path={entry.path} component={entry.component}/>;
39-
})}
40-
</Switch>
41-
</main>
42-
</div>
70+
<App />
4371
</HashRouter>,
4472
document.getElementById('root')
4573
);

0 commit comments

Comments
 (0)