// @flow import range from 'lodash/range'; import * as React from 'react'; import SlideButton from './SlideButton'; type Props = { /** Pure function that returns a button id unique to the given value */ getButtonIdFromValue: Function, /** Pure function that returns a panel id unique to the given value */ getPanelIdFromValue: Function, /** Gets called when a slide is selected. Called with the appropriate slide index */ numOptions: number, /** The number of slides. Each is associated to an index, starting from 0 */ onSelection: Function, selectedIndex: number, }; class SlideNavigator extends React.Component { buttonElements = []; focusOnButtonElement = (index: number) => { if (index + 1 > this.buttonElements.length || index < 0) { return; } this.buttonElements[index].focus(); }; handleKeyDown = (event: SyntheticKeyboardEvent) => { const { numOptions, selectedIndex } = this.props; let nextIndex = null; switch (event.key) { case 'ArrowRight': nextIndex = (selectedIndex + 1) % numOptions; break; case 'ArrowLeft': nextIndex = (selectedIndex - 1 + numOptions) % numOptions; break; default: return; } this.handleSelection(nextIndex); event.preventDefault(); event.stopPropagation(); }; handleSelection = (index: number) => { this.focusOnButtonElement(index); this.props.onSelection(index); }; render() { const { getButtonIdFromValue, getPanelIdFromValue, numOptions, onSelection, selectedIndex } = this.props; return ( ); } } export default SlideNavigator;