From ae427b704438212be4a4a8bc8bacd5061f9daa12 Mon Sep 17 00:00:00 2001 From: Sasha Khamkov Date: Thu, 28 Feb 2019 20:57:35 +0200 Subject: [PATCH] FIX (arrow navigation): fix arrow navigation closes #4 --- src/components/Item.js | 76 +++++++++++++++++++++++++++--------------- src/index.js | 14 +++++++- webpack.config.js | 8 ++--- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/components/Item.js b/src/components/Item.js index 17399ab3..3f3efa5a 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,37 +1,59 @@ -import React from 'react'; +import React, { Component } from 'react'; import styled from '@emotion/styled'; import { hexToRGBA } from '../util'; +import * as PropTypes from 'prop-types'; -const Item = ({ props, state, methods, item, itemIndex }) => { - if (props.itemRenderer) { - return props.itemRenderer({ item, itemIndex, props, state, methods }); +class Item extends Component { + item = React.createRef(); + + componentDidUpdate() { + if (this.props.state.cursor === this.props.itemIndex) { + this.item.current.scrollIntoView(false); + } } - if (!props.keepSelectedInList && methods.isSelected(item)) { - return null; + render() { + const { props, state, methods, item, itemIndex } = this.props; + + if (props.itemRenderer) { + return props.itemRenderer({ item, itemIndex, props, state, methods }); + } + + if (!props.keepSelectedInList && methods.isSelected(item)) { + return null; + } + + return ( + methods.addItem(item)} + onKeyPress={item.disabled ? undefined : () => methods.addItem(item)} + color={props.color}> + {item[props.labelField]} {item.disabled && {props.disabledLabel}} + + ); } +} - return ( - methods.addItem(item)} - onKeyPress={item.disabled ? undefined : () => methods.addItem(item)} - color={props.color}> - {item[props.labelField]} {item.disabled && {props.disabledLabel}} - - ); -}; +Item.propTypes = { + props: PropTypes.any, + state: PropTypes.any, + methods: PropTypes.any, + item: PropTypes.any, + itemIndex: PropTypes.any +} const ItemComponent = styled.span` padding: 5px 10px; diff --git a/src/index.js b/src/index.js index b1668e7c..b9ed4fed 100644 --- a/src/index.js +++ b/src/index.js @@ -357,17 +357,29 @@ export class Select extends Component { } } - if (event.key === 'ArrowUp' && cursor >= 0) { + if (event.key === 'ArrowUp' && cursor > 0) { this.setState((prevState) => ({ cursor: prevState.cursor - 1 })); } + if (event.key === 'ArrowUp' && cursor === 0) { + this.setState({ + cursor: this.searchResults().length + }); + } + if (event.key === 'ArrowDown') { this.setState((prevState) => ({ cursor: prevState.cursor + 1 })); } + + if (event.key === 'ArrowDown' && this.searchResults().length === cursor) { + return this.setState({ + cursor: 0 + }); + } }; renderDropdown = () => diff --git a/webpack.config.js b/webpack.config.js index cf868b7f..485e2021 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -9,8 +9,7 @@ module.exports = { filename: 'react-dropdown-select.js', library: 'Select', libraryTarget: 'umd', - publicPath: '/', - pathinfo: true + globalObject: 'this' }, optimization: { minimizer: [ @@ -24,11 +23,10 @@ module.exports = { unsafe_proto: true, unsafe_methods: true, unsafe_comps: true, - unsafe_arrows: true, - // module: true + unsafe_arrows: true }, output: { - comments: false, + comments: false }, } }),