Skip to content

Commit 9921d59

Browse files
committed
Include fixed Action Items in lookup menu and include them in arrow down navigation
1 parent 0f010d3 commit 9921d59

File tree

4 files changed

+112
-68
lines changed

4 files changed

+112
-68
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: 8 additions & 14 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,20 +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

4440
//Scroll menu item based on up/down mouse keys (assumes all items are the same height)
4541
scrollFocus(){
4642
const height = React.findDOMNode(this).offsetHeight;
47-
if(height && this.props.handleItemFocus){
48-
this.props.handleItemFocus(this.props.index,height);
49-
}
43+
if(height && this.props.handleItemFocus) this.props.handleItemFocus(this.props.index,height);
5044
}
5145

5246
render(){
@@ -56,9 +50,7 @@ class Item extends React.Component {
5650

5751
return (
5852
//IMPORTANT: anchor id is used to set lookup's input's aria-activedescendant
59-
<li
60-
className={className}
61-
role="presentaion">
53+
<li className={className} role="presentaion">
6254
<a
6355
href={this.props.href}
6456
id={id}
@@ -76,15 +68,17 @@ class Item extends React.Component {
7668
}
7769

7870
Item.propTypes = {
71+
key: React.PropTypes.string,
7972
id: React.PropTypes.string,
8073
href: React.PropTypes.string,
74+
type: React.PropTypes.string,
8175
searchTerm: React.PropTypes.string,
76+
index: React.PropTypes.number,
8277
isActive: React.PropTypes.bool,
8378
isDisabled: React.PropTypes.bool,
8479
setFocus: React.PropTypes.func,
85-
scrollFocus: React.PropTypes.func,
80+
handleItemFocus: React.PropTypes.func,
8681
onSelect: React.PropTypes.func,
87-
handleItemFocus: React.PropTypes.func,
8882
};
8983

9084
Item.defaultProps = {

components/SLDSLookup/Menu/index.js

Lines changed: 40 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 {
@@ -41,54 +42,55 @@ class Menu extends React.Component {
4142
return <Item
4243
key={c.id}
4344
id={c.id}
44-
index={i}
4545
type={this.props.type}
46-
setFocus={this.props.setFocus}
46+
searchTerm={this.props.searchTerm}
47+
index={i}
4748
isActive={isActive}
49+
setFocus={this.props.setFocus}
4850
handleItemFocus={this.handleItemFocus.bind(this)}
4951
onSelect={this.props.onSelect}
50-
searchTerm={this.props.searchTerm}>{c}</Item>
52+
>
53+
{c}
54+
</Item>
5155
});
5256
}
5357

54-
renderSearchDetails(){
55-
let className = 'slds-button';
56-
if(this.props.focusIndex === 0) className += ' slds-theme--shade';
57-
return(
58-
<button id="searchDetails" tabIndex="-1" className={className}>
59-
<Icon name="search" category="utility" size="x-small" className="slds-icon-text-default" />
60-
{this.props.searchTerm ? '"' + this.props.searchTerm + '"' : ""} in {this.props.type + 's'}
61-
</button>
62-
);
63-
}
64-
65-
renderAddItem(){
66-
let className = 'slds-button';
67-
if(this.props.focusIndex === this.props.listLength + 1) className += ' slds-theme--shade';
68-
return(
69-
<button id="addItem" tabIndex="-1" className={className} onClick={this.props.addItem} onMouseDown={this.props.addItem}>
70-
<Icon name="add" category="utility" size="x-small" className="slds-icon-text-default" />
71-
New {this.props.type}
72-
</button>
73-
);
74-
}
75-
7658
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+
7764
return (
78-
<div
79-
className="ignore-react-onclickoutside slds-lookup__menu"
80-
role="listbox"
81-
ref="scroll">
65+
<div className="ignore-react-onclickoutside slds-lookup__menu" role="listbox" ref="scroll">
8266
<div className="slds-lookup__item">
83-
{this.renderSearchDetails()}
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>
8477
</div>
85-
<ul className="slds-lookup__list"
86-
role="presentation"
87-
ref="list">
78+
79+
<ul className="slds-lookup__list" role="presentation" ref="list">
8880
{this.renderItems()}
8981
</ul>
82+
9083
<div className="slds-lookup__item">
91-
{this.renderAddItem()}
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>
9294
</div>
9395
</div>
9496
)
@@ -99,14 +101,14 @@ Menu.propTypes = {
99101
searchTerm: React.PropTypes.string,
100102
label: React.PropTypes.string,
101103
type: React.PropTypes.string,
102-
listLength: React.PropTypes.number,
103104
focusIndex: React.PropTypes.number,
105+
listLength: React.PropTypes.number,
104106
items: React.PropTypes.array,
105-
onSelect: React.PropTypes.func,
106-
addItem: React.PropTypes.func,
107107
filterWith: React.PropTypes.func,
108-
setFocus: React.PropTypes.func,
109108
getListLength: React.PropTypes.func,
109+
setFocus: React.PropTypes.func,
110+
onSelect: React.PropTypes.func,
111+
addItem: React.PropTypes.func,
110112
};
111113

112114
Menu.defaultProps = {

components/SLDSLookup/index.jsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class SLDSLookup extends React.Component {
4848
//=================================================
4949
// Using down/up keys, set Focus on list item and assign it to aria-activedescendant attribute in input.
5050
// Need to keep track of filtered list length to be able to increment/decrement the focus index so it's contained to the number of available list items.
51+
// Adding/subtracting 1 from focusIndex to account for fixed action items (searchDetails and addNewItem buttons)
5152
increaseIndex(){
5253
let items = this.state.listLength;
5354
this.setState({ focusIndex: this.state.focusIndex <= items ? this.state.focusIndex + 1 : 0 })
@@ -74,6 +75,7 @@ class SLDSLookup extends React.Component {
7475
selectedIndex: index,
7576
searchTerm: null
7677
});
78+
if(this.props.onItemSelect) this.props.onItemSelect();
7779
}
7880

7981
handleDeleteSelected() {
@@ -83,31 +85,35 @@ class SLDSLookup extends React.Component {
8385
});
8486
}
8587

88+
addItem(){
89+
if(this.props.onAddItem) this.props.onAddItem();
90+
}
91+
8692
//=================================================
8793
// Event Listeners on Input
8894
handleClose() {
8995
this.setState({
9096
isOpen:false,
9197
focusIndex:null,
9298
currentFocus:null,
93-
})
99+
});
94100
}
95101

96102
handleClick() {
97-
this.setState({isOpen:true})
103+
this.setState({isOpen:true});
98104
}
99105

100106
handleBlur() {
101107
this.handleClose();
102108
}
103109

104110
handleFocus() {
105-
this.setState({ isOpen:true });
111+
this.setState({isOpen:true});
106112
}
107113

108114
handleChange(event) {
109115
const target = event.target || event.currentTarget;
110-
this.setState({ searchTerm: target.value });
116+
this.setState({searchTerm: target.value});
111117
}
112118

113119
handleKeyDown(event) {
@@ -123,7 +129,7 @@ class SLDSLookup extends React.Component {
123129
//If user hits up key, advance aria activedescendant to previous item
124130
else if(event.keyCode === KEYS.UP){
125131
EventUtil.trapImmediate(event);
126-
this.state.focusIndex === null ? this.setState({focusIndex: this.state.listLength}) : this.decreaseIndex();
132+
this.state.focusIndex === null ? this.setState({focusIndex: this.state.listLength + 1}) : this.decreaseIndex();
127133
}
128134
//If user hits enter/space key, select current activedescendant item
129135
else if((event.keyCode === KEYS.ENTER || event.keyCode === KEYS.SPACE) && this.state.focusIndex !== null){
@@ -141,29 +147,30 @@ class SLDSLookup extends React.Component {
141147
if(this.state.isOpen){
142148
return <Menu
143149
searchTerm={this.state.searchTerm}
144-
filterWith={this.props.filterWith}
145-
onSelect={this.selectItem.bind(this)}
146150
label={this.props.label}
151+
type={this.props.type}
152+
focusIndex={this.state.focusIndex}
153+
listLength={this.state.listLength}
147154
items={this.props.items}
148-
setFocus={this.setFocus.bind(this)}
155+
filterWith={this.props.filterWith}
149156
getListLength={this.getListLength.bind(this)}
150-
listLength={this.state.listLength}
151-
focusIndex={this.state.focusIndex}
152-
addItem={this.props.addItem}
153-
type={this.props.type}
157+
setFocus={this.setFocus.bind(this)}
158+
onSelect={this.selectItem.bind(this)}
159+
addItem={this.addItem}
154160
/>;
155161
}
156162
}
157163

158164
renderSelectedItem(){
165+
let selectedItem = this.props.items[this.state.selectedIndex].label;
159166
return (
160167
<div className="slds-pill">
161168
<a href="#" className="slds-pill__label">
162169
<Icon name={this.props.type} />
163-
{this.props.items[this.state.selectedIndex].label}
170+
{selectedItem}
164171
</a>
165172
<SLDSButton
166-
label='Remove'
173+
label={'Remove ' + selectedItem}
167174
variant='icon'
168175
iconName='close'
169176
iconSize='medium'
@@ -218,17 +225,17 @@ SLDSLookup.propTypes = {
218225
items: React.PropTypes.array,
219226
label: React.PropTypes.string,
220227
type: React.PropTypes.string,
221-
addItem: React.PropTypes.func,
222228
filterWith: React.PropTypes.func,
223229
onItemSelect: React.PropTypes.func,
230+
onAddItem: React.PropTypes.func,
224231
};
225232

226233
SLDSLookup.defaultProps = {
227234
filterWith: defaultFilter,
228235
onItemSelect: function(item){
229236
//console.log('onItemSelect should be defined');
230237
},
231-
addItem: function(event){
238+
onAddItem: function(event){
232239
//console.log('onItemSelect should be defined');
233240
},
234241
};

0 commit comments

Comments
 (0)