Skip to content

Commit b578251

Browse files
committed
feat(CBreadcrumb): allow to pass custom components as breadcrumb item
1 parent 67106fb commit b578251

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

packages/coreui-react/src/components/breadcrumb/CBreadcrumbItem.tsx

+36-21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import React, { forwardRef, HTMLAttributes } from 'react'
1+
import React, { ElementType, forwardRef, HTMLAttributes } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

55
import { CLink } from '../link/CLink'
66

7+
import { PolymorphicRefForwardingComponent } from '../../helpers'
8+
79
export interface CBreadcrumbItemProps extends HTMLAttributes<HTMLLIElement> {
810
/**
911
* Toggle the active state for the component.
1012
*/
1113
active?: boolean
14+
/**
15+
* Component used for the root node. Either a string to use a HTML element or a component.
16+
*
17+
* @since 5.4.0
18+
*/
19+
as?: ElementType
1220
/**
1321
* A string of all className you want applied to the base component.
1422
*/
@@ -19,26 +27,33 @@ export interface CBreadcrumbItemProps extends HTMLAttributes<HTMLLIElement> {
1927
href?: string
2028
}
2129

22-
export const CBreadcrumbItem = forwardRef<HTMLLIElement, CBreadcrumbItemProps>(
23-
({ children, active, className, href, ...rest }, ref) => {
24-
return (
25-
<li
26-
className={classNames(
27-
'breadcrumb-item',
28-
{
29-
active: active,
30-
},
31-
className,
32-
)}
33-
{...(active && { 'aria-current': 'page' })}
34-
{...rest}
35-
ref={ref}
36-
>
37-
{href ? <CLink href={href}>{children}</CLink> : children}
38-
</li>
39-
)
40-
},
41-
)
30+
export const CBreadcrumbItem: PolymorphicRefForwardingComponent<'li', CBreadcrumbItemProps> =
31+
forwardRef<HTMLLIElement, CBreadcrumbItemProps>(
32+
({ children, active, as, className, href, ...rest }, ref) => {
33+
return (
34+
<li
35+
className={classNames(
36+
'breadcrumb-item',
37+
{
38+
active: active,
39+
},
40+
className,
41+
)}
42+
{...(active && { 'aria-current': 'page' })}
43+
{...rest}
44+
ref={ref}
45+
>
46+
{href ? (
47+
<CLink as={as} href={href}>
48+
{children}
49+
</CLink>
50+
) : (
51+
children
52+
)}
53+
</li>
54+
)
55+
},
56+
)
4257

4358
CBreadcrumbItem.propTypes = {
4459
active: PropTypes.bool,

packages/docs/content/api/CBreadcrumbItem.api.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ import CBreadcrumbItem from '@coreui/react/src/components/breadcrumb/CBreadcrumb
88
| Property | Description | Type | Default |
99
| --- | --- | --- | --- |
1010
| **active** | Toggle the active state for the component. | `boolean` | - |
11+
| **as** **_5.4.0+_** | Component used for the root node. Either a string to use a HTML element or a component. | `(ElementType & 'symbol')` \| `(ElementType & 'object')` \| `(ElementType & 'li')` \| `(ElementType & 'slot')` \| `(ElementType & 'style')` \| `... 174 more ...` \| `(ElementType & FunctionComponent<...>)` | - |
1112
| **className** | A string of all className you want applied to the base component. | `string` | - |
1213
| **href** | The `href` attribute for the inner `<CLink>` component. | `string` | - |

0 commit comments

Comments
 (0)