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

Commit 489c2a8

Browse files
committed
Add NumberPicker component.
Add the NumberPicker component and the supporting redux state. This adds a redux reducer and action to set the text field's value. And also adds the NumberPicker component.
1 parent d37963d commit 489c2a8

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const SET_NUMBER_ENTRY = 'SET_NUMBER_ENTRY';
2+
3+
export function setNumberEntry( number ) {
4+
return {
5+
type: SET_NUMBER_ENTRY,
6+
number
7+
};
8+
}
9+

examples/simple/actions/primes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const ADD_PRIME = 'ADD_PRIME';
33
export const ADD_NON_PRIME = 'ADD_PRIME';
44
export const QUEUE_NUMBER = 'QUEUE_NUMBER';
55

6-
export function testPrime( number ) {
6+
export function addNumber( number ) {
77
return {
88
type: QUEUE_NUMBER,
99
number
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { Component, PropTypes } from 'react';
2+
3+
export default class NumberPicker extends Component {
4+
render() {
5+
const { submitText, value, onChange, onSubmit } = this.props;
6+
7+
return (
8+
<span>
9+
<form onSubmit={ onSubmit }>
10+
<label>
11+
Number:
12+
<input type="text" value={ value } onChange={ onChange } />
13+
</label>
14+
<button type="submit" value="Submit">{ submitText }</button>
15+
</form>
16+
</span>
17+
);
18+
}
19+
}
20+
21+
NumberPicker.propTypes = {
22+
submitText: PropTypes.string.isRequired,
23+
value: PropTypes.string.isRequired,
24+
onChange: PropTypes.func.isRequired,
25+
onSubmit: PropTypes.func.isRequired
26+
};
27+

examples/simple/containers/App.jsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
import React, { Component, PropTypes } from 'react';
22
import { connect } from 'react-redux';
3+
import NumberPicker from '../components/NumberPicker';
4+
import { setNumberEntry } from '../actions/numberentry';
5+
import { addNumber } from '../actions/primes';
36

47
class App extends Component {
58
constructor( props ) {
69
super( props );
10+
this.handleNumberEntryChange = this.handleNumberEntryChange.bind( this );
11+
this.handleNumberEntrySubmit = this.handleNumberEntrySubmit.bind( this );
12+
}
13+
14+
handleNumberEntryChange( e ) {
15+
this.props.dispatch( setNumberEntry( e.target.value ) );
16+
}
17+
18+
handleNumberEntrySubmit( e ) {
19+
e.preventDefault();
20+
21+
const { numberEntry } = this.props;
22+
this.props.dispatch( addNumber( numberEntry ) );
723
}
824

925
render() {
26+
const submitText = "Add Number";
27+
const { numberEntry } = this.props;
28+
1029
return (
1130
<div>
12-
<h1>redux-trigger simple example</h1>
31+
<NumberPicker submitText={ submitText }
32+
value={ numberEntry }
33+
onChange={ this.handleNumberEntryChange }
34+
onSubmit={ this.handleNumberEntrySubmit } />
1335
</div>
1436
);
1537
}
1638
}
1739

1840
App.propTypes = {
41+
numberEntry: PropTypes.string.isRequired,
1942
dispatch: PropTypes.func.isRequired
2043
};
2144

2245
function mapStateToProps( state ) {
23-
return { };
46+
const { numberEntry } = state;
47+
48+
return {
49+
numberEntry
50+
};
2451
}
2552

2653
export default connect( mapStateToProps )( App );

examples/simple/reducers/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
import { combineReducers } from 'redux';
2+
import {
3+
SET_NUMBER_ENTRY
4+
} from '../actions/numberentry';
25
import {
36
ADD_PRIME,
47
ADD_NON_PRIME,
58
QUEUE_NUMBER
69
} from '../actions/primes';
710

11+
function numberEntry( state = '', action ) {
12+
switch ( action.type ) {
13+
case SET_NUMBER_ENTRY:
14+
return action.number;
15+
default:
16+
return state;
17+
}
18+
}
19+
820
const primesInitialState = {
921
primes: [],
1022
nonPrimes: [],
1123
queue: []
1224
};
1325

14-
function primesReducer( state = primesInitialState, action ) {
26+
function primes( state = primesInitialState, action ) {
1527
switch ( action.type ) {
1628
case ADD_PRIME:
1729
const primes = [ ...state.primes, action.prime ];
@@ -37,7 +49,8 @@ function primesReducer( state = primesInitialState, action ) {
3749
}
3850

3951
const rootReducer = combineReducers( {
40-
primesReducer
52+
numberEntry,
53+
primes
4154
} );
4255

4356
export default rootReducer;

0 commit comments

Comments
 (0)