Skip to content

Lookups refactor #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions components/SLDSLookup/Menu/ActionItem/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright (c) 2015, salesforce.com, inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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.
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.
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.
*/

import React, { Component } from 'react';
import {Icon} from "../../../SLDSIcons";

class ActionItem extends React.Component {
constructor(props) {
super(props);
}

componentWillReceiveProps(nextProps){
if(nextProps.isActive !== this.props.isActive && nextProps.isActive === true) this.props.setFocus(this.props.id);
}

render(){
let className = 'slds-button';
if(this.props.isActive) className += ' slds-theme--shade'

return (
<button id={this.props.id} tabIndex="-1" className={className} onClick={this.props.onSelect} onMouseDown={this.props.onSelect}>
<Icon name={this.props.icon} category="utility" size="x-small" className="slds-icon-text-default" />
{this.props.children}
</button>
)
}
}

ActionItem.propTypes = {
};

ActionItem.defaultProps = {
};

module.exports = ActionItem;
32 changes: 15 additions & 17 deletions components/SLDSLookup/Menu/Item/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Item extends React.Component {
}

componentWillReceiveProps(nextProps){
if(nextProps.isActive !== this.props.isActive && (nextProps.isActive === true)){
if(nextProps.isActive !== this.props.isActive && nextProps.isActive === true){
this.scrollFocus();
this.props.setFocus(this.props.id);
}
Expand All @@ -33,19 +33,14 @@ class Item extends React.Component {
}

handleClick(e){
e.preventDefault();
if(e.nativeEvent){
e.nativeEvent.preventDefault();
e.nativeEvent.stopImmediatePropagation();
}
EventUtil.trapImmediate(e);
return this.props.onSelect(this.props.id);
}

//Scroll menu item based on up/down mouse keys (assumes all items are the same height)
scrollFocus(){
const height = React.findDOMNode(this).offsetHeight;
if(height && this.props.handleItemFocus){
this.props.handleItemFocus(this.props.index,height);
}
if(height && this.props.handleItemFocus) this.props.handleItemFocus(this.props.index,height);
}

render(){
Expand All @@ -55,18 +50,16 @@ class Item extends React.Component {

return (
//IMPORTANT: anchor id is used to set lookup's input's aria-activedescendant
<li
className={className}
role="presentaion">
<li className={className} role="presentaion">
<a
href={this.props.href}
id={id}
tabIndex="-1"
aria-disabled={this.props.disabled}
aria-disabled={this.props.isDisabled}
role="option"
onClick={this.handleClick.bind(this)}
onMouseDown={this.handleClick.bind(this)}>
<Icon name="account" />
<Icon name={this.props.type} />
{ this.boldSearchText(this.props.children.label) }
</a>
</li>
Expand All @@ -75,12 +68,17 @@ class Item extends React.Component {
}

Item.propTypes = {
key: React.PropTypes.string,
id: React.PropTypes.string,
setFocus: React.PropTypes.func,
scrollFocus: React.PropTypes.func,
href: React.PropTypes.string,
type: React.PropTypes.string,
searchTerm: React.PropTypes.string,
index: React.PropTypes.number,
isActive: React.PropTypes.bool,
isDisabled: React.PropTypes.bool,
setFocus: React.PropTypes.func,
handleItemFocus: React.PropTypes.func,
onSelect: React.PropTypes.func,
searchTerm: React.PropTypes.string,
};

Item.defaultProps = {
Expand Down
90 changes: 52 additions & 38 deletions components/SLDSLookup/Menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import React, { Component } from 'react';
import Item from './Item';
import ActionItem from './ActionItem';
import {Icon} from "../../SLDSIcons";

class Menu extends React.Component {
Expand All @@ -17,6 +18,7 @@ class Menu extends React.Component {
this.state = {};
}

//Set filtered list length in parent to determine active indexes for aria-activedescendent
componentDidUpdate(prevProps, prevState){
let list = React.findDOMNode(this.refs.list).children.length;
this.props.getListLength(list);
Expand All @@ -26,6 +28,7 @@ class Menu extends React.Component {
return this.props.filterWith(this.props.searchTerm, item);
}

//Scroll menu up/down when using mouse keys
handleItemFocus (itemIndex, itemHeight) {
if(this.refs.list){
React.findDOMNode(this.refs.list).scrollTop = itemIndex * itemHeight;
Expand All @@ -35,66 +38,77 @@ class Menu extends React.Component {
renderItems(){
return this.props.items.filter(this.filter, this).map((c, i) => {
//isActive means it is aria-activedescendant
const isActive = this.props.focusIndex === i ? true : false;
const isActive = this.props.focusIndex === i + 1 ? true : false;
return <Item
key={c.id}
id={c.id}
type={this.props.type}
searchTerm={this.props.searchTerm}
index={i}
setFocus={this.props.setFocus}
isActive={isActive}
setFocus={this.props.setFocus}
handleItemFocus={this.handleItemFocus.bind(this)}
onSelect={this.props.onSelect}
searchTerm={this.props.searchTerm}>{c}</Item>
>
{c}
</Item>
});
}

renderEmptyState(){
let className = 'slds-lookup__item';
return (
<li
className={className}
role="presentaion">
<a
href='#'
id='add-item'
tabIndex="-1"
role="option"
onClick={this.props.addItem}
onMouseDown={this.props.addItem}>
<Icon name="add" category="utility" size="x-small" className="slds-icon-text-default" />
New {this.props.type}
</a>
</li>
);
}

render(){
let isNewItemBtnActive = false;
let isSearchDetailsActive = false;
this.props.focusIndex === this.props.listLength + 1 ? isNewItemBtnActive = true : isNewItemBtnActive = false;
this.props.focusIndex === 0 ? isSearchDetailsActive = true: isSearchDetailsActive = false;

return (
<div
className="ignore-react-onclickoutside slds-lookup__menu"
role="listbox"
ref="scroll">
<ul className="slds-lookup__list"
role="presentation"
ref="list">
{this.renderItems()}
{this.renderEmptyState()}
</ul>
<div className="ignore-react-onclickoutside slds-lookup__menu" role="listbox" ref="scroll">
<div className="slds-lookup__item">
<ActionItem
id='searchDetails'
icon='search'
type={this.props.type}
isActive={isSearchDetailsActive}
setFocus={this.props.setFocus}
onSelect={this.props.addItem}
>
{this.props.searchTerm ? '"' + this.props.searchTerm + '"' : ""} in {this.props.type + 's'}
</ActionItem>
</div>

<ul className="slds-lookup__list" role="presentation" ref="list">
{this.renderItems()}
</ul>

<div className="slds-lookup__item">
<ActionItem
id='addNewItem'
icon='add'
type={this.props.type}
isActive={isNewItemBtnActive}
setFocus={this.props.setFocus}
onSelect={this.props.addItem}
>
New {this.props.type}
</ActionItem>
</div>
</div>
)
}
}

Menu.propTypes = {
searchTerm: React.PropTypes.string,
filterWith: React.PropTypes.func,
onSelect: React.PropTypes.func,
label: React.PropTypes.string,
type: React.PropTypes.string,
focusIndex: React.PropTypes.number,
listLength: React.PropTypes.number,
items: React.PropTypes.array,
setFocus: React.PropTypes.func,
filterWith: React.PropTypes.func,
getListLength: React.PropTypes.func,
listLength: React.PropTypes.number,
focusIndex: React.PropTypes.number,
setFocus: React.PropTypes.func,
onSelect: React.PropTypes.func,
addItem: React.PropTypes.func,
};

Menu.defaultProps = {
Expand Down
Loading