Skip to content

Commit

Permalink
Merge pull request #192 from web3ui/fix/card_state
Browse files Browse the repository at this point in the history
Fix/card state
  • Loading branch information
Y0moo authored Mar 4, 2022
2 parents b0f3978 + f7f4c40 commit e8507c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
19 changes: 16 additions & 3 deletions src/components/Card/Card.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { ComponentStory, ComponentMeta } from '@storybook/react';
import Card from './Card';
import React from 'react';
import React, { useState } from 'react';
import getModuleAnimation from './Animations/animations';
import { Icon, iconTypes } from '../Icon';
import colors from '../../styles/colors';
import { Button } from '../Button';
// import { useArgs } from '@storybook/addons';

/**
* To-do:
* find correct way of using useArgs with react testing
*/

export default {
title: '4.UI/Card',
component: Card,
} as ComponentMeta<typeof Card>;

const Template: ComponentStory<typeof Card> = (args) => {
// const [{ isSelected }, updateArgs] = useArgs();
const [isSelected, setIsSelected] = useState(false);
return (
<div style={{ width: '250px' }}>
<Card key={'0'} {...args} />
<Card
isSelected={isSelected}
{...args}
setIsSelected={setIsSelected}
// setIsSelected={() => updateArgs({ isSelected: !isSelected })}
/>
</div>
);
};
Expand All @@ -32,7 +45,7 @@ RegularSelected.args = {
tooltipText: 'Lorem Ipsum Dole met sai souni lokomit anici trenicid',
children: [<div key={'0'}>{getModuleAnimation(undefined)}</div>],
title: 'dApp',
selected: true,
isSelected: true,
description: 'Click to create a dApp',
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/Card/Card.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fonts from '../../styles/fonts';
import colors from '../../styles/colors';

const DivStyled = styled.div<
Pick<CardProps, 'selected' | 'isDisabled' | 'cursorType'>
Pick<CardProps, 'isSelected' | 'isDisabled' | 'cursorType'>
>`
${resetCSS};
${fonts.text};
Expand All @@ -32,7 +32,7 @@ const DivStyled = styled.div<
);
}`}
${(p) => p.cursorType === 'pointer' && 'cursor: pointer;'}
${(p) => p.selected && `outline-color: ${colors.green};`}
${(p) => p.isSelected && `outline-color: ${colors.green};`}
`;

const AbsoluteIconStyled = styled.div<AbsoluteIconStyledProps>`
Expand Down
23 changes: 6 additions & 17 deletions src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect, useState } from 'react';
import React from 'react';
import { CardProps } from './types';
import { iconTypes } from '../Icon/collection';
Expand All @@ -15,38 +14,28 @@ const Card: React.FC<CardProps> = ({
children,
cursorType = 'pointer',
description,
selected,
isSelected,
title,
tooltipText,
isDisabled = false,
setIsSelected,
}: CardProps) => {
const [isSelected, setSelected] = useState<boolean | undefined>(selected);
const [showCheckedIcon, toggleChecked] =
useState<boolean | undefined>(false);

useEffect(() => {
toggleChecked(selected);
}, [selected]);

return (
<DivStyled
aria-label={isSelected ? 'card not selected' : 'card selected'}
data-testid={'card-test-id'}
id={id}
onClick={() => {
if (isDisabled) {
return;
}
setSelected(!isSelected);
toggleChecked(!showCheckedIcon);
if (isDisabled || !setIsSelected) return;
setIsSelected(!isSelected);
}}
role="button"
selected={isSelected && showCheckedIcon}
isSelected={isSelected}
isDisabled={isDisabled}
cursorType={cursorType}
>
<HeaderStyled data-testid={'header-test-id'}>
{showCheckedIcon && (
{isSelected && (
<AbsoluteIconStyled position="topL">
<Icon
data-testid={'check-test-id'}
Expand Down
8 changes: 7 additions & 1 deletion src/components/Card/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface CardProps {
/**
* set if card is selected
*/
selected?: boolean;
isSelected?: boolean;

/**
* set title of card
Expand All @@ -38,6 +38,12 @@ export interface CardProps {
* Style of the cursor
*/
cursorType?: 'pointer' | 'default';

/**
* Sets isSelected state
*/

setIsSelected?: (value: boolean) => void;
}

export interface AbsoluteIconStyledProps {
Expand Down

0 comments on commit e8507c6

Please sign in to comment.