Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Goutay
committed
Feb 27, 2016
1 parent
b3a6851
commit 8550a95
Showing
4 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
import React from 'react'; | ||
import PureRenderMixin from 'react-addons-pure-render-mixin' | ||
import ReactDOM from 'react-dom'; | ||
import PureRenderMixin from 'react-addons-pure-render-mixin'; | ||
import classNames from 'classnames'; | ||
|
||
export default React.createClass({ | ||
mixins: [PureRenderMixin], | ||
getInitialState: function() { | ||
return {value: this.props.text}; | ||
}, | ||
_handleKeyDown: function(e) { | ||
switch (e.key) { | ||
case 'Enter': | ||
return this.props.doneEditing(this.props.itemId, this.state.value); | ||
case 'Escape': | ||
return this.cancelEditing(this.props.itemId); | ||
} | ||
}, | ||
_handleOnBlur: function(e) { | ||
return this.cancelEditing(this.props.itemId); | ||
}, | ||
_handleOnChange: function(e) { | ||
this.setState({value: e.target.value}); | ||
}, | ||
cancelEditing: function() { | ||
this.setState({'value': this.props.text}); | ||
return this.props.cancelEditing(this.props.itemId); | ||
}, | ||
render: function () { | ||
return <input className="edit" | ||
autoFocus={true} | ||
type="text" /> | ||
value={this.state.value} | ||
onChange={this._handleOnChange} | ||
type="text" | ||
ref="itemInput" | ||
onKeyDown={this._handleKeyDown} | ||
onBlur={this._handleOnBlur} | ||
/> | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import TestUtils from 'react-addons-test-utils'; | ||
import TextInput from '../../src/components/TextInput'; | ||
import {expect} from 'chai'; | ||
|
||
const {renderIntoDocument, | ||
scryRenderedDOMComponentsWithTag, | ||
Simulate} = TestUtils; | ||
|
||
describe('TextInput', () => { | ||
it('calls a callback when pressing enter', () => { | ||
const text = 'React'; | ||
var hasDoneEditing = false; | ||
const doneEditing = () => hasDoneEditing = true; | ||
const component = renderIntoDocument( | ||
<TextInput text={text} doneEditing={doneEditing}/> | ||
); | ||
const input = component.refs.itemInput | ||
Simulate.keyDown(input, {key: "Enter", keyCode: 13, which: 13}); | ||
|
||
expect(hasDoneEditing).to.equal(true); | ||
}); | ||
|
||
it('calls a callback when pressing escape or losing focus', () => { | ||
const text = 'React'; | ||
var hasCanceledEditing = false; | ||
const cancelEditing = () => hasCanceledEditing = true; | ||
const component = renderIntoDocument( | ||
<TextInput text={text} cancelEditing={cancelEditing}/> | ||
); | ||
const input = component.refs.itemInput | ||
Simulate.keyDown(input, {key: "Escape", keyCode: 27, which: 27}); | ||
|
||
expect(hasCanceledEditing).to.equal(true); | ||
}); | ||
}); |