@@ -3,11 +3,14 @@ import debounce from 'lodash.debounce';
33import { shouldComponentUpdate } from 'react/lib/ReactComponentWithPureRenderMixin' ;
44
55
6+ const noop = ( ) => { } ;
7+
8+
69export const DebounceInput = React . createClass ( {
710 propTypes : {
811 element : React . PropTypes . oneOfType ( [ React . PropTypes . string , React . PropTypes . func ] ) ,
912 type : React . PropTypes . string ,
10- onChange : React . PropTypes . func . isRequired ,
13+ onChange : React . PropTypes . func ,
1114 onKeyDown : React . PropTypes . func ,
1215 onBlur : React . PropTypes . func ,
1316 value : React . PropTypes . oneOfType ( [
@@ -66,8 +69,8 @@ export const DebounceInput = React.createClass({
6669
6770
6871 createNotifier ( debounceTimeout ) {
69- if ( debounceTimeout < 0 ) {
70- this . notify = ( ) => null ;
72+ if ( debounceTimeout < 0 || ! this . props . hasOwnProperty ( 'onChange' ) ) {
73+ this . notify = noop ;
7174 } else if ( debounceTimeout === 0 ) {
7275 this . notify = this . props . onChange ;
7376 } else {
@@ -77,6 +80,10 @@ export const DebounceInput = React.createClass({
7780
7881
7982 forceNotify ( event ) {
83+ if ( ! this . props . hasOwnProperty ( 'onChange' ) ) {
84+ return ;
85+ }
86+
8087 if ( this . notify . cancel ) {
8188 this . notify . cancel ( ) ;
8289 }
@@ -95,21 +102,43 @@ export const DebounceInput = React.createClass({
95102 onChange ( event ) {
96103 event . persist ( ) ;
97104
98- const oldValue = this . state . value ;
105+ this . oldValue = this . state . value ;
106+ this . setState ( { value : event . target . value } , this . afterSetState ) ;
107+ } ,
108+
99109
100- this . setState ( { value : event . target . value } , ( ) => {
101- const { value} = this . state ;
110+ afterSetState ( ) {
111+ const { value} = this . state ;
102112
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+ }
107117
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+ }
113142 } ,
114143
115144
@@ -125,35 +154,17 @@ export const DebounceInput = React.createClass({
125154 ...props
126155 } = this . props ;
127156
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 } : { } ;
150161
151162 return React . createElement ( element , {
152163 ...props ,
153- onChange : this . onChange ,
154- value : this . state . value ,
155- ...onKeyDown ,
156- ...onBlur
164+ ... valueProp ,
165+ ... onChangeProp ,
166+ ...onKeyDownProp ,
167+ ...onBlurProp
157168 } ) ;
158169 }
159170} ) ;
0 commit comments