mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Handle up/down arrow keys for selecting opponent
This commit is contained in:
parent
e21042c35b
commit
9534abffc8
@ -17,11 +17,20 @@ class OpponentFinder extends React.Component {
|
|||||||
foundOpponents: [],
|
foundOpponents: [],
|
||||||
selectedOpponent: "",
|
selectedOpponent: "",
|
||||||
selectedOpponentId: "",
|
selectedOpponentId: "",
|
||||||
|
focusedOpponent: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.opponentResults = [];
|
||||||
|
|
||||||
this.debouncedSearch = _.debounce(this.search.bind(this), 250);
|
this.debouncedSearch = _.debounce(this.search.bind(this), 250);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidUpdate() {
|
||||||
|
if (this.state.focusedOpponent !== null) {
|
||||||
|
this.opponentResults[this.state.focusedOpponent].focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
search() {
|
search() {
|
||||||
if (this.state.queryString != "") {
|
if (this.state.queryString != "") {
|
||||||
API.findOpponent(this.state.queryString)
|
API.findOpponent(this.state.queryString)
|
||||||
@ -50,6 +59,37 @@ class OpponentFinder extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleKeyPress(event) {
|
||||||
|
if (this.isKeyingDown(event)) { this.focusNextOpponent(); }
|
||||||
|
|
||||||
|
if (this.isKeyingUp(event)) { this.focusPreviousOpponent(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
isKeyingDown(event) {
|
||||||
|
return event.key === "ArrowDown" || (event.key === "Tab" && !event.shiftKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
isKeyingUp(event) {
|
||||||
|
return event.key === "ArrowUp" || (event.key === "Tab" && event.shiftKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
focusNextOpponent() {
|
||||||
|
if (this.state.focusedOpponent === null) {
|
||||||
|
this.setState({ focusedOpponent: 0 });
|
||||||
|
} else if (this.state.focusedOpponent < this.state.foundOpponents.length - 1) {
|
||||||
|
this.setState({ focusedOpponent: this.state.focusedOpponent + 1 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
focusPreviousOpponent() {
|
||||||
|
if (this.state.focusedOpponent > 0) {
|
||||||
|
this.setState({ focusedOpponent: this.state.focusedOpponent - 1 });
|
||||||
|
} else {
|
||||||
|
this.setState({ focusedOpponent: null });
|
||||||
|
this.queryStringInput.select();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
selectOpponent(event) {
|
selectOpponent(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
@ -63,15 +103,17 @@ class OpponentFinder extends React.Component {
|
|||||||
selectedOpponent,
|
selectedOpponent,
|
||||||
foundOpponents: [],
|
foundOpponents: [],
|
||||||
queryString: selectedOpponent.name,
|
queryString: selectedOpponent.name,
|
||||||
|
focusedOpponent: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
renderOpponents() {
|
renderOpponents() {
|
||||||
return _.map(this.state.foundOpponents, (opponent) => {
|
return _.map(this.state.foundOpponents, (opponent, index) => {
|
||||||
return (
|
return (
|
||||||
<li key={opponent.id}>
|
<li key={opponent.id}>
|
||||||
<a
|
<a
|
||||||
className="opponent-finder__result-item"
|
className="opponent-finder__result-item"
|
||||||
|
ref={(link) => { this.opponentResults[index] = link; }}
|
||||||
data-id={opponent.id}
|
data-id={opponent.id}
|
||||||
href="#"
|
href="#"
|
||||||
onClick={this.selectOpponent.bind(this)}
|
onClick={this.selectOpponent.bind(this)}
|
||||||
@ -95,10 +137,14 @@ class OpponentFinder extends React.Component {
|
|||||||
const { store, gameId } = this.props;
|
const { store, gameId } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="form-field opponent-finder">
|
<div
|
||||||
|
className="form-field opponent-finder"
|
||||||
|
onKeyUp={this.handleKeyPress.bind(this)}
|
||||||
|
>
|
||||||
<label htmlFor="query-string">Find opponent</label>
|
<label htmlFor="query-string">Find opponent</label>
|
||||||
<input
|
<input
|
||||||
id="query-string"
|
id="query-string"
|
||||||
|
ref={(input) => { this.queryStringInput = input; }}
|
||||||
name="q"
|
name="q"
|
||||||
value={this.state.queryString}
|
value={this.state.queryString}
|
||||||
onChange={this.handleChange.bind(this)}
|
onChange={this.handleChange.bind(this)}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user