Skip to content

Commit

Permalink
feat: Add the as prop to Badge to support actionable badges (#4295)
Browse files Browse the repository at this point in the history
* feat: support actional bagdes by adding the as property to the Badge component (#4227)

* add 'as' prop to Badge.js

* add unit test that verifies 'as' prop is working

Signed-off-by: mhatvan <markus_hatvan@aon.at>

* * updated type definitions & simple.test.tsx for Badge

Signed-off-by: mhatvan <markus_hatvan@aon.at>

* feat: added another TS test case

* fix: closing tag

* fix: prettier
  • Loading branch information
Markus Hatvan authored and taion committed Aug 22, 2019
1 parent e8f20f5 commit db09dc0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/Badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ const propTypes = {
* some additional horizontal padding
*/
pill: PropTypes.bool.isRequired,

/** @default span */
as: PropTypes.elementType,
};

const defaultProps = {
pill: false,
};

const Badge = React.forwardRef(
({ bsPrefix, variant, pill, className, ...props }, ref) => {
(
{ bsPrefix, variant, pill, className, as: Component = 'span', ...props },
ref,
) => {
const prefix = useBootstrapPrefix(bsPrefix, 'badge');
return (
<span
<Component
ref={ref}
{...props}
className={classNames(
Expand Down
8 changes: 8 additions & 0 deletions test/BadgeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ describe('Badge', () => {
.text(),
).to.equal('Message');
});

it('should support custom `as`', () => {
mount(
<Badge as="a" href="#" variant="primary" pill>
Message
</Badge>,
).assertSingle('a[href="#"]');
});
});
7 changes: 5 additions & 2 deletions types/components/Badge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { BsPrefixComponent } from './helpers';

export interface BadgeProps extends React.HTMLProps<HTMLSpanElement> {
export interface BadgeProps {
bsPrefix?: string;
variant?:
| 'primary'
Expand All @@ -14,6 +15,8 @@ export interface BadgeProps extends React.HTMLProps<HTMLSpanElement> {
pill?: boolean;
}

declare const Badge: React.ForwardRefExoticComponent<BadgeProps>;
declare class Badge<
As extends React.ElementType = 'span'
> extends BsPrefixComponent<As, BadgeProps> {}

export default Badge;
4 changes: 3 additions & 1 deletion types/simple.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ import {
</Card>
</Accordion>;

<Badge pill={false} ref={React.createRef<HTMLSpanElement>()}>
<Badge pill={false}>42</Badge>;

<Badge as="a" href="#" variant="primary" pill>
42
</Badge>;

Expand Down

0 comments on commit db09dc0

Please sign in to comment.