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

chore: add typescript types for BaseCard #3320

Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 37 additions & 26 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';
Comment on lines +7 to 8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If people are happy with this implementation for BaseCard I'd be more than happy to update CardBody in a similar manner so we don't need this @ts-ignore here


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,
bradenmacdonald marked this conversation as resolved.
Show resolved Hide resolved
children,
as: Component,
as: Component = 'div',
...props
},
ref,
Expand All @@ -37,24 +69,14 @@ const BaseCard = React.forwardRef(
},
);

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

/* 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, '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 All @@ -67,15 +89,4 @@ BaseCard.propTypes = {
children: PropTypes.node,
};

BaseCard.defaultProps = {
prefix: undefined,
hasBody: false,
as: 'div',
borderColor: undefined,
className: undefined,
children: undefined,
bgColor: undefined,
textColor: undefined,
};

export default BaseCard;
2 changes: 1 addition & 1 deletion src/Card/tests/BaseCard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('BaseCard Component', () => {
);
const contentElement = screen.getByText('Direct Content');
expect(contentElement).toBeInTheDocument();
expect(contentElement.closest('div')).not.toHaveClass('card-body');
expect(contentElement.closest('div')).not.toHaveClass('pgn__card-body');
});

it('supports a custom tag with the `as` prop', () => {
Expand Down
Loading