Skip to content

Commit d6c1169

Browse files
committed
Make lookup work on down/up arrow keys instead of tabbin first
1 parent 69560dd commit d6c1169

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

components/SLDSLookup/Menu/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Menu extends React.Component {
3737
renderItems(){
3838
return this.props.items.filter(this.filter, this).map((c, i) => {
3939
//isActive means it is aria-activedescendant
40-
const isActive = this.props.focusIndex === i ? true : false;
40+
const isActive = this.props.focusIndex === i + 1 ? true : false;
4141
return <Item
4242
key={c.id}
4343
id={c.id}
@@ -51,17 +51,21 @@ class Menu extends React.Component {
5151
}
5252

5353
renderSearchDetails(){
54+
let className = 'slds-button';
55+
if(this.props.focusIndex === 0) className += ' slds-theme--shade';
5456
return(
55-
<button className="slds-button">
57+
<button id="searchDetails" className={className}>
5658
<Icon name="search" category="utility" size="x-small" className="slds-icon-text-default" />
5759
{this.props.searchTerm ? '"' + this.props.searchTerm + '"' : ""} in {this.props.type}
5860
</button>
5961
);
6062
}
6163

6264
renderAddItem(){
65+
let className = 'slds-button';
66+
if(this.props.focusIndex === this.props.listLength + 1) className += ' slds-theme--shade';
6367
return(
64-
<button className="slds-button" onClick={this.props.addItem} onMouseDown={this.props.addItem}>
68+
<button id="addItem" className={className} onClick={this.props.addItem} onMouseDown={this.props.addItem}>
6569
<Icon name="add" category="utility" size="x-small" className="slds-icon-text-default" />
6670
New {this.props.type}
6771
</button>

components/SLDSLookup/index.jsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ class SLDSLookup extends React.Component {
4646
// Using down/up keys, set Focus on list item and assign it to aria-activedescendant attribute in input.
4747
// 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.
4848
increaseIndex(){
49-
let items = this.state.listLength - 1;
50-
this.setState({ focusIndex: this.state.focusIndex < items ? this.state.focusIndex + 1 : 0 })
49+
let items = this.state.listLength;
50+
this.setState({ focusIndex: this.state.focusIndex <= items ? this.state.focusIndex + 1 : 0 })
5151
}
5252

5353
decreaseIndex(){
54-
let items = this.state.listLength - 1;
54+
let items = this.state.listLength;
5555
this.setState({ focusIndex: this.state.focusIndex > 0 ? this.state.focusIndex - 1 : items })
5656
}
5757

@@ -85,7 +85,8 @@ class SLDSLookup extends React.Component {
8585
handleClose() {
8686
this.setState({
8787
isOpen:false,
88-
focusIndex:null
88+
focusIndex:null,
89+
currentFocus:null,
8990
})
9091
}
9192

@@ -111,25 +112,22 @@ class SLDSLookup extends React.Component {
111112
//If user hits esc key, close menu
112113
event.keyCode === KEYS.ESCAPE ? this.handleClose() : this.handleClick();
113114

114-
//If user hits tab key, move aria activedescendant to first menu item
115-
if(event.keyCode === KEYS.TAB && this.state.focusIndex === null){
116-
this.setState({focusIndex: 0});
117-
EventUtil.trapImmediate(event);
118-
}
119115
//If user hits down key, advance aria activedescendant to next item
120-
else if(event.keyCode === KEYS.DOWN && this.state.focusIndex !== null){
116+
if(event.keyCode === KEYS.DOWN){
121117
EventUtil.trapImmediate(event);
122-
this.increaseIndex();
118+
this.state.focusIndex === null ? this.setState({focusIndex: 0}) : this.increaseIndex();
123119
}
124120
//If user hits up key, advance aria activedescendant to previous item
125-
else if(event.keyCode === KEYS.UP && this.state.focusIndex !== null){
121+
else if(event.keyCode === KEYS.UP){
126122
EventUtil.trapImmediate(event);
127-
this.decreaseIndex();
123+
this.state.focusIndex === null ? this.setState({focusIndex: this.state.listLength}) : this.decreaseIndex();
128124
}
129125
//If user hits enter/space key, select current activedescendant item
130126
else if((event.keyCode === KEYS.ENTER || event.keyCode === KEYS.SPACE) && this.state.focusIndex !== null){
131127
EventUtil.trapImmediate(event);
132-
this.selectItem(this.state.currentFocus);
128+
//If the focus is on the first or last item in the menu (search details or add item buttons), then close.
129+
//If not, then select menu item
130+
(this.state.focusIndex === 0 || this.state.focusIndex === (this.state.listLength + 1)) ? this.handleClose() : this.selectItem(this.state.currentFocus);
133131
}
134132
}
135133
}
@@ -227,7 +225,7 @@ SLDSLookup.defaultProps = {
227225
onItemSelect: function(item){
228226
//console.log('onItemSelect should be defined');
229227
},
230-
addItem: function(item){
228+
addItem: function(event){
231229
//console.log('onItemSelect should be defined');
232230
},
233231
};

demo/pages/HomePage/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ module.exports = React.createClass( {
5959
</div>
6060
<main className='stage-main slds-grid slds-wrap slds-grow' role='main'>
6161
<div className='region region--main slds-grow slds-size--1-of-1 slds-medium-size--1-of-2 slds-large-size--8-of-12 slds-col-rule--right slds-p-around--large'>
62+
<LookupBaseSection />
6263

6364
<ButtonSection />
6465

65-
<LookupBaseSection />
6666

6767
<PicklistBaseSection />
6868

0 commit comments

Comments
 (0)