Skip to content

Commit a0a5c7f

Browse files
authored
Merge pull request #59 from nkbt/Fix-bug-with-sticky-onChange-props
Fix bug with sticky on change props, rebased #34
2 parents 476bca7 + 27fc54b commit a0a5c7f

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "./node_modules/react-component-template/.eslintrc"
2+
"extends": "./node_modules/react-component-template/.eslintrc",
3+
"rules": {
4+
"react/jsx-sort-props": 0
5+
}
36
}

src/Component.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,32 @@ export const DebounceInput = React.createClass({
6969
if (debounceTimeout < 0) {
7070
this.notify = () => null;
7171
} else if (debounceTimeout === 0) {
72-
this.notify = this.props.onChange;
72+
this.notify = this.doNotify;
7373
} else {
74-
this.notify = debounce(this.props.onChange, debounceTimeout);
74+
this.notify = debounce(this.doNotify, debounceTimeout);
7575
}
7676
},
7777

7878

79+
doNotify(...args) {
80+
const {onChange} = this.props;
81+
82+
onChange(...args);
83+
},
84+
85+
7986
forceNotify(event) {
8087
if (this.notify.cancel) {
8188
this.notify.cancel();
8289
}
8390

8491
const {value} = this.state;
85-
const {minLength, onChange} = this.props;
92+
const {minLength} = this.props;
8693

8794
if (value.length >= minLength) {
88-
onChange(event);
95+
this.doNotify(event);
8996
} else {
90-
onChange({...event, target: {...event.target, value}});
97+
this.doNotify({...event, target: {...event.target, value}});
9198
}
9299
},
93100

@@ -122,27 +129,29 @@ export const DebounceInput = React.createClass({
122129
debounceTimeout: _debounceTimeout,
123130
forceNotifyByEnter,
124131
forceNotifyOnBlur,
132+
onKeyDown,
133+
onBlur,
125134
...props
126135
} = this.props;
127136

128-
const onKeyDown = forceNotifyByEnter ? {
137+
const maybeOnKeyDown = forceNotifyByEnter ? {
129138
onKeyDown: event => {
130139
if (event.key === 'Enter') {
131140
this.forceNotify(event);
132141
}
133142
// Invoke original onKeyDown if present
134-
if (this.props.onKeyDown) {
135-
this.props.onKeyDown(event);
143+
if (onKeyDown) {
144+
onKeyDown(event);
136145
}
137146
}
138147
} : {};
139148

140-
const onBlur = forceNotifyOnBlur ? {
149+
const maybeOnBlur = forceNotifyOnBlur ? {
141150
onBlur: event => {
142151
this.forceNotify(event);
143152
// Invoke original onBlur if present
144-
if (this.props.onBlur) {
145-
this.props.onBlur(event);
153+
if (onBlur) {
154+
onBlur(event);
146155
}
147156
}
148157
} : {};
@@ -152,8 +161,8 @@ export const DebounceInput = React.createClass({
152161
...props,
153162
onChange: this.onChange,
154163
value: this.state.value,
155-
...onKeyDown,
156-
...onBlur
164+
...maybeOnKeyDown,
165+
...maybeOnBlur
157166
});
158167
}
159168
});

src/example/App/UndoRedo.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,21 @@ const UndoRedo = React.createClass({
3535
},
3636

3737

38-
redo() {
38+
onRedo() {
3939
this.setValueFromHistory(this.state.historyIndex + 1);
4040
},
4141

4242

43-
undo() {
43+
onUndo() {
4444
this.setValueFromHistory(this.state.historyIndex - 1);
4545
},
4646

4747

4848
render() {
49-
const history = this.state.history.map((value, key) => (
49+
const history = this.state.history.map((value, key) =>
5050
<span className={css.item} key={key}>
5151
{key === this.state.historyIndex ? <b>"{value}"</b> : <span>"{value}"</span>}
52-
</span>
53-
));
52+
</span>);
5453

5554

5655
return (
@@ -67,11 +66,11 @@ const UndoRedo = React.createClass({
6766
</label>
6867

6968
<label className={css.label}>
70-
<button className={css.input} onClick={this.undo}>Undo</button>
69+
<button className={css.input} onClick={this.onUndo}>Undo</button>
7170
</label>
7271

7372
<label className={css.label}>
74-
<button className={css.input} onClick={this.redo}>Redo</button>
73+
<button className={css.input} onClick={this.onRedo}>Redo</button>
7574
</label>
7675
</div>
7776

src/example/App/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Textarea from './Textarea';
66
import css from './App.css';
77

88

9-
const App = () => (
9+
const App = () =>
1010
<div className={css.app}>
1111
<h1>react-debounce-input</h1>
1212
<section className={css.section}>
@@ -28,8 +28,7 @@ const App = () => (
2828
<h2>Example 4. Debounced Textarea</h2>
2929
<Textarea />
3030
</section>
31-
</div>
32-
);
31+
</div>;
3332

3433

3534
export default App;

0 commit comments

Comments
 (0)