Skip to content

Commit

Permalink
add modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
jscottsmith committed Nov 6, 2018
1 parent cb08a80 commit 13583a4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion _stories/molecules/SearchSelect/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SearchSelect

`<SearchSelect>` allows for external options to be provided and selected through a dropdown.
`<SearchSelect>` allows for external options to be provided and selected through a dropdown menu, such as a text input that calls an API endpoint for autocomplete options.

| Name | Default | Description | type |
| ------------ | ----------------- | ------------------------------------------------------------------------------------------------ | -------- |
Expand Down
7 changes: 3 additions & 4 deletions components/molecules/SearchSelectOptions.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import charCodes from '../../constants/charCodes';
import modulo from '../utils/modulo';

const styleReset = {
display: 'block',
Expand Down Expand Up @@ -42,16 +43,14 @@ class SearchSelectOptions extends Component {

if (NEXT.includes(event.keyCode)) {
event.preventDefault();
const nextIndex = currentIndex + 1 === options.length ? 0 : currentIndex + 1;
// console.log(nextIndex);
const nextIndex = modulo(currentIndex + 1, options.length);
this[`_option${nextIndex}`].focus();
return this.setState({ focusedIndex: nextIndex });
}

if (PREV.includes(event.keyCode)) {
event.preventDefault();
const prevIndex = currentIndex - 1 < 0 ? options.length - 1 : currentIndex - 1;
// console.log(prevIndex);
const prevIndex = modulo(currentIndex - 1, options.length);
this[`_option${prevIndex}`].focus();
return this.setState({ focusedIndex: prevIndex });
}
Expand Down
3 changes: 3 additions & 0 deletions components/utils/modulo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function modulo(a, b) {
return (a % b + b) % b;
}

0 comments on commit 13583a4

Please sign in to comment.