Skip to content

Commit f577cf6

Browse files
committed
Make onClick select item from list
1 parent 0201006 commit f577cf6

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

components/SLDSLookup/Menu/Item/index.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
99

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

1414
class Item extends React.Component {
1515
constructor(props) {
@@ -25,6 +25,15 @@ class Item extends React.Component {
2525
});
2626
}
2727

28+
handleClick(e){
29+
e.preventDefault();
30+
if(e.nativeEvent){
31+
e.nativeEvent.preventDefault();
32+
e.nativeEvent.stopImmediatePropagation();
33+
}
34+
return this.props.onSelect(this.props.index);
35+
}
36+
2837
render(){
2938
let className = 'slds-lookup__item';
3039

@@ -33,6 +42,7 @@ class Item extends React.Component {
3342
if(this.props.isActive) styles = {backgroundColor: 'red'};
3443

3544
return (
45+
//IMPORTANT: id is used to set lookup's input's aria-activedescendant
3646
<li
3747
className={className}
3848
style={styles}
@@ -42,7 +52,8 @@ class Item extends React.Component {
4252
id={this.props.id}
4353
tabIndex="-1"
4454
aria-disabled={this.props.disabled}
45-
role="option">
55+
role="option"
56+
onClick={this.handleClick.bind(this)}>
4657
<Icon name="account" />
4758
{ this.boldSearchText(this.props.children) }
4859
</a>

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} isActive={isActive} searchTerm={this.props.searchTerm}>{c}</Item>
29+
return <Item key={i} id={'item-' + i} index={i} isActive={isActive} onSelect={this.props.onSelect} searchTerm={this.props.searchTerm}>{c}</Item>
3030
});
3131
}
3232

components/SLDSLookup/index.jsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class SLDSLookup extends React.Component {
2525
this.state = {
2626
searchTerm: '',
2727
isOpen:false,
28-
selectedItem: null,
28+
selectedIndex: null,
2929
activeIndex:null,
3030
};
3131
}
3232

3333
//=================================================
34-
// Set Active Descendant (currently focused/hovered item in list)
34+
// Set Active Descendant (on key down/up, set currently focused/hovered item in list)
3535
increaseIndex(){
3636
this.setState({
3737
activeIndex: this.state.activeIndex <= this.props.items.length ? this.state.activeIndex + 1 : 0
@@ -44,6 +44,22 @@ class SLDSLookup extends React.Component {
4444
})
4545
}
4646

47+
getActiveDescendant(){
48+
console.log("aria-activedescendant is ", 'item-' + this.state.activeIndex);
49+
return this.state.activeIndex !== null ? 'item-' + this.state.activeIndex: "";
50+
}
51+
52+
//=================================================
53+
// Select menu item (onClick or on key enter/space)
54+
selectItem(index){
55+
this.setState({
56+
selectedIndex: index,
57+
searchTerm: null
58+
});
59+
console.log("Selected Item is ", this.props.items[index]);
60+
}
61+
62+
4763
//=================================================
4864
// Basic Event Listeners on Input
4965
handleClose() {
@@ -101,6 +117,7 @@ class SLDSLookup extends React.Component {
101117
return <Menu
102118
searchTerm={this.state.searchTerm}
103119
filterWith={this.props.filterWith}
120+
onSelect={this.selectItem.bind(this)}
104121
label={this.props.label}
105122
items={this.props.items}
106123
activeIndex={this.state.activeIndex}/>;
@@ -125,7 +142,7 @@ class SLDSLookup extends React.Component {
125142
aria-label="lookup"
126143
aria-haspopup="true"
127144
aria-autocomplete="list"
128-
aria-activedescendant={this.state.activeIndex !== null ? "item-" + this.state.activeIndex : ""}
145+
aria-activedescendant={this.getActiveDescendant()}
129146
aria-expanded={this.state.isOpen}
130147
role="combobox"
131148
onChange={this.handleChange.bind(this)}

0 commit comments

Comments
 (0)