Skip to content

Commit 770592d

Browse files
committed
Add front-end validation
1 parent 80479fa commit 770592d

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/components/app/ListForm.jsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react'
22

3+
var errorTimeoutID = null
4+
35
export default class ListForm extends React.Component {
46
constructor(props) {
57
super(props)
@@ -8,10 +10,39 @@ export default class ListForm extends React.Component {
810
handleCreateListItemEvent(event) {
911
event.preventDefault()
1012
const input = this.refs['item-content']
11-
this.props.createListItem(input.value)
13+
this.validates(input.value,
14+
/* Input success */
15+
() => {
16+
this.props.createListItem(input.value)
17+
},
18+
/* Input Error */
19+
(error) => {
20+
input.setAttribute('placeholder', error)
21+
input.className = 'error'
22+
if (errorTimeoutID) window.clearTimeout(errorTimeoutID)
23+
errorTimeoutID = setTimeout(() => {
24+
input.setAttribute('placeholder', '')
25+
errorTimeoutID = null
26+
input.className = ''
27+
}, 3000)
28+
}
29+
)
30+
1231
input.value = ''
1332
}
1433

34+
validates(value, success, error) {
35+
if (value === '') {
36+
error('Title cannot be empty!')
37+
} else if (/\"/.test(value)) {
38+
error('Wrong title format!')
39+
} else if (value.length > 40) {
40+
error('Maximum length: 40 characters')
41+
} else {
42+
success()
43+
}
44+
}
45+
1546
render() {
1647
return (
1748
<div className="list-form">

public/css/index.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ a { text-decoration: none; }
5959
border: none;
6060
transition: .25s;
6161
}
62+
.list-form > input.error, .list-form input.error:focus {
63+
background-color: #FEABB7;
64+
transition: .25s;
65+
}
6266
.list-form > input:focus {
6367
background-color: #eee;
6468
outline: none;

public/js/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23595,6 +23595,8 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
2359523595

2359623596
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
2359723597

23598+
var errorTimeoutID = null;
23599+
2359823600
var ListForm = function (_React$Component) {
2359923601
_inherits(ListForm, _React$Component);
2360023602

@@ -23607,11 +23609,42 @@ var ListForm = function (_React$Component) {
2360723609
_createClass(ListForm, [{
2360823610
key: 'handleCreateListItemEvent',
2360923611
value: function handleCreateListItemEvent(event) {
23612+
var _this2 = this;
23613+
2361023614
event.preventDefault();
2361123615
var input = this.refs['item-content'];
23612-
this.props.createListItem(input.value);
23616+
this.validates(input.value,
23617+
/* Input success */
23618+
function () {
23619+
_this2.props.createListItem(input.value);
23620+
},
23621+
/* Input Error */
23622+
function (error) {
23623+
input.setAttribute('placeholder', error);
23624+
input.className = 'error';
23625+
if (errorTimeoutID) window.clearTimeout(errorTimeoutID);
23626+
errorTimeoutID = setTimeout(function () {
23627+
input.setAttribute('placeholder', '');
23628+
errorTimeoutID = null;
23629+
input.className = '';
23630+
}, 3000);
23631+
});
23632+
2361323633
input.value = '';
2361423634
}
23635+
}, {
23636+
key: 'validates',
23637+
value: function validates(value, success, error) {
23638+
if (value === '') {
23639+
error('Title cannot be empty!');
23640+
} else if (/\"/.test(value)) {
23641+
error('Wrong title format!');
23642+
} else if (value.length > 40) {
23643+
error('Maximum length: 40 characters');
23644+
} else {
23645+
success();
23646+
}
23647+
}
2361523648
}, {
2361623649
key: 'render',
2361723650
value: function render() {

0 commit comments

Comments
 (0)