Skip to content

Commit

Permalink
chore: add typescript types for BaseCard
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-smith-tcril committed Dec 5, 2024
1 parent f3beb20 commit 2a0f617
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/Card/BaseCard.jsx → src/Card/BaseCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,53 @@ 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 BaseCard = React.forwardRef(
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,
hasBody = false,
children,
as: Component,
as: Component = 'div',
...props
},
ref,
Expand All @@ -37,24 +69,13 @@ const BaseCard = React.forwardRef(
},
);

const colorVariants = [
'primary',
'secondary',
'success',
'danger',
'warning',
'info',
'dark',
'light',
];

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, 'white', 'muted']),
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. */
Expand Down
11 changes: 11 additions & 0 deletions src/Card/tests/BaseCard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ describe('BaseCard Component', () => {
});

it('renders children directly when hasBody is false', () => {
render(
<BaseCard hasBody={false}>
<span>Direct Content</span>
</BaseCard>,
);
const contentElement = screen.getByText('Direct Content');
expect(contentElement).toBeInTheDocument();
expect(contentElement.closest('div')).not.toHaveClass('card-body');
});

it('renders children directly when hasBody isn\'t set', () => {
render(
<BaseCard>
<span>Direct Content</span>
Expand Down

0 comments on commit 2a0f617

Please sign in to comment.