Skip to content

Commit 02d1dc2

Browse files
committed
Make onSelect work with enter/space key
1 parent 3e8ef57 commit 02d1dc2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

components/SLDSLookup/Menu/Item/index.jsx

Lines changed: 8 additions & 2 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;
@@ -38,10 +44,10 @@ class Item extends React.Component {
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,7 +56,7 @@ 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"

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={c.id} id={c.id} 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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SLDSLookup extends React.Component {
2929
this.state = {
3030
searchTerm: '',
3131
isOpen:false,
32+
activeItem:null,
3233
selectedIndex: null,
3334
activeIndex:null,
3435
};
@@ -48,8 +49,8 @@ class SLDSLookup extends React.Component {
4849
})
4950
}
5051

51-
getActiveDescendant(){
52-
return this.state.activeIndex !== null ? 'item-' + this.state.activeIndex: "";
52+
setActiveDescendant(id){
53+
this.setState({activeItem:id});
5354
}
5455

5556
//=================================================
@@ -120,7 +121,7 @@ class SLDSLookup extends React.Component {
120121
//If user hits enter/space key, select current activedescendant item
121122
else if((event.keyCode === KEYS.ENTER || event.keyCode === KEYS.SPACE) && this.state.activeIndex !== null){
122123
EventUtil.trapImmediate(event);
123-
this.selectItem(this.state.activeIndex);
124+
this.selectItem(this.state.activeItem);
124125
}
125126
}
126127
}
@@ -135,6 +136,7 @@ class SLDSLookup extends React.Component {
135136
onSelect={this.selectItem.bind(this)}
136137
label={this.props.label}
137138
items={this.props.items}
139+
setActiveDescendant={this.setActiveDescendant.bind(this)}
138140
activeIndex={this.state.activeIndex}/>;
139141
}else{
140142
return null;
@@ -177,12 +179,12 @@ class SLDSLookup extends React.Component {
177179
aria-label="lookup"
178180
aria-haspopup="true"
179181
aria-autocomplete="list"
180-
aria-activedescendant={this.getActiveDescendant()}
182+
aria-activedescendant={this.state.activeItem ? this.state.activeItem:""}
181183
aria-expanded={this.state.isOpen}
182184
role="combobox"
183185
onChange={this.handleChange.bind(this)}
184186
onFocus={this.handleFocus.bind(this)}
185-
//onBlur={this.handleBlur.bind(this)}
187+
onBlur={this.handleBlur.bind(this)}
186188
onClick={this.handleClick.bind(this)}
187189
onKeyDown={this.handleKeyDown.bind(this)}
188190
value={this.state.searchTerm} />

0 commit comments

Comments
 (0)