Skip to content

Commit fcdecdb

Browse files
feat(Tag): add as prop to Tag to specify wrapping element (#13959) (#14112)
* feat(Tag): add as prop to Tag to specify wrapping element (#13959) * fixup! updated snapshots * chore: update contributors list --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 84cdca4 commit fcdecdb

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,15 @@
12141214
"contributions": [
12151215
"code"
12161216
]
1217+
},
1218+
{
1219+
"login": "tulivlk",
1220+
"name": "Petr Kadlec",
1221+
"avatar_url": "https://avatars.githubusercontent.com/u/67226666?v=4",
1222+
"profile": "https://github.com/tulivlk",
1223+
"contributions": [
1224+
"code"
1225+
]
12171226
}
12181227
],
12191228
"commitConvention": "none"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
246246
<td align="center"><a href="https://github.com/anton-tsymuk-viacomcbs"><img src="https://avatars.githubusercontent.com/u/112623876?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anton Tsymuk</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=anton-tsymuk-viacomcbs" title="Code">💻</a></td>
247247
<td align="center"><a href="https://github.com/modaslam"><img src="https://avatars.githubusercontent.com/u/33179527?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mohammed Aslam P. A.</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=modaslam" title="Code">💻</a> <a href="https://github.com/carbon-design-system/carbon/commits?author=modaslam" title="Documentation">📖</a></td>
248248
<td align="center"><a href="https://github.com/tongyy"><img src="https://avatars.githubusercontent.com/u/24784725?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tony ZL</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=tongyy" title="Code">💻</a></td>
249+
<td align="center"><a href="https://github.com/tulivlk"><img src="https://avatars.githubusercontent.com/u/67226666?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Petr Kadlec</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=tulivlk" title="Code">💻</a></td>
249250
</tr>
250251
</table>
251252

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8278,6 +8278,9 @@ Map {
82788278
},
82798279
"Tag" => Object {
82808280
"propTypes": Object {
8281+
"as": Object {
8282+
"type": "elementType",
8283+
},
82818284
"children": Object {
82828285
"type": "node",
82838286
},

packages/react/src/components/Tag/Tag.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const Tag = ({
3939
disabled,
4040
onClose,
4141
size,
42+
as: BaseComponent,
4243
...other
4344
}) => {
4445
const prefix = usePrefix();
@@ -59,8 +60,9 @@ const Tag = ({
5960
};
6061

6162
if (filter) {
63+
const ComponentTag = BaseComponent ?? 'div';
6264
return (
63-
<div className={tagClasses} id={tagId} {...other}>
65+
<ComponentTag className={tagClasses} id={tagId} {...other}>
6466
<span
6567
className={`${prefix}--tag__label`}
6668
title={typeof children === 'string' ? children : null}>
@@ -75,11 +77,11 @@ const Tag = ({
7577
title={title}>
7678
<Close />
7779
</button>
78-
</div>
80+
</ComponentTag>
7981
);
8082
}
8183

82-
const ComponentTag = other.onClick ? 'button' : 'div';
84+
const ComponentTag = BaseComponent ?? (other.onClick ? 'button' : 'div');
8385

8486
return (
8587
<ComponentTag
@@ -102,6 +104,12 @@ const Tag = ({
102104
};
103105

104106
Tag.propTypes = {
107+
/**
108+
* Provide an alternative tag or component to use instead of the default
109+
* wrapping element
110+
*/
111+
as: PropTypes.elementType,
112+
105113
/**
106114
* Provide content to be rendered inside of a <Tag>
107115
*/

0 commit comments

Comments
 (0)