Skip to content

Commit 5adfc20

Browse files
committed
Merge pull request #23 from salesforce-ux/lookups-refactor
Lookups refactor
2 parents 90c027f + 87774a5 commit 5adfc20

File tree

8 files changed

+187
-118
lines changed

8 files changed

+187
-118
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright (c) 2015, salesforce.com, inc. All rights reserved.
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
5+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
6+
Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
7+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8+
*/
9+
10+
import React, { Component } from 'react';
11+
import {Icon} from "../../../SLDSIcons";
12+
13+
class ActionItem extends React.Component {
14+
constructor(props) {
15+
super(props);
16+
}
17+
18+
componentWillReceiveProps(nextProps){
19+
if(nextProps.isActive !== this.props.isActive && nextProps.isActive === true) this.props.setFocus(this.props.id);
20+
}
21+
22+
render(){
23+
let className = 'slds-button';
24+
if(this.props.isActive) className += ' slds-theme--shade'
25+
26+
return (
27+
<button id={this.props.id} tabIndex="-1" className={className} onClick={this.props.onSelect} onMouseDown={this.props.onSelect}>
28+
<Icon name={this.props.icon} category="utility" size="x-small" className="slds-icon-text-default" />
29+
{this.props.children}
30+
</button>
31+
)
32+
}
33+
}
34+
35+
ActionItem.propTypes = {
36+
};
37+
38+
ActionItem.defaultProps = {
39+
};
40+
41+
module.exports = ActionItem;

components/SLDSLookup/Menu/Item/index.jsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Item extends React.Component {
1717
}
1818

1919
componentWillReceiveProps(nextProps){
20-
if(nextProps.isActive !== this.props.isActive && (nextProps.isActive === true)){
20+
if(nextProps.isActive !== this.props.isActive && nextProps.isActive === true){
2121
this.scrollFocus();
2222
this.props.setFocus(this.props.id);
2323
}
@@ -33,19 +33,14 @@ class Item extends React.Component {
3333
}
3434

3535
handleClick(e){
36-
e.preventDefault();
37-
if(e.nativeEvent){
38-
e.nativeEvent.preventDefault();
39-
e.nativeEvent.stopImmediatePropagation();
40-
}
36+
EventUtil.trapImmediate(e);
4137
return this.props.onSelect(this.props.id);
4238
}
4339

40+
//Scroll menu item based on up/down mouse keys (assumes all items are the same height)
4441
scrollFocus(){
4542
const height = React.findDOMNode(this).offsetHeight;
46-
if(height && this.props.handleItemFocus){
47-
this.props.handleItemFocus(this.props.index,height);
48-
}
43+
if(height && this.props.handleItemFocus) this.props.handleItemFocus(this.props.index,height);
4944
}
5045

5146
render(){
@@ -55,18 +50,16 @@ class Item extends React.Component {
5550

5651
return (
5752
//IMPORTANT: anchor id is used to set lookup's input's aria-activedescendant
58-
<li
59-
className={className}
60-
role="presentaion">
53+
<li className={className} role="presentaion">
6154
<a
6255
href={this.props.href}
6356
id={id}
6457
tabIndex="-1"
65-
aria-disabled={this.props.disabled}
58+
aria-disabled={this.props.isDisabled}
6659
role="option"
6760
onClick={this.handleClick.bind(this)}
6861
onMouseDown={this.handleClick.bind(this)}>
69-
<Icon name="account" />
62+
<Icon name={this.props.type} />
7063
{ this.boldSearchText(this.props.children.label) }
7164
</a>
7265
</li>
@@ -75,12 +68,17 @@ class Item extends React.Component {
7568
}
7669

7770
Item.propTypes = {
71+
key: React.PropTypes.string,
7872
id: React.PropTypes.string,
79-
setFocus: React.PropTypes.func,
80-
scrollFocus: React.PropTypes.func,
73+
href: React.PropTypes.string,
74+
type: React.PropTypes.string,
75+
searchTerm: React.PropTypes.string,
76+
index: React.PropTypes.number,
8177
isActive: React.PropTypes.bool,
78+
isDisabled: React.PropTypes.bool,
79+
setFocus: React.PropTypes.func,
80+
handleItemFocus: React.PropTypes.func,
8281
onSelect: React.PropTypes.func,
83-
searchTerm: React.PropTypes.string,
8482
};
8583

8684
Item.defaultProps = {

components/SLDSLookup/Menu/index.js

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import React, { Component } from 'react';
1111
import Item from './Item';
12+
import ActionItem from './ActionItem';
1213
import {Icon} from "../../SLDSIcons";
1314

1415
class Menu extends React.Component {
@@ -17,6 +18,7 @@ class Menu extends React.Component {
1718
this.state = {};
1819
}
1920

21+
//Set filtered list length in parent to determine active indexes for aria-activedescendent
2022
componentDidUpdate(prevProps, prevState){
2123
let list = React.findDOMNode(this.refs.list).children.length;
2224
this.props.getListLength(list);
@@ -26,6 +28,7 @@ class Menu extends React.Component {
2628
return this.props.filterWith(this.props.searchTerm, item);
2729
}
2830

31+
//Scroll menu up/down when using mouse keys
2932
handleItemFocus (itemIndex, itemHeight) {
3033
if(this.refs.list){
3134
React.findDOMNode(this.refs.list).scrollTop = itemIndex * itemHeight;
@@ -35,66 +38,77 @@ class Menu extends React.Component {
3538
renderItems(){
3639
return this.props.items.filter(this.filter, this).map((c, i) => {
3740
//isActive means it is aria-activedescendant
38-
const isActive = this.props.focusIndex === i ? true : false;
41+
const isActive = this.props.focusIndex === i + 1 ? true : false;
3942
return <Item
4043
key={c.id}
4144
id={c.id}
45+
type={this.props.type}
46+
searchTerm={this.props.searchTerm}
4247
index={i}
43-
setFocus={this.props.setFocus}
4448
isActive={isActive}
49+
setFocus={this.props.setFocus}
4550
handleItemFocus={this.handleItemFocus.bind(this)}
4651
onSelect={this.props.onSelect}
47-
searchTerm={this.props.searchTerm}>{c}</Item>
52+
>
53+
{c}
54+
</Item>
4855
});
4956
}
5057

51-
renderEmptyState(){
52-
let className = 'slds-lookup__item';
53-
return (
54-
<li
55-
className={className}
56-
role="presentaion">
57-
<a
58-
href='#'
59-
id='add-item'
60-
tabIndex="-1"
61-
role="option"
62-
onClick={this.props.addItem}
63-
onMouseDown={this.props.addItem}>
64-
<Icon name="add" category="utility" size="x-small" className="slds-icon-text-default" />
65-
New {this.props.type}
66-
</a>
67-
</li>
68-
);
69-
}
70-
7158
render(){
59+
let isNewItemBtnActive = false;
60+
let isSearchDetailsActive = false;
61+
this.props.focusIndex === this.props.listLength + 1 ? isNewItemBtnActive = true : isNewItemBtnActive = false;
62+
this.props.focusIndex === 0 ? isSearchDetailsActive = true: isSearchDetailsActive = false;
63+
7264
return (
73-
<div
74-
className="ignore-react-onclickoutside slds-lookup__menu"
75-
role="listbox"
76-
ref="scroll">
77-
<ul className="slds-lookup__list"
78-
role="presentation"
79-
ref="list">
80-
{this.renderItems()}
81-
{this.renderEmptyState()}
82-
</ul>
65+
<div className="ignore-react-onclickoutside slds-lookup__menu" role="listbox" ref="scroll">
66+
<div className="slds-lookup__item">
67+
<ActionItem
68+
id='searchDetails'
69+
icon='search'
70+
type={this.props.type}
71+
isActive={isSearchDetailsActive}
72+
setFocus={this.props.setFocus}
73+
onSelect={this.props.addItem}
74+
>
75+
{this.props.searchTerm ? '"' + this.props.searchTerm + '"' : ""} in {this.props.type + 's'}
76+
</ActionItem>
77+
</div>
78+
79+
<ul className="slds-lookup__list" role="presentation" ref="list">
80+
{this.renderItems()}
81+
</ul>
82+
83+
<div className="slds-lookup__item">
84+
<ActionItem
85+
id='addNewItem'
86+
icon='add'
87+
type={this.props.type}
88+
isActive={isNewItemBtnActive}
89+
setFocus={this.props.setFocus}
90+
onSelect={this.props.addItem}
91+
>
92+
New {this.props.type}
93+
</ActionItem>
94+
</div>
8395
</div>
8496
)
8597
}
8698
}
8799

88100
Menu.propTypes = {
89101
searchTerm: React.PropTypes.string,
90-
filterWith: React.PropTypes.func,
91-
onSelect: React.PropTypes.func,
92102
label: React.PropTypes.string,
103+
type: React.PropTypes.string,
104+
focusIndex: React.PropTypes.number,
105+
listLength: React.PropTypes.number,
93106
items: React.PropTypes.array,
94-
setFocus: React.PropTypes.func,
107+
filterWith: React.PropTypes.func,
95108
getListLength: React.PropTypes.func,
96-
listLength: React.PropTypes.number,
97-
focusIndex: React.PropTypes.number,
109+
setFocus: React.PropTypes.func,
110+
onSelect: React.PropTypes.func,
111+
addItem: React.PropTypes.func,
98112
};
99113

100114
Menu.defaultProps = {

0 commit comments

Comments
 (0)