Skip to content
This repository was archived by the owner on Jun 13, 2020. It is now read-only.

Commit 894faf3

Browse files
committed
Simplify code, only call onChange after releasing button
1 parent 8805a33 commit 894faf3

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

src/InputNumeric.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,56 +102,62 @@ export default class InputNumeric extends Component {
102102
}
103103
}
104104

105+
/**
106+
* Decrement the input field's value by one step (this.props.step)
107+
*/
105108
decrement() {
106-
const newValue = this.validate(this.props.value - this.props.step);
107-
if (this.props.onChange) {
108-
this.props.onChange(newValue);
109-
}
109+
const newValue = this.validate(this.state.newValue - this.props.step);
110+
this.setState({
111+
newValue
112+
});
113+
}
114+
115+
/**
116+
* Increment the input field's value by one step (this.props.step)
117+
*/
118+
increment() {
119+
const newValue = this.validate(this.state.newValue + this.props.step);
120+
this.setState({
121+
newValue
122+
});
110123
}
111124

125+
/**
126+
* Start an interval in which the input field's value is repeatedly decremented
127+
*/
112128
startDecrement() {
113129
this.decrement();
114-
this.decrementTimeout = setTimeout(() => {
115-
this.decrementInterval = setInterval(() => {
130+
this.timeout = setTimeout(() => {
131+
this.interval = setInterval(() => {
116132
this.decrement();
117133
}, this.mouseDownInterval);
118134
}, this.mouseDownDelay);
119135
}
120136

121-
stopDecrement() {
122-
if (this.decrementTimeout) {
123-
clearTimeout(this.decrementTimeout);
124-
}
125-
if (this.decrementInterval) {
126-
clearInterval(this.decrementInterval);
127-
}
128-
if (this.props.onBlur) {
129-
this.props.onBlur(this.state.newValue);
130-
}
131-
}
132-
133-
increment() {
134-
const newValue = this.validate(this.props.value + this.props.step);
135-
if (this.props.onChange) {
136-
this.props.onChange(newValue);
137-
}
138-
}
139-
137+
/**
138+
* Start an interval in which the input field's value is repeatedly incremented
139+
*/
140140
startIncrement() {
141141
this.increment();
142-
this.incrementTimeout = setTimeout(() => {
143-
this.incrementInterval = setInterval(() => {
142+
this.timeout = setTimeout(() => {
143+
this.interval = setInterval(() => {
144144
this.increment();
145145
}, this.mouseDownInterval);
146146
}, this.mouseDownDelay);
147147
}
148148

149-
stopIncrement() {
150-
if (this.incrementTimeout) {
151-
clearTimeout(this.incrementTimeout);
149+
/**
150+
* Stop the decrement/increment interval and execute the onChange() and onBlur() functions
151+
*/
152+
stop() {
153+
if (this.timeout) {
154+
clearTimeout(this.timeout);
152155
}
153-
if (this.incrementInterval) {
154-
clearInterval(this.incrementInterval);
156+
if (this.interval) {
157+
clearInterval(this.interval);
158+
}
159+
if (this.props.onChange) {
160+
this.props.onChange(this.state.newValue);
155161
}
156162
if (this.props.onBlur) {
157163
this.props.onBlur(this.state.newValue);
@@ -164,8 +170,8 @@ export default class InputNumeric extends Component {
164170
<button
165171
type="button"
166172
onMouseDown={() => this.startDecrement()}
167-
onMouseUp={() => this.stopDecrement()}
168-
onMouseLeave={() => this.stopDecrement()}
173+
onMouseUp={() => this.stop()}
174+
onMouseLeave={() => this.stop()}
169175
>
170176
171177
</button>
@@ -179,8 +185,8 @@ export default class InputNumeric extends Component {
179185
<button
180186
type="button"
181187
onMouseDown={() => this.startIncrement()}
182-
onMouseUp={() => this.stopIncrement()}
183-
onMouseLeave={() => this.stopIncrement()}
188+
onMouseUp={() => this.stop()}
189+
onMouseLeave={() => this.stop()}
184190
>
185191
+
186192
</button>

stories/InputContainer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default class InputContainer extends Component {
1515
<InputNumeric
1616
value={this.state.value}
1717
onChange={value => this.setState({ value })}
18+
min={0}
19+
max={50}
1820
/>
1921
);
2022
}

0 commit comments

Comments
 (0)