Skip to content

Commit 1fc2a16

Browse files
committed
onChange gets called when input content is deleted #27
1 parent b73a5af commit 1fc2a16

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

__tests__/misc.test.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe ('NumericInput/misc', function() {
4444
*
4545
* @see https://github.com/vlad-ignatov/react-numeric-input/issues/19
4646
*/
47-
it ('onChange get called properly with `min`', done => {
47+
it ('onChange gets called properly with `min`', done => {
4848
let log = []
4949
let widget = TestUtils.renderIntoDocument(
5050
<NumericInput min={100} onChange={function(...args) { log.push(...args) }}/>
@@ -99,4 +99,25 @@ describe ('NumericInput/misc', function() {
9999
done()
100100
})
101101
}
102+
103+
/**
104+
* onChange gets called when input content is deleted
105+
* @see https://github.com/vlad-ignatov/react-numeric-input/issues/27
106+
*/
107+
it ('onChange gets called when input content is deleted', done => {
108+
let log = []
109+
let widget = TestUtils.renderIntoDocument(
110+
<NumericInput min={100} value={1} onChange={(...args) => log.push(...args)}/>
111+
)
112+
let input = widget.refs.input
113+
114+
TestUtils.Simulate.focus(input)
115+
input.value = 2
116+
TestUtils.Simulate.change(input)
117+
expect(log).toEqual([2,'2'])
118+
input.value = ""
119+
TestUtils.Simulate.change(input)
120+
expect(log).toEqual([2,'2',null,""])
121+
done()
122+
})
102123
})

dist/react-numeric-input.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ return /******/ (function(modules) { // webpackBootstrap
229229
// Call the onChange if needed. This is placed here because there are
230230
// many reasons for changing the value and this is the common place
231231
// that can capture them all
232-
if (prevState.value !== this.state.value && !isNaN(this.state.value)) {
232+
if (prevState.value !== this.state.value && (!isNaN(this.state.value) || this.state.value === null)) {
233233
this._invokeEventCallback("onChange", this.state.value, this.refs.input.value);
234234
}
235235

@@ -804,7 +804,11 @@ return /******/ (function(modules) { // webpackBootstrap
804804

805805
_extends(attrs.input, {
806806
onChange: function onChange(e) {
807-
_this5.setState({ value: _this5._parse(e.target.value) });
807+
var val = _this5._parse(e.target.value);
808+
if (isNaN(val)) {
809+
val = null;
810+
}
811+
_this5.setState({ value: val });
808812
},
809813
onKeyDown: this._onKeyDown.bind(this),
810814
onInput: function onInput() {

dist/react-numeric-input.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ module.exports =
134134
}, {
135135
key: "componentDidUpdate",
136136
value: function componentDidUpdate(prevProps, prevState) {
137-
if (prevState.value !== this.state.value && !isNaN(this.state.value)) {
137+
if (prevState.value !== this.state.value && (!isNaN(this.state.value) || this.state.value === null)) {
138138
this._invokeEventCallback("onChange", this.state.value, this.refs.input.value);
139139
}
140140

@@ -568,7 +568,11 @@ module.exports =
568568

569569
_extends(attrs.input, {
570570
onChange: function onChange(e) {
571-
_this5.setState({ value: _this5._parse(e.target.value) });
571+
var val = _this5._parse(e.target.value);
572+
if (isNaN(val)) {
573+
val = null;
574+
}
575+
_this5.setState({ value: val });
572576
},
573577
onKeyDown: this._onKeyDown.bind(this),
574578
onInput: function onInput() {

src/NumericInput.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class NumericInput extends Component
357357
// Call the onChange if needed. This is placed here because there are
358358
// many reasons for changing the value and this is the common place
359359
// that can capture them all
360-
if (prevState.value !== this.state.value && !isNaN(this.state.value)) {
360+
if (prevState.value !== this.state.value && (!isNaN(this.state.value) || this.state.value === null)) {
361361
this._invokeEventCallback("onChange", this.state.value, this.refs.input.value)
362362
}
363363

@@ -909,7 +909,11 @@ class NumericInput extends Component
909909

910910
Object.assign(attrs.input, {
911911
onChange : e => {
912-
this.setState({ value: this._parse(e.target.value) })
912+
let val = this._parse(e.target.value)
913+
if (isNaN(val)) {
914+
val = null
915+
}
916+
this.setState({ value: val })
913917
},
914918
onKeyDown: this._onKeyDown.bind(this),
915919
onInput: (...args) => {

0 commit comments

Comments
 (0)