Skip to content

Commit 88c26a2

Browse files
committed
Merge pull request #10 from salesforce-ux/lookups-refactor
Lookups refactor
2 parents c110551 + 02d1dc2 commit 88c26a2

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

components/SLDSLookup/Menu/Item/index.jsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class Item extends React.Component {
1616
super(props);
1717
}
1818

19+
componentWillReceiveProps(nextProps){
20+
if(nextProps.isActive !== this.props.isActive){
21+
this.props.setActiveDescendant(nextProps.id);
22+
}
23+
}
24+
1925
boldSearchText(children) {
2026
const term = this.props.searchTerm;
2127
if(!children || !term) return children;
@@ -31,17 +37,17 @@ class Item extends React.Component {
3137
e.nativeEvent.preventDefault();
3238
e.nativeEvent.stopImmediatePropagation();
3339
}
34-
return this.props.onSelect(this.props.index);
40+
return this.props.onSelect(this.props.id);
3541
}
3642

3743
render(){
3844
let className = 'slds-lookup__item';
3945

4046
//TODO: make isActive styles into a class??
47+
let id = this.props.id;
4148
let styles = {};
4249
if(this.props.isActive) className += ' slds-theme--shade';
4350

44-
4551
return (
4652
//IMPORTANT: id is used to set lookup's input's aria-activedescendant
4753
<li
@@ -50,14 +56,14 @@ class Item extends React.Component {
5056
role="presentaion">
5157
<a
5258
href={this.props.href}
53-
id={this.props.id}
59+
id={id}
5460
tabIndex="-1"
5561
aria-disabled={this.props.disabled}
5662
role="option"
5763
onClick={this.handleClick.bind(this)}
5864
onMouseDown={this.handleClick.bind(this)}>
5965
<Icon name="account" />
60-
{ this.boldSearchText(this.props.children) }
66+
{ this.boldSearchText(this.props.children.label) }
6167
</a>
6268
</li>
6369
)

components/SLDSLookup/Menu/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Menu extends React.Component {
2626
return this.props.items.filter(this.filter, this).map((c, i) => {
2727
//isActive means it is aria-activedescendant
2828
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>
29+
return <Item key={c.id} id={c.id} setActiveDescendant={this.props.setActiveDescendant} isActive={isActive} onSelect={this.props.onSelect} searchTerm={this.props.searchTerm}>{c}</Item>
3030
});
3131
}
3232

components/SLDSLookup/index.jsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ const defaultFilter = (term, item) => {
2222
class SLDSLookup extends React.Component {
2323
constructor(props) {
2424
super(props);
25+
this.props.items.map((item, index) => {
26+
return item.id = 'item-' + index;
27+
})
28+
2529
this.state = {
2630
searchTerm: '',
2731
isOpen:false,
32+
activeItem:null,
2833
selectedIndex: null,
2934
activeIndex:null,
3035
};
@@ -44,14 +49,15 @@ class SLDSLookup extends React.Component {
4449
})
4550
}
4651

47-
getActiveDescendant(){
48-
return this.state.activeIndex !== null ? 'item-' + this.state.activeIndex: "";
52+
setActiveDescendant(id){
53+
this.setState({activeItem:id});
4954
}
5055

5156
//=================================================
5257
// Select menu item (onClick or on key enter/space)
53-
selectItem(index){
58+
selectItem(itemId){
5459
//console.log('selectItem fired');
60+
let index = itemId.replace('item-', '');
5561
this.setState({
5662
selectedIndex: index,
5763
searchTerm: null
@@ -115,7 +121,7 @@ class SLDSLookup extends React.Component {
115121
//If user hits enter/space key, select current activedescendant item
116122
else if((event.keyCode === KEYS.ENTER || event.keyCode === KEYS.SPACE) && this.state.activeIndex !== null){
117123
EventUtil.trapImmediate(event);
118-
this.selectItem(this.state.activeIndex);
124+
this.selectItem(this.state.activeItem);
119125
}
120126
}
121127
}
@@ -130,6 +136,7 @@ class SLDSLookup extends React.Component {
130136
onSelect={this.selectItem.bind(this)}
131137
label={this.props.label}
132138
items={this.props.items}
139+
setActiveDescendant={this.setActiveDescendant.bind(this)}
133140
activeIndex={this.state.activeIndex}/>;
134141
}else{
135142
return null;
@@ -172,7 +179,7 @@ class SLDSLookup extends React.Component {
172179
aria-label="lookup"
173180
aria-haspopup="true"
174181
aria-autocomplete="list"
175-
aria-activedescendant={this.getActiveDescendant()}
182+
aria-activedescendant={this.state.activeItem ? this.state.activeItem:""}
176183
aria-expanded={this.state.isOpen}
177184
role="combobox"
178185
onChange={this.handleChange.bind(this)}

0 commit comments

Comments
 (0)