-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(link): support
component
prop (#1699)
* feat(link): support `component` prop * docs(link): add story Co-authored-by: maxin <maxin@growingio.com>
- Loading branch information
Showing
5 changed files
with
93 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,60 @@ | ||
import { LoadingOutlined } from '@gio-design/icons'; | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import { OverridableComponent } from '@gio-design/utils'; | ||
import usePrefixCls from '../utils/hooks/use-prefix-cls'; | ||
import { LinkProps } from './interface'; | ||
import { LinkTypeMap } from './interface'; | ||
|
||
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>( | ||
({ className, children, prefix, loading = false, disabled: disabledProp = false, href, ...restProps }, ref) => { | ||
const prefixCls = usePrefixCls('link'); | ||
const classes = classNames([prefixCls, className], { | ||
[`${prefixCls}_disabled`]: disabledProp, | ||
[`${prefixCls}_loading`]: loading, | ||
}); | ||
export const Link = React.forwardRef((props, ref) => { | ||
const { | ||
className, | ||
children, | ||
prefix, | ||
loading, | ||
disabled: disabledProp, | ||
href, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: 在使用 Link 组件的时候能识别出 component prop,但是在这里却无法识别 | ||
component = 'a', | ||
...restProps | ||
} = props; | ||
const prefixCls = usePrefixCls('link'); | ||
const classes = classNames([prefixCls, className], { | ||
[`${prefixCls}_disabled`]: disabledProp, | ||
[`${prefixCls}_loading`]: loading, | ||
}); | ||
|
||
const prefixIcon = loading ? ( | ||
<span className={`${prefixCls}-prefix-icon`}> | ||
<LoadingOutlined rotating /> | ||
</span> | ||
) : ( | ||
prefix && <span className={`${prefixCls}-prefix-icon`}>{prefix}</span> | ||
); | ||
const prefixIcon = loading ? ( | ||
<span className={`${prefixCls}-prefix-icon`}> | ||
<LoadingOutlined rotating /> | ||
</span> | ||
) : ( | ||
prefix && <span className={`${prefixCls}-prefix-icon`}>{prefix}</span> | ||
); | ||
|
||
const disabled = disabledProp || loading; | ||
const disabled = disabledProp || loading; | ||
|
||
return ( | ||
<a | ||
className={classes} | ||
ref={ref} | ||
aria-disabled={disabled} | ||
tabIndex={disabled ? -1 : 0} | ||
data-testid="link" | ||
href={disabled ? undefined : href} | ||
{...restProps} | ||
> | ||
{prefixIcon} | ||
{children} | ||
</a> | ||
); | ||
} | ||
); | ||
const Component: React.ElementType = component; | ||
return ( | ||
<Component | ||
className={classes} | ||
disabled={disabledProp} | ||
aria-disabled={disabled} | ||
tabIndex={disabled ? -1 : 0} | ||
ref={ref} | ||
href={disabled ? undefined : href} | ||
data-testid="link" | ||
loading={component === 'a' ? undefined : loading} | ||
{...restProps} | ||
> | ||
{prefixIcon} | ||
{children} | ||
</Component> | ||
); | ||
}) as OverridableComponent<LinkTypeMap>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: 存在 displayName 属性 | ||
Link.displayName = 'Link'; | ||
|
||
Link.defaultProps = { | ||
loading: false, | ||
disabled: false, | ||
}; | ||
|
||
export default Link; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Link from './Link'; | ||
|
||
export type { LinkProps } from './interface'; | ||
export { LinkProps } from './interface'; | ||
|
||
export default Link; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
import { OverrideProps } from '@gio-design/utils/dist/interfaces'; | ||
|
||
export interface LinkProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'prefix' | 'href'> { | ||
/** | ||
* 文本前的图标 | ||
*/ | ||
prefix?: React.ReactNode; | ||
export interface LinkTypeMap<T extends React.ElementType = 'a'> { | ||
props: Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'prefix' | 'type'> & { | ||
/** | ||
* 文本前的图标 | ||
*/ | ||
prefix?: React.ReactNode; | ||
|
||
/** | ||
* 载入状态 | ||
* @default false | ||
*/ | ||
loading?: boolean; | ||
/** | ||
* 载入状态 | ||
* @default false | ||
*/ | ||
loading?: boolean; | ||
|
||
/** | ||
* 禁用状态 | ||
*/ | ||
disabled?: boolean | ||
|
||
/** | ||
* 包含超链接指向的 URL 或 URL 片段。`a` 标签的原生属性 | ||
*/ | ||
href?: string | ||
/** | ||
* 禁用状态 | ||
* @default false | ||
*/ | ||
disabled?: boolean; | ||
}; | ||
defaultComponent: T; | ||
} | ||
|
||
// prettier-ignore | ||
export type LinkProps< | ||
D extends React.ElementType = LinkTypeMap['defaultComponent'] | ||
> = OverrideProps<LinkTypeMap<D>, D>; |
0bb93f0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: