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

Commit 6b8b41e

Browse files
committed
Validate number entry.
This validates the number entry and clears it after submitting.
1 parent 489c2a8 commit 6b8b41e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

examples/simple/containers/App.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class App extends Component {
1919
e.preventDefault();
2020

2121
const { numberEntry } = this.props;
22+
this.props.dispatch( setNumberEntry( '' ) );
2223
this.props.dispatch( addNumber( numberEntry ) );
2324
}
2425

examples/simple/reducers/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ import {
1111
function numberEntry( state = '', action ) {
1212
switch ( action.type ) {
1313
case SET_NUMBER_ENTRY:
14-
return action.number;
14+
if ( '' === action.number || isNormalInteger( action.number ) ) {
15+
return action.number;
16+
} else {
17+
// Note: Could set an error state here for user validation.
18+
return state;
19+
}
1520
default:
1621
return state;
1722
}
1823
}
1924

25+
function isNormalInteger( str ) {
26+
return /^\+?(0|[1-9]\d*)$/.test( str );
27+
}
28+
2029
const primesInitialState = {
2130
primes: [],
2231
nonPrimes: [],

0 commit comments

Comments
 (0)