From 3b062c54a0c43c4ee58341211896ea52d83516d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Wed, 31 Jan 2018 09:17:26 +0100 Subject: [PATCH] Improved Select --- src/components/base/Select/index.js | 48 +++++++++++++++++------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/components/base/Select/index.js b/src/components/base/Select/index.js index 1942db1b..4d632e95 100644 --- a/src/components/base/Select/index.js +++ b/src/components/base/Select/index.js @@ -4,7 +4,6 @@ import React, { PureComponent } from 'react' import Downshift from 'downshift' import styled from 'styled-components' import { space } from 'styled-system' -import get from 'lodash/get' import type { Element } from 'react' @@ -18,18 +17,19 @@ import Text from 'components/base/Text' import Triangles from './Triangles' type Props = { + fuseOptions?: Object, + highlight?: boolean, items: Array, - value?: Object | null, itemToString?: Function, + keyProp?: string, + maxHeight?: number, onChange?: Function, - fuseOptions?: Object, - highlight?: boolean, - searchable?: boolean, placeholder?: string, renderHighlight?: string => Element<*>, - renderSelected?: any => Element<*>, renderItem?: (*) => Element<*>, - keyProp?: string, + renderSelected?: any => Element<*>, + searchable?: boolean, + value?: Object | null, } const Container = styled(Box).attrs({ relative: true, color: 'steel' })`` @@ -113,13 +113,16 @@ class Select extends PureComponent { static defaultProps = { itemToString: (item: Object) => item && item.name, keyProp: undefined, + maxHeight: 300, } _scrollToSelectedItem = true + _oldHighlightedIndex = 0 _useKeyboard = false + _children = {} renderItems = (items: Array, selectedItem: any, downshiftProps: Object) => { - const { renderItem, keyProp } = this.props + const { renderItem, maxHeight, keyProp } = this.props const { getItemProps, highlightedIndex } = downshiftProps const selectedItemIndex = items.indexOf(selectedItem) @@ -128,29 +131,36 @@ class Select extends PureComponent { {items.length ? ( { - const { contentEl } = scrollbar - const children = get(contentEl, 'children[0].children[0].children', {}) - - const currentHighlighted = children[highlightedIndex] - const currentSelectedItem = children[selectedItemIndex] + const currentHighlighted = this._children[highlightedIndex] + const currentSelectedItem = this._children[selectedItemIndex] if (this._useKeyboard && currentHighlighted) { scrollbar.scrollIntoView(currentHighlighted, { - alignToTop: false, + alignToTop: highlightedIndex < this._oldHighlightedIndex, + offsetTop: -1, + onlyScrollIfNeeded: true, }) } else if (this._scrollToSelectedItem && currentSelectedItem) { - scrollbar.scrollIntoView(currentSelectedItem, { - alignToTop: false, - }) + window.requestAnimationFrame(() => + scrollbar.scrollIntoView(currentSelectedItem, { + offsetTop: -1, + }), + ) this._scrollToSelectedItem = false } + + this._oldHighlightedIndex = highlightedIndex }} > {items.map((item, i) => ( - + (this._children[i] = n)} + {...getItemProps({ item })} + > {renderItem ? (