Skip to content

Commit 027953c

Browse files
committed
Add correct isDisabled prop
1 parent eb5d930 commit 027953c

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

example/pages/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const Home: NextPage = () => {
4444
label="This is just a button link"
4545
other={{ onClick: () => alert('click') }}
4646
/>
47+
<Row
48+
label="This is disabled"
49+
href="/other"
50+
other={{ isDisabled: true }}
51+
/>
4752

4853
<Heading size="md">{`IsExternal prop => [o -> /other, g -> https://google.com]`}</Heading>
4954
<Row label="/o1" href="/other" other={{ isExternal: true }} />

src/LinkingComponent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ export interface LinkingComponentProps extends Omit<LinkProps, 'href'> {
1515
href?: Url;
1616
justLink?: boolean;
1717
isExternal?: boolean;
18+
isDisabled?: boolean;
1819
}
1920

2021
export const LinkingComponent: React.FC<LinkingComponentProps> = (props) => {
21-
const { as, href, children, justLink, isExternal } = props;
22+
const { as, href, children, justLink, isExternal, isDisabled } = props;
23+
24+
if (isDisabled) {
25+
return <>{children}</>;
26+
}
2227

2328
if (href === undefined || (!as && external(href)) || isExternal) {
2429
const props = {

src/index.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const Link: React.FC<LinkProps> = (props) => {
4040
children,
4141
linkProps,
4242
justLink,
43+
isDisabled,
4344
noUnderline,
4445
...rest
4546
} = props;
@@ -51,22 +52,26 @@ export const Link: React.FC<LinkProps> = (props) => {
5152
as={nextAs}
5253
justLink={justLink}
5354
isExternal={(rest as any).isExternal}
55+
isDisabled={isDisabled}
5456
{...linkProps}
5557
>
5658
{justLink ? (
5759
children
5860
) : (
5961
<ChakraLink
60-
as={asHelper(href)}
61-
href={href}
62-
isExternal={(rest as any).isExternal || (!nextAs && external(href))}
6362
{...(noUnderline && {
63+
_hover: { textDecoration: 'none !important' }
64+
})}
65+
isExternal={(rest as any).isExternal || (!nextAs && external(href))}
66+
{...(isDisabled && {
67+
cursor: 'not-allowed',
68+
opacity: '0.4',
6469
_hover: {
65-
textDecoration: 'none !important'
70+
textDecoration: 'none'
6671
}
6772
})}
68-
{...rest}
6973
aria-current={isActive ? 'page' : undefined}
74+
{...rest}
7075
>
7176
{children}
7277
</ChakraLink>
@@ -76,19 +81,29 @@ export const Link: React.FC<LinkProps> = (props) => {
7681
};
7782

7883
export const LinkButton: React.FC<LinkButtonProps> = (props) => {
79-
const { nextAs, href, children, linkProps, isExternal, ...rest } = props;
84+
const {
85+
nextAs,
86+
href,
87+
children,
88+
isDisabled,
89+
linkProps,
90+
isExternal,
91+
...rest
92+
} = props;
8093
const isActive = useIsActive(href);
8194

8295
return (
8396
<LinkingComponent
8497
isExternal={isExternal}
8598
href={href}
8699
as={nextAs}
100+
isDisabled={isDisabled}
87101
{...linkProps}
88102
>
89103
<Button
90104
as={asHelper(href)}
91105
aria-current={isActive ? 'page' : undefined}
106+
isDisabled={isDisabled}
92107
{...rest}
93108
>
94109
{children}
@@ -98,20 +113,30 @@ export const LinkButton: React.FC<LinkButtonProps> = (props) => {
98113
};
99114

100115
export const LinkIconButton: React.FC<LinkButtonIconProps> = (props) => {
101-
const { nextAs, href, children, linkProps, isExternal, ...rest } = props;
116+
const {
117+
nextAs,
118+
href,
119+
children,
120+
isDisabled,
121+
linkProps,
122+
isExternal,
123+
...rest
124+
} = props;
102125
const isActive = useIsActive(href);
103126

104127
return (
105128
<LinkingComponent
106129
isExternal={isExternal}
107130
href={href}
108131
as={nextAs}
132+
isDisabled={isDisabled}
109133
{...linkProps}
110134
>
111135
<IconButton
112136
as={asHelper(href)}
113137
// ADD DISABLED
114138
aria-current={isActive ? 'page' : undefined}
139+
isDisabled={isDisabled}
115140
{...rest}
116141
>
117142
{children}

src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ export type BaseProps = {
1414

1515
type JustLink = { justLink: true };
1616
type ChakraSpecific = {
17-
isDisabled?: boolean;
1817
justLink?: false;
1918
noUnderline?: boolean;
2019
};
2120

2221
type Props<T> = BaseProps &
23-
((ChakraSpecific & T) | JustLink) & { noUnderline?: boolean };
22+
((ChakraSpecific & T) | JustLink) & {
23+
noUnderline?: boolean;
24+
isDisabled?: boolean;
25+
};
2426

2527
export type LinkProps = Props<ChakraLinkProps>;
2628
export type LinkButtonProps = ChakraButtonProps & ChakraSpecific & BaseProps;

0 commit comments

Comments
 (0)