Skip to content

Commit

Permalink
Fix empty text onChange call count
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Feb 25, 2017
1 parent 49e198c commit c9a1797
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-input-number",
"version": "3.1.1",
"version": "3.1.2",
"description": "input-number ui component for react(web & react-native)",
"keywords": [
"react",
Expand Down
7 changes: 3 additions & 4 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ export default {

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
const value = this.toNumber(nextProps.value);
this.setState({
inputValue: nextProps.value ? nextProps.value.toString() : nextProps.value,
value,
inputValue: nextProps.value,
value: nextProps.value,
});
}
},
Expand Down Expand Up @@ -101,7 +100,7 @@ export default {
setValue(v, callback) {
// trigger onChange
const newValue = this.isNotCompleteNumber(parseFloat(v, 10)) ? undefined : parseFloat(v, 10);
const changed = `${newValue}` !== this.state.inputValue;
const changed = newValue !== this.state.value;
if (!('value' in this.props)) {
this.setState({
value: v,
Expand Down
23 changes: 23 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('inputNumber', () => {
const container = document.createElement('div');
document.body.appendChild(container);
let onChangeFirstArgument;
let onChangeCallCount = 0;

const Component = React.createClass({
getInitialState() {
Expand All @@ -31,6 +32,7 @@ describe('inputNumber', () => {
},
onChange(value) {
onChangeFirstArgument = value;
onChangeCallCount += 1;
this.setState({ value });
},
triggerBoolean(propName) {
Expand Down Expand Up @@ -65,10 +67,12 @@ describe('inputNumber', () => {
example = ReactDOM.render(<Component />, container);
inputNumber = example.refs.inputNum;
inputElement = ReactDOM.findDOMNode(inputNumber.refs.input);
onChangeCallCount = 0;
});

afterEach(() => {
ReactDOM.unmountComponentAtNode(container);
onChangeCallCount = 0;
});

describe('keyboard works', () => {
Expand Down Expand Up @@ -567,5 +571,24 @@ describe('inputNumber', () => {
expect(inputElement.value).to.be('6');
expect(num).to.be(6);
});

it('onChange should not be called when input is not changed', () => {
Simulate.focus(inputElement);
Simulate.change(inputElement, { target: { value: '1' } });
expect(onChangeCallCount).to.be(1);
expect(onChangeFirstArgument).to.be(1);
Simulate.blur(inputElement);
expect(onChangeCallCount).to.be(1);
Simulate.focus(inputElement);
Simulate.change(inputElement, { target: { value: '' } });
expect(onChangeCallCount).to.be(2);
expect(onChangeFirstArgument).to.be('');
Simulate.blur(inputElement);
expect(onChangeCallCount).to.be(3);
expect(onChangeFirstArgument).to.be(undefined);
Simulate.focus(inputElement);
Simulate.blur(inputElement);
expect(onChangeCallCount).to.be(3);
});
});
});

0 comments on commit c9a1797

Please sign in to comment.