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

feat(Chip): allow using badge in selectable chips #1134

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/__private_stories__/skin-components-story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ export const Default: StoryComponent<Args> = ({variant}) => {

{/** Chip */}
<Inline space={16} wrap>
<Chip badge={10} Icon={IconLightningRegular}>
Default chip
</Chip>
<Chip Icon={IconLightningRegular} active>
Active chip
</Chip>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
marcoskolodny marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 11 additions & 7 deletions src/__screenshot_tests__/chip-screenshot-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ test.each(CHIP_OPTIONS)('Chip - %s', async (option) => {
});

test.each`
selection | inverse
${'single'} | ${false}
${'single'} | ${true}
${'multiple'} | ${false}
${'multiple'} | ${true}
`('Chip - $selection selection (inverse = $inverse)', async ({selection, inverse}) => {
selection | inverse | badge
${'single'} | ${false} | ${undefined}
${'single'} | ${true} | ${undefined}
${'multiple'} | ${false} | ${undefined}
${'multiple'} | ${true} | ${undefined}
${'single'} | ${false} | ${2}
${'single'} | ${true} | ${2}
${'multiple'} | ${false} | ${2}
${'multiple'} | ${true} | ${2}
`('Chip - $selection selection (inverse = $inverse, badge = $badge)', async ({selection, inverse, badge}) => {
const page = await openStoryPage({
id: `components-chip--${selection}-selection`,
device: 'DESKTOP',
args: {inverse, withIcon: true},
args: {inverse, withIcon: true, badge},
});

const story = await screen.findByTestId(`chip-${selection}-selection`);
Expand Down
20 changes: 13 additions & 7 deletions src/__stories__/chip-story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ export const Default: StoryComponent<Args> = ({inverse, withIcon, closable, badg
);
};

export const SingleSelection: StoryComponent<Omit<Args, 'closable' | 'badge'>> = ({inverse, withIcon}) => {
export const SingleSelection: StoryComponent<Omit<Args, 'closable'>> = ({inverse, badge, withIcon}) => {
const props = {
Icon: withIcon ? IconLightningFilled : undefined,
badge: badge !== 'undefined' ? +badge : undefined,
};

return (
Expand Down Expand Up @@ -106,9 +107,10 @@ export const SingleSelection: StoryComponent<Omit<Args, 'closable' | 'badge'>> =
);
};

export const MultipleSelection: StoryComponent<Omit<Args, 'closable' | 'badge'>> = ({inverse, withIcon}) => {
export const MultipleSelection: StoryComponent<Omit<Args, 'closable'>> = ({inverse, badge, withIcon}) => {
const props = {
Icon: withIcon ? IconLightningFilled : undefined,
badge: badge !== 'undefined' ? +badge : undefined,
};

return (
Expand Down Expand Up @@ -150,15 +152,19 @@ const defaultArgs = {
closable: false,
};

Default.storyName = 'Chip';

Default.argTypes = {
const defaultArgTypes = {
badge: {
options: badgeOptions,
control: {type: 'select'},
},
};

Default.storyName = 'Chip';
Default.argTypes = defaultArgTypes;
Default.args = defaultArgs;
SingleSelection.args = {...(({closable, badge, ...o}) => o)(defaultArgs)};
MultipleSelection.args = {...(({closable, badge, ...o}) => o)(defaultArgs)};

SingleSelection.argTypes = defaultArgTypes;
SingleSelection.args = {...(({closable, ...o}) => o)(defaultArgs)};

MultipleSelection.argTypes = defaultArgTypes;
MultipleSelection.args = {...(({closable, ...o}) => o)(defaultArgs)};
33 changes: 9 additions & 24 deletions src/chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ const Chip: React.FC<ChipProps> = (props: ChipProps) => {
const paddingRight = {mobile: 20, desktop: 12} as const;
const paddingIcon = {mobile: 16, desktop: 8} as const;

const renderBadge = () => {
if (!badge) {
return null;
}
return <>{badge === true ? <Badge /> : <Badge value={badge} />}</>;
};

const body = (
<>
{Icon && (
Expand All @@ -69,22 +62,6 @@ const Chip: React.FC<ChipProps> = (props: ChipProps) => {
</>
);

if (badge) {
return (
<Box
className={classnames(
overAlternative ? styles.chipVariants.overAlternative : styles.chipVariants.default,
styles.chipWrapper
)}
paddingLeft={paddingLeft}
paddingRight={paddingIcon}
{...getPrefixedDataAttributes(dataAttributes, 'Chip')}
>
{body}
{renderBadge()}
</Box>
);
}
if (onClose) {
return (
<Box
Expand Down Expand Up @@ -118,6 +95,13 @@ const Chip: React.FC<ChipProps> = (props: ChipProps) => {

const chipDataAttributes = {'component-name': 'Chip', ...dataAttributes};

const renderBadge = () => {
if (!badge) {
return null;
}
return <>{badge === true ? <Badge /> : <Badge value={badge} />}</>;
};

const renderContent = (dataAttributes?: DataAttributes) => (
<Box
className={classnames(
Expand All @@ -129,10 +113,11 @@ const Chip: React.FC<ChipProps> = (props: ChipProps) => {
}
)}
paddingLeft={paddingLeft}
paddingRight={paddingRight}
paddingRight={badge ? paddingIcon : paddingRight}
dataAttributes={dataAttributes}
>
{body}
{renderBadge()}
</Box>
);

Expand Down
Loading