Skip to content

Commit dfc11a4

Browse files
authored
Merge pull request #669 from devtron-labs/feat/anchor-support-in-button
feat: add support for anchor tag in Button component
2 parents 0b58cb5 + 69bddbb commit dfc11a4

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.10.6",
3+
"version": "1.10.8",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Components/Button/Button.component.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ButtonHTMLAttributes, MutableRefObject, PropsWithChildren, useEffect, useRef, useState } from 'react'
18-
import { Link, LinkProps } from 'react-router-dom'
17+
import { MutableRefObject, PropsWithChildren, useEffect, useRef, useState } from 'react'
18+
import { Link } from 'react-router-dom'
1919
import { Progressing } from '@Common/Progressing'
2020
import { Tooltip } from '@Common/Tooltip'
2121
import { TooltipProps } from '@Common/Tooltip/types'
@@ -26,6 +26,7 @@ import './button.scss'
2626

2727
const ButtonElement = ({
2828
component = ButtonComponentType.button,
29+
anchorProps,
2930
linkProps,
3031
buttonProps,
3132
onClick,
@@ -53,26 +54,44 @@ const ButtonElement = ({
5354
elementRef: MutableRefObject<HTMLButtonElement | HTMLAnchorElement>
5455
}
5556
>) => {
57+
// Added the specific class to ensure that the link override is applied
58+
const linkOrAnchorClassName = `${props.className} button__link ${props.disabled ? 'dc__disable-click' : ''}`
59+
5660
if (component === ButtonComponentType.link) {
5761
return (
5862
<Link
5963
{...linkProps}
6064
{...props}
61-
// Added the specific class to ensure that the link override is applied
62-
className={`${props.className} button__link ${props.disabled ? 'dc__disable-click' : ''}`}
63-
onClick={onClick as LinkProps['onClick']}
65+
className={linkOrAnchorClassName}
66+
onClick={onClick as ButtonProps<typeof component>['onClick']}
6467
ref={elementRef as MutableRefObject<HTMLAnchorElement>}
6568
/>
6669
)
6770
}
6871

72+
if (component === ButtonComponentType.anchor) {
73+
return (
74+
<a
75+
target="_blank"
76+
rel="noopener noreferrer"
77+
{...anchorProps}
78+
{...props}
79+
className={linkOrAnchorClassName}
80+
onClick={onClick as ButtonProps<typeof component>['onClick']}
81+
ref={elementRef as MutableRefObject<HTMLAnchorElement>}
82+
>
83+
{props.children}
84+
</a>
85+
)
86+
}
87+
6988
return (
7089
<button
7190
{...buttonProps}
7291
{...props}
7392
// eslint-disable-next-line react/button-has-type
7493
type={buttonProps?.type || 'button'}
75-
onClick={onClick as ButtonHTMLAttributes<HTMLButtonElement>['onClick']}
94+
onClick={onClick as ButtonProps<typeof component>['onClick']}
7695
ref={elementRef as MutableRefObject<HTMLButtonElement>}
7796
/>
7897
)

src/Shared/Components/Button/types.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { TooltipProps } from '@Common/Tooltip/types'
1818
import { ComponentSizeType } from '@Shared/constants'
19-
import { ButtonHTMLAttributes, ReactElement } from 'react'
19+
import { AnchorHTMLAttributes, ButtonHTMLAttributes, ReactElement } from 'react'
2020
import { LinkProps } from 'react-router-dom'
2121

2222
// Using the same for BEM class elements
@@ -39,6 +39,7 @@ export enum ButtonStyleType {
3939
export enum ButtonComponentType {
4040
button = 'button',
4141
link = 'link',
42+
anchor = 'anchor',
4243
}
4344

4445
export type ButtonProps<ComponentType extends ButtonComponentType = ButtonComponentType.button> =
@@ -50,25 +51,43 @@ export type ButtonProps<ComponentType extends ButtonComponentType = ButtonCompon
5051
*/
5152
linkProps: Omit<LinkProps, 'children' | 'styles' | 'className' | 'onClick'>
5253
buttonProps?: never
54+
anchorProps?: never
5355
onClick?: LinkProps['onClick']
5456
}
55-
: {
56-
/**
57-
* Component to be rendered from the available options
58-
*
59-
* @default ButtonComponentType.button
60-
*/
61-
component?: ComponentType | never
62-
/**
63-
* Props for the button component
64-
*/
65-
buttonProps?: Omit<
66-
ButtonHTMLAttributes<HTMLButtonElement>,
67-
'children' | 'styles' | 'className' | 'disabled' | 'onClick'
68-
>
69-
linkProps?: never
70-
onClick?: ButtonHTMLAttributes<HTMLButtonElement>['onClick']
71-
}) & {
57+
: ComponentType extends ButtonComponentType.anchor
58+
? {
59+
component: ButtonComponentType.anchor
60+
linkProps?: never
61+
buttonProps?: never
62+
/**
63+
* Props for the anchor component
64+
*
65+
* Note: The target is '_blank' & rel is 'noopener noreferrer' by default.
66+
*/
67+
anchorProps: Omit<
68+
AnchorHTMLAttributes<HTMLAnchorElement>,
69+
'children' | 'styles' | 'className' | 'onClick'
70+
>
71+
onClick?: AnchorHTMLAttributes<HTMLAnchorElement>['onClick']
72+
}
73+
: {
74+
/**
75+
* Component to be rendered from the available options
76+
*
77+
* @default ButtonComponentType.button
78+
*/
79+
component?: ComponentType | never
80+
/**
81+
* Props for the button component
82+
*/
83+
buttonProps?: Omit<
84+
ButtonHTMLAttributes<HTMLButtonElement>,
85+
'children' | 'styles' | 'className' | 'disabled' | 'onClick'
86+
>
87+
linkProps?: never
88+
anchorProps?: never
89+
onClick?: ButtonHTMLAttributes<HTMLButtonElement>['onClick']
90+
}) & {
7291
/**
7392
* Variant of the button
7493
*

0 commit comments

Comments
 (0)