2024-03-22 14:22:42 +08:00
|
|
|
import React from 'react';
|
2024-03-23 18:47:23 +08:00
|
|
|
import { EditOutlined, GithubOutlined } from '@ant-design/icons';
|
2024-03-22 14:22:42 +08:00
|
|
|
import { Descriptions, theme, Tooltip, Typography, type GetProp } from 'antd';
|
|
|
|
import { createStyles, css } from 'antd-style';
|
2024-03-22 15:43:15 +08:00
|
|
|
import kebabCase from 'lodash/kebabCase';
|
2024-03-22 14:22:42 +08:00
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
|
|
|
|
import useLocale from '../../../hooks/useLocale';
|
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
|
|
|
import: '使用',
|
|
|
|
copy: '复制',
|
|
|
|
copied: '已复制',
|
|
|
|
source: '源码',
|
2024-03-23 18:47:23 +08:00
|
|
|
docs: '文档',
|
|
|
|
edit: '编辑此页',
|
2024-03-22 14:22:42 +08:00
|
|
|
},
|
|
|
|
en: {
|
|
|
|
import: 'Import',
|
|
|
|
copy: 'Copy',
|
|
|
|
copied: 'Copied',
|
|
|
|
source: 'Source',
|
2024-03-23 18:47:23 +08:00
|
|
|
docs: 'Docs',
|
|
|
|
edit: 'Edit this page',
|
2024-03-22 14:22:42 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-03-23 18:47:23 +08:00
|
|
|
const branchUrl = 'https://github.com/ant-design/ant-design/edit/master/';
|
|
|
|
|
2024-03-22 14:22:42 +08:00
|
|
|
const useStyle = createStyles(({ token }) => ({
|
|
|
|
code: css`
|
|
|
|
cursor: pointer;
|
|
|
|
position: relative;
|
2024-03-22 15:43:15 +08:00
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
column-gap: 4px;
|
|
|
|
border-radius: 4px;
|
2024-03-22 14:22:42 +08:00
|
|
|
padding-inline: ${token.paddingXS}px;
|
|
|
|
transition: all ${token.motionDurationSlow} !important;
|
2024-03-23 18:47:23 +08:00
|
|
|
font-family: ${token.codeFamily};
|
|
|
|
color: ${token.colorTextSecondary} !important;
|
|
|
|
|
2024-03-22 14:22:42 +08:00
|
|
|
&:hover {
|
|
|
|
background: ${token.controlItemBgHover};
|
|
|
|
}
|
2024-03-23 18:47:23 +08:00
|
|
|
|
|
|
|
a&:hover {
|
|
|
|
text-decoration: underline !important;
|
|
|
|
}
|
2024-03-22 14:22:42 +08:00
|
|
|
`,
|
2024-03-22 15:43:15 +08:00
|
|
|
import: css`
|
|
|
|
color: ${token.magenta8};
|
|
|
|
`,
|
|
|
|
component: css`
|
|
|
|
color: ${token.colorText};
|
|
|
|
`,
|
|
|
|
from: css`
|
|
|
|
color: ${token.magenta8};
|
|
|
|
`,
|
|
|
|
antd: css`
|
|
|
|
color: ${token.green8};
|
|
|
|
`,
|
|
|
|
semicolon: css`
|
|
|
|
color: ${token.colorText};
|
|
|
|
`,
|
2024-03-22 14:22:42 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
export interface ComponentMetaProps {
|
|
|
|
component: string;
|
|
|
|
source: string | true;
|
2024-03-23 18:47:23 +08:00
|
|
|
filename?: string;
|
2024-03-22 14:22:42 +08:00
|
|
|
}
|
|
|
|
|
2024-03-22 15:43:15 +08:00
|
|
|
const ComponentMeta: React.FC<ComponentMetaProps> = (props) => {
|
2024-03-23 18:47:23 +08:00
|
|
|
const { component, source, filename } = props;
|
2024-03-22 14:22:42 +08:00
|
|
|
const { token } = theme.useToken();
|
|
|
|
const [locale] = useLocale(locales);
|
|
|
|
|
|
|
|
const { styles } = useStyle();
|
|
|
|
|
|
|
|
// ========================= Copy =========================
|
|
|
|
const [copied, setCopied] = React.useState(false);
|
|
|
|
|
|
|
|
const onCopy = () => {
|
|
|
|
setCopied(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onOpenChange = (open: boolean) => {
|
|
|
|
if (open) {
|
|
|
|
setCopied(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ======================== Source ========================
|
|
|
|
const [filledSource, abbrSource] = React.useMemo(() => {
|
|
|
|
if (String(source) === 'true') {
|
|
|
|
const kebabComponent = kebabCase(component);
|
|
|
|
return [
|
|
|
|
`https://github.com/ant-design/ant-design/blob/master/components/${kebabComponent}`,
|
|
|
|
`components/${kebabComponent}`,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof source !== 'string') {
|
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [source, source];
|
|
|
|
}, [component, source]);
|
|
|
|
|
|
|
|
// ======================== Render ========================
|
|
|
|
const importList = [
|
2024-03-22 15:43:15 +08:00
|
|
|
<span key="import" className={styles.import}>
|
2024-03-22 14:22:42 +08:00
|
|
|
import
|
|
|
|
</span>,
|
2024-03-22 15:43:15 +08:00
|
|
|
<span key="component" className={styles.component}>{`{ ${component} }`}</span>,
|
|
|
|
<span key="from" className={styles.from}>
|
2024-03-22 14:22:42 +08:00
|
|
|
from
|
|
|
|
</span>,
|
2024-03-22 15:43:15 +08:00
|
|
|
<span key="antd" className={styles.antd}>
|
|
|
|
{`"antd"`}
|
2024-03-22 14:22:42 +08:00
|
|
|
</span>,
|
2024-03-22 15:43:15 +08:00
|
|
|
<span key="semicolon" className={styles.semicolon}>
|
2024-03-22 14:22:42 +08:00
|
|
|
;
|
|
|
|
</span>,
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Descriptions
|
|
|
|
size="small"
|
|
|
|
colon={false}
|
|
|
|
column={1}
|
|
|
|
style={{ marginTop: token.margin }}
|
2024-03-23 18:47:23 +08:00
|
|
|
labelStyle={{ paddingInlineEnd: token.padding, width: 54 }}
|
2024-03-22 14:22:42 +08:00
|
|
|
items={
|
|
|
|
[
|
|
|
|
{
|
|
|
|
label: locale.import,
|
|
|
|
children: (
|
|
|
|
<Tooltip
|
|
|
|
placement="right"
|
|
|
|
title={copied ? locale.copied : locale.copy}
|
|
|
|
onOpenChange={onOpenChange}
|
|
|
|
>
|
|
|
|
<span>
|
2024-03-22 15:43:15 +08:00
|
|
|
<CopyToClipboard text={`import { ${component} } from "antd";`} onCopy={onCopy}>
|
2024-03-22 14:22:42 +08:00
|
|
|
<Typography.Text className={styles.code} onClick={onCopy}>
|
2024-03-22 15:43:15 +08:00
|
|
|
{importList}
|
2024-03-22 14:22:42 +08:00
|
|
|
</Typography.Text>
|
|
|
|
</CopyToClipboard>
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
filledSource && {
|
|
|
|
label: locale.source,
|
|
|
|
children: (
|
|
|
|
<Typography.Link className={styles.code} href={filledSource} target="_blank">
|
2024-03-23 18:47:23 +08:00
|
|
|
<GithubOutlined style={{ marginInlineEnd: 4 }} />
|
|
|
|
<span>{abbrSource}</span>
|
|
|
|
</Typography.Link>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
filename && {
|
|
|
|
label: locale.docs,
|
|
|
|
children: (
|
|
|
|
<Typography.Link
|
|
|
|
className={styles.code}
|
|
|
|
href={`${branchUrl}${filename}`}
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
<EditOutlined style={{ marginInlineEnd: 4 }} />
|
|
|
|
<span>{locale.edit}</span>
|
2024-03-22 14:22:42 +08:00
|
|
|
</Typography.Link>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
].filter((v) => v) as GetProp<typeof Descriptions, 'items'>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ComponentMeta;
|