Skip to content

Commit 7dbefa2

Browse files
committed
Merge pull request #12 from salesforce-ux/lookups-refactor
Add scroll handler to scroll through menu items when they are in focus
2 parents 028e05f + 72a9db7 commit 7dbefa2

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

components/SLDSLookup/Menu/Item/index.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Item extends React.Component {
1818

1919
componentWillReceiveProps(nextProps){
2020
if(nextProps.isActive !== this.props.isActive && (nextProps.isActive === true)){
21+
this.scrollFocus();
2122
this.props.setFocus(this.props.id);
2223
}
2324
}
@@ -40,6 +41,13 @@ class Item extends React.Component {
4041
return this.props.onSelect(this.props.id);
4142
}
4243

44+
scrollFocus(){
45+
const height = React.findDOMNode(this).offsetHeight;
46+
if(height && this.props.handleItemFocus){
47+
this.props.handleItemFocus(this.props.index,height);
48+
}
49+
}
50+
4351
render(){
4452
let className = 'slds-lookup__item';
4553
let id = this.props.id;
@@ -69,6 +77,7 @@ class Item extends React.Component {
6977
Item.propTypes = {
7078
id: React.PropTypes.string,
7179
setFocus: React.PropTypes.func,
80+
scrollFocus: React.PropTypes.func,
7281
isActive: React.PropTypes.bool,
7382
onSelect: React.PropTypes.func,
7483
searchTerm: React.PropTypes.string,

components/SLDSLookup/Menu/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,37 @@ class Menu extends React.Component {
2727
return this.props.filterWith(this.props.searchTerm, item);
2828
}
2929

30+
handleItemFocus (itemIndex, itemHeight) {
31+
if(this.refs.list){
32+
React.findDOMNode(this.refs.list).scrollTop = itemIndex * itemHeight;
33+
}
34+
}
35+
3036
renderItems(){
3137
return this.props.items.filter(this.filter, this).map((c, i) => {
3238
//isActive means it is aria-activedescendant
3339
const isActive = this.props.focusIndex === i ? true : false;
34-
return <Item key={c.id} id={c.id} setFocus={this.props.setFocus} isActive={isActive} onSelect={this.props.onSelect} searchTerm={this.props.searchTerm}>{c}</Item>
40+
return <Item
41+
key={c.id}
42+
id={c.id}
43+
index={i}
44+
setFocus={this.props.setFocus}
45+
isActive={isActive}
46+
handleItemFocus={this.handleItemFocus.bind(this)}
47+
onSelect={this.props.onSelect}
48+
searchTerm={this.props.searchTerm}>{c}</Item>
3549
});
3650
}
3751

3852
render(){
3953
return (
4054
<div
4155
className="ignore-react-onclickoutside slds-lookup__menu"
42-
role="listbox">
43-
<ul className="slds-lookup__list" role="presentation" ref="list">
56+
role="listbox"
57+
ref="scroll">
58+
<ul className="slds-lookup__list"
59+
role="presentation"
60+
ref="list">
4461
{this.renderItems()}
4562
</ul>
4663
</div>

components/SLDSPicklistBase/list.jsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = React.createClass({
7171
newHighlightedIndex = this.props.options.length - 1;
7272
}
7373
else if(newHighlightedIndex >= this.props.options.length){
74-
newHighlightedIndex = 0;
74+
newHighlightedIndex = 0;
7575
}
7676
if(this.props.onUpdateHighlighted){
7777
this.props.onUpdateHighlighted(newHighlightedIndex);
@@ -125,17 +125,17 @@ module.exports = React.createClass({
125125
getItems () {
126126
return this.props.options.map((option, index) =>{
127127
return (
128-
<ListItem
128+
<ListItem
129129
key={index}
130-
index={index}
130+
index={index}
131131
label={option.label}
132-
value={option.value}
132+
value={option.value}
133133
isHighlighted={(index===this.props.highlightedIndex)}
134-
isSelected={(index===this.props.selectedIndex)}
135-
onUpdateHighlighted={this.handleUpdateHighlighted}
134+
isSelected={(index===this.props.selectedIndex)}
135+
onUpdateHighlighted={this.handleUpdateHighlighted}
136136
onMoveFocus={this.handleMoveFocus}
137137
onBlur={this.handleListItemBlur}
138-
onFocus={this.handleItemFocus}
138+
onFocus={this.handleItemFocus}
139139
onSelect={this.handleSelect}
140140
onSearch={this.handleSearch}
141141
onCancel={this.handleCancel}/>
@@ -145,17 +145,17 @@ module.exports = React.createClass({
145145

146146
render () {
147147
return (
148-
<div
148+
<div
149149
ref="scroll"
150150
className={'slds-wrap slds-grow slds-scrollable--y '+this.props.className}
151151
style={{
152152
maxHeight:260
153153
}}
154154
>
155-
<ul
155+
<ul
156156
ref="scroll"
157157
className={"slds-dropdown__list slds-theme--"+this.props.theme}
158-
role="menu"
158+
role="menu"
159159
aria-labelledby={this.props.label}>
160160
{ this.getItems() }
161161
</ul>

0 commit comments

Comments
 (0)