Skip to content

Instantly share code, notes, and snippets.

@souhe
Created January 23, 2017 14:25
Show Gist options
  • Save souhe/ed479508aca763012dc93a674929fc3d to your computer and use it in GitHub Desktop.
Save souhe/ed479508aca763012dc93a674929fc3d to your computer and use it in GitHub Desktop.
_handleKeyDown = (key: number) => {
switch (key) {
case keyCodes.up:
this._selectNewActive(x => x - 1);
break;
case keyCodes.down:
this._selectNewActive(x => x + 1);
break;
case keyCodes.center:
if (this.state.activeSelectable) {
this.state.activeSelectable.onPress();
}
break;
}
return true;
}
_selectNewActive(idxModifier: Function) {
if (this.state.activeSelectable) { // blur active Selectable
this.state.activeSelectable.onBlur();
}
const sortedSelectables = this.state.selectables.sort((a, b) => (a.y - b.y));
if (this.state.activeSelectable) { // select next Selectable
const idx = sortedSelectables.indexOf(this.state.activeSelectable);
const newIdx = idxModifier(idx || 0);
if (newIdx >= 0 && newIdx < sortedSelectables.length) {
sortedSelectables[newIdx].onFocus();
this.setState({ activeSelectable: sortedSelectables[newIdx] });
} else {
this.setState({ activeSelectable: null });
}
} else { // select first Selectable
sortedSelectables[0].onFocus();
this.setState({ activeSelectable: sortedSelectables[0] });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment