Skip to content

Commit

Permalink
feat(card): fix story for selection
Browse files Browse the repository at this point in the history
  • Loading branch information
davidicus committed Nov 3, 2020
1 parent 2c56d07 commit 557209c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
21 changes: 1 addition & 20 deletions src/components/Card/Card.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div
role="presentation"
Expand All @@ -89,9 +72,7 @@ const CardWrapper = ({
onTouchStart={onTouchStart}
onScroll={onScroll}
onFocus={onFocus}
onBlur={(e) => handleBlur(e)}
onKeyDown={handleSelection}
onClick={handleSelection}
onBlur={onBlur}
tabIndex={tabIndex}
className={classnames(className, `${iotPrefix}--card--wrapper`, {
[`${iotPrefix}--card--wrapper__selected`]: isCardSelected,
Expand Down
59 changes: 38 additions & 21 deletions src/components/Card/Card.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div style={{ width: text('width', `450px`), margin: 20 }}>
<Card
title={text('title', 'Card Title')}
id="facilitycard-basic"
size={size}
isLoading={boolean('isLoading', false)}
isSelected={boolean('isSelected', false)}
isEmpty={boolean('isEmpty', false)}
isEditable={boolean('isEditable', false)}
isExpanded={boolean('isExpanded', false)}
breakpoint="lg"
availableActions={{ range: true, expand: true }}
onCardAction={action('onCardAction')}
onFocus={action('onFocus')}
onBlur={action('onBlur')}
tabIndex={0}
/>
</div>
);
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 (
<div style={{ width: text('width', `450px`), margin: 20 }}>
<Card
title={text('title', 'Card Title')}
id="facilitycard-basic"
size={size}
isLoading={boolean('isloading', false)}
isSelected={selected}
isEmpty={boolean('isEmpty', false)}
isEditable={boolean('isEditable', false)}
isExpanded={boolean('isExpanded', false)}
breakpoint="lg"
availableActions={{ range: true, expand: true }}
onCardAction={action('onCardAction')}
onFocus={action('onFocus')}
onBlur={handleBlur}
tabIndex={0}
onClick={handleClick}
/>
</div>
);
};
return <StatefulExample />;
})
.add('with ellipsed title tooltip & external tooltip', () => {
const size = select('size', Object.keys(CARD_SIZES), CARD_SIZES.MEDIUM);
Expand Down
1 change: 1 addition & 0 deletions src/components/Card/__snapshots__/Card.story.storyshot
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down

0 comments on commit 557209c

Please sign in to comment.