Skip to content

Commit d0ce961

Browse files
committed
minor performance improvement
1 parent cba908e commit d0ce961

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 from 'react';
11+
import SLDSIcon from "../../../SLDSIcon";
12+
import { EventUtil } from '../../../utils';
13+
14+
const displayName = "LookupDefaultSectionHeader";
15+
const propTypes = {};
16+
const defaultProps = {};
17+
18+
class DefaultSectionHeader extends React.Component {
19+
constructor(props) {
20+
super(props);
21+
}
22+
23+
componentWillReceiveProps(nextProps){
24+
if(nextProps.isActive !== this.props.isActive && nextProps.isActive === true) {
25+
this.props.setFocus('searchRecords');
26+
}
27+
}
28+
29+
handleClick(){
30+
console.log('=====> Lookup Header Clicked');
31+
if(this.props.onClose){
32+
this.props.onClose();
33+
}
34+
}
35+
36+
handleMouseDown(event) {
37+
EventUtil.trapImmediate(event);
38+
}
39+
40+
render(){
41+
return (
42+
<div className="slds-lookup__item"
43+
tabIndex="-1"
44+
>
45+
@@@ Section
46+
</div>
47+
)
48+
}
49+
}
50+
51+
DefaultSectionHeader.displayName = displayName;
52+
DefaultSectionHeader.propTypes = propTypes;
53+
DefaultSectionHeader.defaultProps = defaultProps;
54+
55+
module.exports = DefaultSectionHeader;

components/SLDSLookup/Menu/index.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import ReactDOM from 'react-dom';
1212

1313
import Item from './Item';
1414

15+
import DefaultSectionHeader from './DefaultSectionHeader';
16+
1517
const displayName = 'SLDSLookup-Menu';
1618
const propTypes = {
1719
boldRegex: React.PropTypes.instanceOf(RegExp),
@@ -32,15 +34,24 @@ const defaultProps = {
3234
class Menu extends React.Component {
3335
constructor(props){
3436
super(props);
35-
this.state = {};
37+
this.state = {filteredItems:this.filteredItems()};
3638
}
3739

3840
//Set filtered list length in parent to determine active indexes for aria-activedescendent
39-
componentDidUpdate(){
41+
componentDidUpdate(prevProps){
4042
// make an array of the children of the list but only count the actual items
4143
let list = [].slice.call(ReactDOM.findDOMNode(this.refs.list).children)
4244
.filter((child) => child.className.indexOf("slds-lookup__item") > -1).length;
4345
this.props.getListLength(list);
46+
if(
47+
prevProps.items !== this.props.items ||
48+
prevProps.filter !== this.props.filter ||
49+
prevProps.searchTerm !== this.props.searchTerm
50+
){
51+
this.setState({
52+
filteredItems:this.filteredItems()
53+
});
54+
}
4455
}
4556

4657
filter(item){
@@ -58,6 +69,12 @@ class Menu extends React.Component {
5869
}
5970
}
6071

72+
getFilteredItemForIndex(i){
73+
if(i && this.state.filteredItems && i< this.state.filteredItems.length){
74+
return this.state.filteredItems[i];
75+
}
76+
}
77+
6178
renderHeader(){
6279
return this.props.header;
6380
}
@@ -67,14 +84,19 @@ class Menu extends React.Component {
6784
}
6885

6986
renderItems(){
70-
return this.filteredItems().map((c, i) => {
87+
let focusIndex = this.props.focusIndex;
88+
return this.state.filteredItems.map((c, i) => {
7189
//isActive means it is aria-activedescendant
7290
const id = c.id;
7391
let isActive = false;
7492
if (this.props.header) {
75-
isActive = this.props.focusIndex === i + 1 ? true : false;
93+
isActive = focusIndex === i + 1 ? true : false;
7694
}else{
77-
isActive = this.props.focusIndex === i ? true : false;
95+
isActive = focusIndex === i ? true : false;
96+
}
97+
if(c.data.type==='section'){
98+
// focusIndex++;
99+
return <DefaultSectionHeader data={c.data} key={'section_header_'+i}/>;
78100
}
79101
return <Item
80102
boldRegex={this.props.boldRegex}
@@ -98,7 +120,7 @@ class Menu extends React.Component {
98120
}
99121

100122
renderContent(){
101-
if (this.filteredItems().length === 0)
123+
if (this.state.filteredItems.length === 0)
102124
return (
103125
<li className="slds-lookup__message" aria-live="polite">
104126
<span className="slds-m-left--x-large slds-p-vertical--medium">{this.props.emptyMessage}</span>

components/SLDSLookup/index.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const propTypes = {
9595
*/
9696
const defaultFilter = (term, item) => {
9797
if(!term) return true;
98-
return item.label.match(new RegExp(escapeRegExp(term), "ig"));
98+
return (item.data && item.data.type === 'section') || item.label.match(new RegExp(escapeRegExp(term), "ig"));
9999
};
100100

101101

@@ -166,8 +166,16 @@ class SLDSLookup extends React.Component {
166166
// Using down/up keys, set Focus on list item and assign it to aria-activedescendant attribute in input.
167167
// 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.
168168
increaseIndex() {
169+
if(filteredItem && filteredItem.data.type === 'section'){
170+
this.state.focusIndex++;
171+
}
169172
let numFocusable = this.getNumFocusableItems();
170-
this.setState({ focusIndex: this.state.focusIndex < numFocusable ? this.state.focusIndex + 1 : 0 });
173+
let nextFocusIndex = this.state.focusIndex < numFocusable ? this.state.focusIndex + 1 : 0;
174+
const filteredItem = this.refs.menu.getFilteredItemForIndex(nextFocusIndex);
175+
if(filteredItem && filteredItem.data.type === 'section'){
176+
nextFocusIndex++;
177+
}
178+
this.setState({ focusIndex: nextFocusIndex });
171179
}
172180

173181
decreaseIndex() {
@@ -362,6 +370,7 @@ class SLDSLookup extends React.Component {
362370
renderMenuContent() {
363371
if(this.state.isOpen){
364372
return <Menu
373+
ref="menu"
365374
emptyMessage={this.props.emptyMessage}
366375
filterWith={this.props.filterWith}
367376
focusIndex={this.state.focusIndex}

demo/pages/Playground.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ module.exports = React.createClass({
2020
onChange={function(newValue){console.log("New search term: ", newValue)}}
2121
onSelect={function(item){console.log(item , " Selected")}}
2222
options={[
23+
{type:'section', label:'Section 1'},
2324
{label: "Paddy\"s Pub"},
2425
{label: "Tyrell Corp"},
26+
{type:'section', label:'Section 2'},
2527
{label: "Paper St. Soap Company"},
2628
{label: "Nakatomi Investments"},
2729
{label: "Acme Landscaping"},
30+
{type:'section', label:'Section 3'},
2831
{label: "Acme Construction"}
2932
]}
3033
/>

0 commit comments

Comments
 (0)