Skip to content

Commit

Permalink
fix(Tile): prevent spacebar in textinput from closing ExpandableTile (#…
Browse files Browse the repository at this point in the history
…8638)

* fix(Tile): prevent spacebar in textinput from closing ExpandableTile

* chore(tests): update snapshots

* fix(ExpandableTile): don't close on click if clicking on input

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
tw15egan and kodiakhq[bot] authored May 31, 2021
1 parent 41567a1 commit 9541752
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
6 changes: 6 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5773,6 +5773,12 @@ Map {
"onBeforeClick": Object {
"type": "func",
},
"onClick": Object {
"type": "func",
},
"onKeyUp": Object {
"type": "func",
},
"tabIndex": Object {
"type": "number",
},
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/Tile/Tile-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TileAboveTheFoldContent,
TileBelowTheFoldContent,
} from '../Tile';
import TextInput from '../TextInput';
import TileGroup from '../TileGroup';
import RadioTile from '../RadioTile';
import Link from '../Link';
Expand Down Expand Up @@ -169,7 +170,10 @@ export const Expandable = () => (
<div style={{ height: '200px' }}>Above the fold content here</div>
</TileAboveTheFoldContent>
<TileBelowTheFoldContent>
<div style={{ height: '400px' }}>Below the fold content here</div>
<div style={{ height: '400px' }}>
Below the fold content here
<TextInput id="test2" invalidText="A valid value is required" />
</div>
</TileBelowTheFoldContent>
</ExpandableTile>
);
35 changes: 21 additions & 14 deletions packages/react/src/components/Tile/Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@carbon/icons-react';
import { keys, matches } from '../../internal/keyboard';
import deprecate from '../../prop-types/deprecate';
import { composeEventHandlers } from '../../tools/events';

const { prefix } = settings;

Expand Down Expand Up @@ -428,6 +429,16 @@ export class ExpandableTile extends Component {
*/
onBeforeClick: PropTypes.func,

/**
* optional handler to trigger a function the Tile is clicked
*/
onClick: PropTypes.func,

/**
* optional handler to trigger a function when a key is pressed
*/
onKeyUp: PropTypes.func,

/**
* The `tabindex` attribute.
*/
Expand Down Expand Up @@ -528,7 +539,7 @@ export class ExpandableTile extends Component {
};

handleClick = (evt) => {
if (!this.props.onBeforeClick(evt)) {
if (!this.props.onBeforeClick(evt) || evt.target.tagName === 'INPUT') {
return;
}
evt.persist();
Expand All @@ -543,18 +554,11 @@ export class ExpandableTile extends Component {
);
};

handleKeyDown = (evt) => {
if (matches(evt, [keys.Enter, keys.Space])) {
evt.persist();
this.setState(
{
expanded: !this.state.expanded,
},
() => {
this.setMaxHeight();
this.props.handleClick(evt);
}
);
handleKeyUp = (evt) => {
if (evt.target !== this.tile) {
if (matches(evt, [keys.Enter, keys.Space])) {
evt.preventDefault();
}
}
};

Expand All @@ -570,6 +574,8 @@ export class ExpandableTile extends Component {
tileMaxHeight, // eslint-disable-line
tilePadding, // eslint-disable-line
handleClick, // eslint-disable-line
onClick,
onKeyUp,
tileCollapsedIconText,
tileExpandedIconText,
tileCollapsedLabel,
Expand Down Expand Up @@ -611,7 +617,8 @@ export class ExpandableTile extends Component {
aria-expanded={isExpanded}
title={isExpanded ? tileExpandedIconText : tileCollapsedIconText}
{...other}
onClick={this.handleClick}
onKeyUp={composeEventHandlers([onKeyUp, this.handleKeyUp])}
onClick={composeEventHandlers([onClick, this.handleClick])}
tabIndex={tabIndex}>
<div
ref={(tileContent) => {
Expand Down

0 comments on commit 9541752

Please sign in to comment.