Skip to content

Commit 9e79421

Browse files
committed
Refactor up/down key handlers with simple ternary
1 parent 39ba39c commit 9e79421

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

lib/ReactTags.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,16 @@ function pressUpKey (e) {
5151
e.preventDefault()
5252

5353
// if first item, cycle to the bottom
54-
if (this.state.index <= 0) {
55-
this.setState({ index: this.state.options.length - 1 })
56-
} else {
57-
this.setState({ index: this.state.index - 1 })
58-
}
54+
const size = this.state.options.length - 1
55+
this.setState({ index: this.state.index <= 0 ? size : this.state.index - 1 })
5956
}
6057

6158
function pressDownKey (e) {
6259
e.preventDefault()
6360

6461
// if last item, cycle to top
65-
if (this.state.index >= this.state.options.length - 1) {
66-
this.setState({ index: 0 })
67-
} else {
68-
this.setState({ index: this.state.index + 1 })
69-
}
62+
const size = this.state.options.length - 1
63+
this.setState({ index: this.state.index >= size ? 0 : this.state.index + 1 })
7064
}
7165

7266
function pressBackspaceKey () {

0 commit comments

Comments
 (0)