Skip to content

Commit abe4ee7

Browse files
committed
Merge pull request #9 from salesforce-ux/lookups-refactor
Lookups refactor
2 parents ecca8ff + 3fb3391 commit abe4ee7

File tree

6 files changed

+223
-194
lines changed

6 files changed

+223
-194
lines changed
Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
88
*/
99

1010
import React, { Component } from 'react';
11-
import {Icon} from "../../SLDSIcons";
11+
import {Icon} from "../../../SLDSIcons";
12+
import {KEYS,EventUtil} from '../../../utils';
1213

1314
class Item extends React.Component {
1415
constructor(props) {
1516
super(props);
16-
this.id = this.props.id || `item-${Item.globalIdx++}-${this.props.idx}`;
17-
1817
}
1918

2019
boldSearchText(children) {
@@ -26,83 +25,44 @@ class Item extends React.Component {
2625
});
2726
}
2827

29-
selectedItem(e) {
28+
handleClick(e){
3029
e.preventDefault();
3130
if(e.nativeEvent){
3231
e.nativeEvent.preventDefault();
3332
e.nativeEvent.stopImmediatePropagation();
3433
}
35-
return this.props.selectedItem(this.props.idx, this);
36-
}
37-
38-
getClassName(cls) {
39-
return classNames(this.props.className, cls);
34+
return this.props.onSelect(this.props.index);
4035
}
4136

42-
render() {
37+
render(){
4338
let className = 'slds-lookup__item';
44-
if(this.props.isSelected) className += ' slds-is-selected';
45-
const tabIndex = this.props.idx === 0 ? 0 : -1;
39+
40+
//TODO: make isActive styles into a class??
41+
let styles = {};
42+
if(this.props.isActive) className += ' slds-theme--shade';
43+
4644

4745
return (
48-
<li key={this.id} { ...this.props } className={className} role="presentation" tabIndex={tabIndex} style={{whiteSpace: 'nowrap'}} >
49-
<a href={ this.props.href } onClick={this.selectedItem.bind(this)} onMouseDown={this.selectedItem.bind(this)} tabIndex="-1" aria-disabled={ this.props.disabled } role="option">
46+
//IMPORTANT: id is used to set lookup's input's aria-activedescendant
47+
<li
48+
className={className}
49+
style={styles}
50+
role="presentaion">
51+
<a
52+
href={this.props.href}
53+
id={this.props.id}
54+
tabIndex="-1"
55+
aria-disabled={this.props.disabled}
56+
role="option"
57+
onClick={this.handleClick.bind(this)}
58+
onMouseDown={this.handleClick.bind(this)}>
5059
<Icon name="account" />
5160
{ this.boldSearchText(this.props.children) }
5261
</a>
5362
</li>
54-
);
63+
)
5564
}
56-
}
57-
Item.globalIdx = 0;
58-
59-
60-
module.exports = React.createClass({
61-
62-
displayName: "SLDSLookup",
63-
64-
getInitialState: function(){
65-
return {currentSelectedIndex: null};
66-
},
67-
68-
getDefaultProps: function(){
69-
return {
70-
};
71-
},
72-
73-
selectedItem: function(idx, item) {
74-
console.log(this.props.selectedItem);
75-
if(this.props.selectedItem) this.props.selectedItem(item);
76-
this.setState({currentSelectedIndex: idx});
77-
},
78-
79-
filter: function(item) {
80-
return this.props.filterWith(this.props.searchTerm, item);
81-
},
82-
83-
items: function() {
84-
return this.props.items.filter(this.filter, this).map((c, i) => {
85-
const isSelected = (i === this.state.currentSelectedIndex);
86-
return <Item key={i} isSelected={isSelected} idx={i} searchTerm={this.props.searchTerm} selectedItem={this.selectedItem}>{c}</Item>
87-
});
88-
},
89-
90-
render: function() {
91-
return (
92-
<div
93-
className="ignore-react-onclickoutside"
94-
style={{
95-
position:'inherit',
96-
float:'inherit'
97-
}}
98-
role="listbox">
99-
<ul className="slds-lookup__list" role="presentation">
100-
{this.items()}
101-
</ul>
102-
</div>
103-
);
104-
},
105-
10665

66+
}
10767

108-
});
68+
module.exports = Item;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import {KEYS} from '../../utils';
13+
import Item from './Item';
14+
15+
class Menu extends React.Component {
16+
constructor(props){
17+
super(props);
18+
this.state = {};
19+
}
20+
21+
filter(item){
22+
return this.props.filterWith(this.props.searchTerm, item);
23+
}
24+
25+
renderItems(){
26+
return this.props.items.filter(this.filter, this).map((c, i) => {
27+
//isActive means it is aria-activedescendant
28+
const isActive = this.props.activeIndex === i ? true : false;
29+
return <Item key={i} id={'item-' + i} index={i} isActive={isActive} onSelect={this.props.onSelect} searchTerm={this.props.searchTerm}>{c}</Item>
30+
});
31+
}
32+
33+
render(){
34+
return (
35+
<div
36+
className="ignore-react-onclickoutside slds-lookup__menu"
37+
role="listbox">
38+
<ul className="slds-lookup__list" role="presentation" ref="list">
39+
{this.renderItems()}
40+
</ul>
41+
</div>
42+
)
43+
}
44+
}
45+
module.exports = Menu;

0 commit comments

Comments
 (0)