Skip to content
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

[ListItem] Exposing onTouchTap API in order to address Issue #6938 #7105

Merged
merged 1 commit into from Jun 12, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/List/ListItem.js
Expand Up @@ -250,7 +250,11 @@ class ListItem extends Component {
onTouchEnd: PropTypes.func,
/** @ignore */
onTouchStart: PropTypes.func,
/** @ignore */
/**
* Callback function fired when the list item is touch-tapped.
*
* @param {object} event TouchTap event targeting the list item.
*/
onTouchTap: PropTypes.func,
/**
* Control toggle state of nested list.
Expand Down Expand Up @@ -459,6 +463,16 @@ class ListItem extends Component {
this.props.onMouseLeave(event);
};

handleTouchTap = (event) => {
if (this.props.onTouchTap) {
this.props.onTouchTap(event);
}

if (this.props.primaryTogglesNestedList) {
this.handleNestedListToggle(event);
}
};

handleNestedListToggle = (event) => {
event.stopPropagation();

Expand Down Expand Up @@ -563,7 +577,6 @@ class ListItem extends Component {
onMouseLeave, // eslint-disable-line no-unused-vars
onNestedListToggle, // eslint-disable-line no-unused-vars
onTouchStart, // eslint-disable-line no-unused-vars
onTouchTap,
rightAvatar,
rightIcon,
rightIconButton,
Expand Down Expand Up @@ -708,7 +721,7 @@ class ListItem extends Component {
onMouseEnter={this.handleMouseEnter}
onTouchStart={this.handleTouchStart}
onTouchEnd={this.handleTouchEnd}
onTouchTap={primaryTogglesNestedList ? this.handleNestedListToggle : onTouchTap}
onTouchTap={this.handleTouchTap}
ref={(node) => this.button = node}
style={Object.assign({}, styles.root, style)}
>
Expand Down
14 changes: 14 additions & 0 deletions src/List/ListItem.spec.js
Expand Up @@ -2,6 +2,7 @@
import React from 'react';
import {shallow} from 'enzyme';
import {assert} from 'chai';
import {spy} from 'sinon';
import ListItem from './ListItem';
import EnhancedButton from '../internal/EnhancedButton';
import getMuiTheme from '../styles/getMuiTheme';
Expand Down Expand Up @@ -76,6 +77,19 @@ describe('<ListItem />', () => {
assert.strictEqual(wrapper.find(`.${testClass}`).length, 1, 'should have a div with the test class');
});

it('should trigger onTouchTap handler when appropriate.', () => {
const onTouchTap = spy();
const wrapper = shallowWithContext(
<ListItem
onTouchTap={onTouchTap}
/>
);
const primaryTextButton = wrapper.find(EnhancedButton);

primaryTextButton.simulate('touchTap', {stopPropagation: () => {}});
assert.strictEqual(onTouchTap.callCount, 1);
});

describe('prop: primaryTogglesNestedList', () => {
it('should toggle nested list when true', () => {
const wrapper = shallowWithContext(
Expand Down