Skip to content

Lookup sections #260

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 7 commits into from
Apr 28, 2016
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
45 changes: 45 additions & 0 deletions components/SLDSLookup/Menu/DefaultSectionHeader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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 from 'react';
import { EventUtil } from '../../../utils';

const displayName = "LookupDefaultSectionHeader";
const propTypes = {
data: React.PropTypes.object,
key: React.PropTypes.string

};

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

handleMouseDown(event) {
EventUtil.trapImmediate(event);
}

render(){
return (
<li className="slds-p-around--x-small"
tabIndex="-1"
>
<span className="slds-m-left--x-small">
<strong>{this.props.data.label}</strong>
</span>
</li>
)
}
}

DefaultSectionHeader.displayName = displayName;
DefaultSectionHeader.propTypes = propTypes;

module.exports = DefaultSectionHeader;
53 changes: 46 additions & 7 deletions components/SLDSLookup/Menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import ReactDOM from 'react-dom';

import Item from './Item';

import DefaultSectionHeader from './DefaultSectionHeader';

const displayName = 'SLDSLookup-Menu';
const propTypes = {
boldRegex: React.PropTypes.instanceOf(RegExp),
Expand All @@ -32,23 +34,50 @@ const defaultProps = {
class Menu extends React.Component {
constructor(props){
super(props);
this.state = {};
this.state = {filteredItems:this.filteredItems()};
}

//Set filtered list length in parent to determine active indexes for aria-activedescendent
componentDidUpdate(){
componentDidUpdate(prevProps){
// make an array of the children of the list but only count the actual items
let list = [].slice.call(ReactDOM.findDOMNode(this.refs.list).children)
.filter((child) => child.className.indexOf("slds-lookup__item") > -1).length;
this.props.getListLength(list);
if(
prevProps.items !== this.props.items ||
prevProps.filter !== this.props.filter ||
prevProps.searchTerm !== this.props.searchTerm
){
this.setState({
filteredItems:this.filteredItems()
});
}
}

filter(item){
return this.props.filterWith(this.props.searchTerm, item);
}

filteredItems() {
return this.props.items.filter(this.filter, this)
return this.filterEmptySections(this.props.items.filter(this.filter, this));
}

filterEmptySections(items){
const result = [];
items.forEach((item,index) => {
if(item && item.data && item.data.type === 'section'){
if(index+1<items.length){
const nextItem = items[index+1];
if(nextItem.data && nextItem.data.type !== 'section'){
result.push(item);
}
}
}
else{
result.push(item);
}
});
return result;
}

//Scroll menu up/down when using mouse keys
Expand All @@ -58,6 +87,12 @@ class Menu extends React.Component {
}
}

getFilteredItemForIndex(i){
if(i>-1 && this.state.filteredItems && i< this.state.filteredItems.length){
return this.state.filteredItems[i];
}
}

renderHeader(){
return this.props.header;
}
Expand All @@ -67,14 +102,18 @@ class Menu extends React.Component {
}

renderItems(){
return this.filteredItems().map((c, i) => {
let focusIndex = this.props.focusIndex;
return this.state.filteredItems.map((c, i) => {
//isActive means it is aria-activedescendant
const id = c.id;
let isActive = false;
if (this.props.header) {
isActive = this.props.focusIndex === i + 1 ? true : false;
isActive = focusIndex === i + 1 ? true : false;
}else{
isActive = this.props.focusIndex === i ? true : false;
isActive = focusIndex === i ? true : false;
}
if(c.data.type==='section'){
return <DefaultSectionHeader data={c.data} key={'section_header_'+i}/>;
}
return <Item
boldRegex={this.props.boldRegex}
Expand All @@ -98,7 +137,7 @@ class Menu extends React.Component {
}

renderContent(){
if (this.filteredItems().length === 0)
if (this.state.filteredItems.length === 0)
return (
<li className="slds-lookup__message" aria-live="polite">
<span className="slds-m-left--x-large slds-p-vertical--medium">{this.props.emptyMessage}</span>
Expand Down
32 changes: 28 additions & 4 deletions components/SLDSLookup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const propTypes = {
*/
const defaultFilter = (term, item) => {
if(!term) return true;
return item.label.match(new RegExp(escapeRegExp(term), "ig"));
return (item.data && item.data.type === 'section') || item.label.match(new RegExp(escapeRegExp(term), "ig"));
};


Expand Down Expand Up @@ -162,17 +162,40 @@ class SLDSLookup extends React.Component {
this.setState({items: items});
}

setFirstIndex(){
let numFocusable = this.getNumFocusableItems();
let nextFocusIndex = 0;
let filteredItem = this.state.items[0];
if(this.refs.menu && this.refs.menu.getFilteredItemForIndex){
filteredItem = this.refs.menu.getFilteredItemForIndex(nextFocusIndex);
}
if(filteredItem && filteredItem.data.type === 'section'){
nextFocusIndex++;
}
this.setState({ focusIndex: nextFocusIndex });
}
//=================================================
// Using down/up keys, set Focus on list item and assign it to aria-activedescendant attribute in input.
// 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.
increaseIndex() {
let numFocusable = this.getNumFocusableItems();
this.setState({ focusIndex: this.state.focusIndex < numFocusable ? this.state.focusIndex + 1 : 0 });
let nextFocusIndex = this.state.focusIndex < numFocusable ? this.state.focusIndex + 1 : 0;
const filteredItem = this.refs.menu.getFilteredItemForIndex(nextFocusIndex);
if(filteredItem && filteredItem.data.type === 'section'){
nextFocusIndex++;
}
this.setState({ focusIndex: nextFocusIndex });
}

decreaseIndex() {
let numFocusable = this.getNumFocusableItems();
this.setState({ focusIndex: this.state.focusIndex > 0 ? this.state.focusIndex - 1 : numFocusable});
let prevFocusIndex = this.state.focusIndex > 0 ? this.state.focusIndex - 1 : numFocusable;
const filteredItem = this.refs.menu.getFilteredItemForIndex(prevFocusIndex);
console.log('filteredItem', filteredItem);
if(filteredItem && filteredItem.data.type === 'section'){
prevFocusIndex === 0 ? prevFocusIndex = numFocusable : prevFocusIndex--;
}
this.setState({ focusIndex: prevFocusIndex});
}

setFocus(id) {
Expand Down Expand Up @@ -285,7 +308,7 @@ class SLDSLookup extends React.Component {
//If user hits down key, advance aria activedescendant to next item
if(event.keyCode === KEYS.DOWN){
EventUtil.trapImmediate(event);
this.state.focusIndex === null ? this.setState({ focusIndex: 0 }) : this.increaseIndex();
this.state.focusIndex === null ? this.setFirstIndex() : this.increaseIndex();
}
//If user hits up key, advance aria activedescendant to previous item
else if(event.keyCode === KEYS.UP){
Expand Down Expand Up @@ -362,6 +385,7 @@ class SLDSLookup extends React.Component {
renderMenuContent() {
if(this.state.isOpen){
return <Menu
ref="menu"
emptyMessage={this.props.emptyMessage}
filterWith={this.props.filterWith}
focusIndex={this.state.focusIndex}
Expand Down
4 changes: 4 additions & 0 deletions demo/code-snippets/LookupExamples3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
onChange={function(newValue){console.log("New search term: ", newValue)}}
onSelect={function(item){console.log(item , " Selected")}}
options={[
{type:'section', label:'SECTION 1'},
{label: "Paddy\"s Pub"},
{label: "Tyrell Corp"},
{type:'section', label:'SECTION 2'},
{label: "Paper St. Soap Company"},
{label: "Nakatomi Investments"},
{label: "Acme Landscaping"},
{type:'section', label:'SECTION 3'},
{label: "Acme Construction"}
]}
/>

Loading