-
Notifications
You must be signed in to change notification settings - Fork 72
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: Replaced bootstrap BaseCard component #3318
Merged
brian-smith-tcril
merged 3 commits into
alpha
from
Peter_Kulko/remove-bootstrap-base-card
Dec 11, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
import type { ComponentWithAsProp, BsPropsWithAs } from '../utils/types/bootstrap'; | ||
|
||
// @ts-ignore | ||
import CardBody from './CardBody'; | ||
|
||
const BASE_CARD_CLASSNAME = 'card'; | ||
|
||
const colorVariants = [ | ||
'primary', | ||
'secondary', | ||
'success', | ||
'danger', | ||
'warning', | ||
'info', | ||
'dark', | ||
'light', | ||
] as const; | ||
|
||
const textVariants = [ | ||
'white', | ||
'muted', | ||
] as const; | ||
|
||
type ColorVariant = typeof colorVariants[number]; | ||
type TextVariant = typeof textVariants[number]; | ||
interface Props extends BsPropsWithAs { | ||
prefix?: string; | ||
bgColor?: ColorVariant; | ||
textColor?: ColorVariant | TextVariant; | ||
borderColor?: ColorVariant; | ||
hasBody?: boolean; | ||
className?: string; | ||
children: React.ReactNode; | ||
} | ||
type BaseCardType = ComponentWithAsProp<'div', Props>; | ||
|
||
const BaseCard : BaseCardType = React.forwardRef<HTMLDivElement, Props>( | ||
( | ||
{ | ||
prefix, | ||
className, | ||
bgColor, | ||
textColor, | ||
borderColor, | ||
hasBody = false, | ||
children, | ||
as: Component = 'div', | ||
...props | ||
}, | ||
ref, | ||
) => { | ||
const classes = classNames( | ||
className, | ||
prefix ? `${prefix}-${BASE_CARD_CLASSNAME}` : BASE_CARD_CLASSNAME, | ||
bgColor && `bg-${bgColor}`, | ||
textColor && `text-${textColor}`, | ||
borderColor && `border-${borderColor}`, | ||
); | ||
|
||
return ( | ||
<Component ref={ref} {...props} className={classes}> | ||
{hasBody ? <CardBody>{children}</CardBody> : children} | ||
</Component> | ||
); | ||
}, | ||
); | ||
|
||
/* eslint-disable react/require-default-props */ | ||
BaseCard.propTypes = { | ||
/** Prefix for component CSS classes. */ | ||
prefix: PropTypes.string, | ||
/** Background color of the card. */ | ||
bgColor: PropTypes.oneOf(colorVariants), | ||
/** Text color of the card. */ | ||
textColor: PropTypes.oneOf([...colorVariants, ...textVariants]), | ||
/** Border color of the card. */ | ||
borderColor: PropTypes.oneOf(colorVariants), | ||
/** Determines whether the card should render its children inside a `CardBody` wrapper. */ | ||
hasBody: PropTypes.bool, | ||
/** Set a custom element for this component. */ | ||
as: PropTypes.elementType, | ||
/** Additional CSS class names to apply to the card element. */ | ||
className: PropTypes.string, | ||
/** The content to render inside the card. */ | ||
children: PropTypes.node, | ||
}; | ||
|
||
export default BaseCard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import BaseCard from '../BaseCard'; | ||
|
||
describe('BaseCard Component', () => { | ||
it('renders a default card', () => { | ||
render(<BaseCard>Default Card Content</BaseCard>); | ||
const cardElement = screen.getByText('Default Card Content'); | ||
expect(cardElement).toBeInTheDocument(); | ||
expect(cardElement).toHaveClass('card'); | ||
}); | ||
|
||
it('applies the correct background color', () => { | ||
render(<BaseCard bgColor="primary">Card with Background</BaseCard>); | ||
const cardElement = screen.getByText('Card with Background'); | ||
expect(cardElement).toHaveClass('bg-primary'); | ||
}); | ||
|
||
it('applies the correct text color', () => { | ||
render(<BaseCard textColor="muted">Card with Text Color</BaseCard>); | ||
const cardElement = screen.getByText('Card with Text Color'); | ||
expect(cardElement).toHaveClass('text-muted'); | ||
}); | ||
|
||
it('applies the correct border color', () => { | ||
render(<BaseCard borderColor="danger">Card with Border Color</BaseCard>); | ||
const cardElement = screen.getByText('Card with Border Color'); | ||
expect(cardElement).toHaveClass('border-danger'); | ||
}); | ||
|
||
it('renders children inside CardBody when hasBody is true', () => { | ||
render( | ||
<BaseCard hasBody> | ||
<span>Content in CardBody</span> | ||
</BaseCard>, | ||
); | ||
const cardBodyElement = screen.getByText('Content in CardBody'); | ||
expect(cardBodyElement).toBeInTheDocument(); | ||
expect(cardBodyElement.closest('div')).toHaveClass('pgn__card-body'); | ||
}); | ||
|
||
it('renders children directly when hasBody is false', () => { | ||
render( | ||
<BaseCard> | ||
<span>Direct Content</span> | ||
</BaseCard>, | ||
); | ||
const contentElement = screen.getByText('Direct Content'); | ||
expect(contentElement).toBeInTheDocument(); | ||
expect(contentElement.closest('div')).not.toHaveClass('pgn__card-body'); | ||
}); | ||
|
||
it('supports a custom tag with the `as` prop', () => { | ||
render( | ||
<BaseCard as="section"> | ||
<span>Custom Tag</span> | ||
</BaseCard>, | ||
); | ||
const sectionElement = screen.getByText('Custom Tag').closest('section'); | ||
expect(sectionElement).toBeInTheDocument(); | ||
expect(sectionElement).toHaveClass('card'); | ||
}); | ||
|
||
it('applies additional class names', () => { | ||
render(<BaseCard className="custom-class">Custom Class</BaseCard>); | ||
const cardElement = screen.getByText('Custom Class'); | ||
expect(cardElement).toHaveClass('custom-class'); | ||
}); | ||
|
||
it('uses prefix correctly', () => { | ||
render(<BaseCard prefix="test-prefix">Prefixed Card</BaseCard>); | ||
const cardElement = screen.getByText('Prefixed Card'); | ||
expect(cardElement).toHaveClass('test-prefix-card'); | ||
}); | ||
|
||
it('renders without children', () => { | ||
render(<BaseCard />); | ||
const cardElement = document.querySelector('.card'); | ||
expect(cardElement).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to land this PR as-is and make a small issue for adding types to
CardBody
that we can address in a follow-up PR@adamstankiewicz thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a new issue for this change, let me know if you have anything to add
#3332