From 72a9db77133a37c5cbf471e3a9f369fb1a589025 Mon Sep 17 00:00:00 2001 From: Donielle Berg Date: Mon, 5 Oct 2015 16:55:06 -0700 Subject: [PATCH] Add scroll handler to scroll through menu items when they are in focus --- components/SLDSLookup/Menu/Item/index.jsx | 9 +++++++++ components/SLDSLookup/Menu/index.js | 23 ++++++++++++++++++++--- components/SLDSPicklistBase/list.jsx | 20 ++++++++++---------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/components/SLDSLookup/Menu/Item/index.jsx b/components/SLDSLookup/Menu/Item/index.jsx index 3b7f8a60c7..9843547f10 100644 --- a/components/SLDSLookup/Menu/Item/index.jsx +++ b/components/SLDSLookup/Menu/Item/index.jsx @@ -18,6 +18,7 @@ class Item extends React.Component { componentWillReceiveProps(nextProps){ if(nextProps.isActive !== this.props.isActive && (nextProps.isActive === true)){ + this.scrollFocus(); this.props.setFocus(this.props.id); } } @@ -40,6 +41,13 @@ class Item extends React.Component { return this.props.onSelect(this.props.id); } + scrollFocus(){ + const height = React.findDOMNode(this).offsetHeight; + if(height && this.props.handleItemFocus){ + this.props.handleItemFocus(this.props.index,height); + } + } + render(){ let className = 'slds-lookup__item'; let id = this.props.id; @@ -69,6 +77,7 @@ class Item extends React.Component { Item.propTypes = { id: React.PropTypes.string, setFocus: React.PropTypes.func, + scrollFocus: React.PropTypes.func, isActive: React.PropTypes.bool, onSelect: React.PropTypes.func, searchTerm: React.PropTypes.string, diff --git a/components/SLDSLookup/Menu/index.js b/components/SLDSLookup/Menu/index.js index 3d6844ec6b..ab5e7a8b90 100644 --- a/components/SLDSLookup/Menu/index.js +++ b/components/SLDSLookup/Menu/index.js @@ -27,11 +27,25 @@ class Menu extends React.Component { return this.props.filterWith(this.props.searchTerm, item); } + handleItemFocus (itemIndex, itemHeight) { + if(this.refs.list){ + React.findDOMNode(this.refs.list).scrollTop = itemIndex * itemHeight; + } + } + renderItems(){ return this.props.items.filter(this.filter, this).map((c, i) => { //isActive means it is aria-activedescendant const isActive = this.props.focusIndex === i ? true : false; - return {c} + return {c} }); } @@ -39,8 +53,11 @@ class Menu extends React.Component { return (
-
diff --git a/components/SLDSPicklistBase/list.jsx b/components/SLDSPicklistBase/list.jsx index eb427e6413..77f4a4dccf 100644 --- a/components/SLDSPicklistBase/list.jsx +++ b/components/SLDSPicklistBase/list.jsx @@ -71,7 +71,7 @@ module.exports = React.createClass({ newHighlightedIndex = this.props.options.length - 1; } else if(newHighlightedIndex >= this.props.options.length){ - newHighlightedIndex = 0; + newHighlightedIndex = 0; } if(this.props.onUpdateHighlighted){ this.props.onUpdateHighlighted(newHighlightedIndex); @@ -125,17 +125,17 @@ module.exports = React.createClass({ getItems () { return this.props.options.map((option, index) =>{ return ( - @@ -145,17 +145,17 @@ module.exports = React.createClass({ render () { return ( -
-
    { this.getItems() }