@@ -3,11 +3,14 @@ import debounce from 'lodash.debounce';
3
3
import { shouldComponentUpdate } from 'react/lib/ReactComponentWithPureRenderMixin' ;
4
4
5
5
6
+ const noop = ( ) => { } ;
7
+
8
+
6
9
export const DebounceInput = React . createClass ( {
7
10
propTypes : {
8
11
element : React . PropTypes . oneOfType ( [ React . PropTypes . string , React . PropTypes . func ] ) ,
9
12
type : React . PropTypes . string ,
10
- onChange : React . PropTypes . func . isRequired ,
13
+ onChange : React . PropTypes . func ,
11
14
onKeyDown : React . PropTypes . func ,
12
15
onBlur : React . PropTypes . func ,
13
16
value : React . PropTypes . oneOfType ( [
@@ -66,8 +69,8 @@ export const DebounceInput = React.createClass({
66
69
67
70
68
71
createNotifier ( debounceTimeout ) {
69
- if ( debounceTimeout < 0 ) {
70
- this . notify = ( ) => null ;
72
+ if ( debounceTimeout < 0 || ! this . props . hasOwnProperty ( 'onChange' ) ) {
73
+ this . notify = noop ;
71
74
} else if ( debounceTimeout === 0 ) {
72
75
this . notify = this . props . onChange ;
73
76
} else {
@@ -77,6 +80,10 @@ export const DebounceInput = React.createClass({
77
80
78
81
79
82
forceNotify ( event ) {
83
+ if ( ! this . props . hasOwnProperty ( 'onChange' ) ) {
84
+ return ;
85
+ }
86
+
80
87
if ( this . notify . cancel ) {
81
88
this . notify . cancel ( ) ;
82
89
}
@@ -95,21 +102,43 @@ export const DebounceInput = React.createClass({
95
102
onChange ( event ) {
96
103
event . persist ( ) ;
97
104
98
- const oldValue = this . state . value ;
105
+ this . oldValue = this . state . value ;
106
+ this . setState ( { value : event . target . value } , this . afterSetState ) ;
107
+ } ,
108
+
99
109
100
- this . setState ( { value : event . target . value } , ( ) => {
101
- const { value} = this . state ;
110
+ afterSetState ( ) {
111
+ const { value} = this . state ;
102
112
103
- if ( value . length >= this . props . minLength ) {
104
- this . notify ( event ) ;
105
- return ;
106
- }
113
+ if ( value . length >= this . props . minLength ) {
114
+ this . notify ( event ) ;
115
+ return ;
116
+ }
107
117
108
- // If user hits backspace and goes below minLength consider it cleaning the value
109
- if ( oldValue . length > value . length ) {
110
- this . notify ( { ...event , target : { ...event . target , value : '' } } ) ;
111
- }
112
- } ) ;
118
+ // If user hits backspace and goes below minLength consider it cleaning the value
119
+ if ( this . oldValue . length > value . length ) {
120
+ this . notify ( { ...event , target : { ...event . target , value : '' } } ) ;
121
+ }
122
+ } ,
123
+
124
+
125
+ onKeyDown ( event ) {
126
+ if ( event . key === 'Enter' ) {
127
+ this . forceNotify ( event ) ;
128
+ }
129
+ // Invoke original onKeyDown if present
130
+ if ( this . props . hasOwnProperty ( 'onKeyDown' ) ) {
131
+ this . props . onKeyDown ( event ) ;
132
+ }
133
+ } ,
134
+
135
+
136
+ onBlur ( event ) {
137
+ this . forceNotify ( event ) ;
138
+ // Invoke original onBlur if present
139
+ if ( this . props . hasOwnProperty ( 'onBlur' ) ) {
140
+ this . props . onBlur ( event ) ;
141
+ }
113
142
} ,
114
143
115
144
@@ -125,35 +154,17 @@ export const DebounceInput = React.createClass({
125
154
...props
126
155
} = this . props ;
127
156
128
- const onKeyDown = forceNotifyByEnter ? {
129
- onKeyDown : event => {
130
- if ( event . key === 'Enter' ) {
131
- this . forceNotify ( event ) ;
132
- }
133
- // Invoke original onKeyDown if present
134
- if ( this . props . onKeyDown ) {
135
- this . props . onKeyDown ( event ) ;
136
- }
137
- }
138
- } : { } ;
139
-
140
- const onBlur = forceNotifyOnBlur ? {
141
- onBlur : event => {
142
- this . forceNotify ( event ) ;
143
- // Invoke original onBlur if present
144
- if ( this . props . onBlur ) {
145
- this . props . onBlur ( event ) ;
146
- }
147
- }
148
- } : { } ;
149
-
157
+ const onChangeProp = this . props . hasOwnProperty ( 'onChange' ) ? { onChange : this . onChange } : { } ;
158
+ const valueProp = this . props . hasOwnProperty ( 'onChange' ) ? { value : this . state . value } : { } ;
159
+ const onKeyDownProp = forceNotifyByEnter ? { onKeyDown : this . onKeyDown } : { } ;
160
+ const onBlurProp = forceNotifyOnBlur ? { onBlur : this . onBlur } : { } ;
150
161
151
162
return React . createElement ( element , {
152
163
...props ,
153
- onChange : this . onChange ,
154
- value : this . state . value ,
155
- ...onKeyDown ,
156
- ...onBlur
164
+ ... valueProp ,
165
+ ... onChangeProp ,
166
+ ...onKeyDownProp ,
167
+ ...onBlurProp
157
168
} ) ;
158
169
}
159
170
} ) ;
0 commit comments