From 557209c8ca7f0d74e232cb0464c29b5ab45b9276 Mon Sep 17 00:00:00 2001 From: David Conner Date: Tue, 3 Nov 2020 09:36:35 -0500 Subject: [PATCH] feat(card): fix story for selection --- src/components/Card/Card.jsx | 21 +------ src/components/Card/Card.story.jsx | 59 ++++++++++++------- .../Card/__snapshots__/Card.story.storyshot | 1 + 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/components/Card/Card.jsx b/src/components/Card/Card.jsx index 25949ff03f..91fa7d166d 100644 --- a/src/components/Card/Card.jsx +++ b/src/components/Card/Card.jsx @@ -60,23 +60,6 @@ const CardWrapper = ({ setIsCardSelected(isSelected); }, [isSelected, setIsCardSelected]); - const handleSelection = (e) => { - if (e.type === 'click' || e.key === 'Enter') { - setIsCardSelected(true); - } - }; - - const handleBlur = (e) => { - if ( - e.relatedTarget.classList.contains( - `${iotPrefix}--card iot--card--wrapper` - ) - ) { - setIsCardSelected(false); - } - onBlur(); - }; - return (
handleBlur(e)} - onKeyDown={handleSelection} - onClick={handleSelection} + onBlur={onBlur} tabIndex={tabIndex} className={classnames(className, `${iotPrefix}--card--wrapper`, { [`${iotPrefix}--card--wrapper__selected`]: isCardSelected, diff --git a/src/components/Card/Card.story.jsx b/src/components/Card/Card.story.jsx index 937c9e0f22..2c6910a196 100644 --- a/src/components/Card/Card.story.jsx +++ b/src/components/Card/Card.story.jsx @@ -32,27 +32,44 @@ export const getDataStateProp = () => ({ storiesOf('Watson IoT/Card', module) .add('basic', () => { - const size = select('size', Object.keys(CARD_SIZES), CARD_SIZES.MEDIUM); - return ( -
- -
- ); + const StatefulExample = () => { + const [selected, setSelected] = React.useState(false); + const size = select('size', Object.keys(CARD_SIZES), CARD_SIZES.MEDIUM); + const handleClick = () => { + setSelected(true); + }; + const handleBlur = (e) => { + if ( + !e.currentTarget.contains(e.relatedTarget) || + (e.target === e.currentTarget && e.relatedTarget === null) + ) { + setSelected(false); + } + action('onBlur'); + }; + return ( +
+ +
+ ); + }; + return ; }) .add('with ellipsed title tooltip & external tooltip', () => { const size = select('size', Object.keys(CARD_SIZES), CARD_SIZES.MEDIUM); diff --git a/src/components/Card/__snapshots__/Card.story.storyshot b/src/components/Card/__snapshots__/Card.story.storyshot index 1951fdc142..760954b607 100644 --- a/src/components/Card/__snapshots__/Card.story.storyshot +++ b/src/components/Card/__snapshots__/Card.story.storyshot @@ -25,6 +25,7 @@ exports[`Storybook Snapshot tests and console checks Storyshots Watson IoT/Card data-testid="Card" id="facilitycard-basic" onBlur={[Function]} + onClick={[Function]} onFocus={[Function]} role="presentation" style={