Skip to content

Commit cc97354

Browse files
author
Nicolas Goutay
committedFeb 27, 2016
Added TodoHeader component
1 parent 8550a95 commit cc97354

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
 

‎src/components/TodoApp.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from 'react';
22
import TodoList from './TodoList'
3+
import TodoHeader from './TodoHeader'
34

45
export default React.createClass({
56
render: function () {
67
return <div>
78
<section className="todoapp">
9+
<TodoHeader />
810
<TodoList todos={this.props.todos} filter={this.props.filter} />
911
</section>
1012
</div>

‎src/components/TodoHeader.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import PureRenderMixin from 'react-addons-pure-render-mixin';
3+
4+
export default React.createClass({
5+
mixins: [PureRenderMixin],
6+
_handleKeyPress: function(e) {
7+
if (e.key === 'Enter' && this.refs.addTodoInput.value !== '') {
8+
return this.props.addItem(
9+
this.refs.addTodoInput.value
10+
);
11+
}
12+
},
13+
render: function () {
14+
return <header className="header">
15+
<h1>todos</h1>
16+
<input className="new-todo"
17+
ref="addTodoInput"
18+
autofocus
19+
autoComplete="off"
20+
placeholder="What needs to be done?"
21+
onKeyPress = {this._handleKeyPress} />
22+
</header>
23+
}
24+
});

‎test/components/TodoHeader_spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import TestUtils from 'react-addons-test-utils';
3+
import TodoHeader from '../../src/components/TodoHeader';
4+
import {expect} from 'chai';
5+
6+
const {renderIntoDocument,
7+
scryRenderedDOMComponentsWithTag,
8+
Simulate} = TestUtils;
9+
10+
describe('TodoHeader', () => {
11+
it('calls a callback on submit', () => {
12+
var addedItem = ''
13+
const addItem = (item) => addedItem = item;
14+
const component = renderIntoDocument(
15+
<TodoHeader addItem={addItem} />
16+
);
17+
18+
const input = component.refs.addTodoInput;
19+
input.value = 'This is a new item';
20+
Simulate.change(input);
21+
Simulate.keyPress(input, {key: "Enter", keyCode: 13, which: 13});
22+
23+
expect(addedItem).to.equal('This is a new item');
24+
});
25+
26+
});

0 commit comments

Comments
 (0)
Please sign in to comment.