import React from 'react'; import { EditOutlined, GithubOutlined } from '@ant-design/icons'; import { Descriptions, theme, Tooltip, Typography, type GetProp } from 'antd'; import { createStyles, css } from 'antd-style'; import kebabCase from 'lodash/kebabCase'; import CopyToClipboard from 'react-copy-to-clipboard'; import useLocale from '../../../hooks/useLocale'; const locales = { cn: { import: '使用', copy: '复制', copied: '已复制', source: '源码', docs: '文档', edit: '编辑此页', version: '版本', }, en: { import: 'Import', copy: 'Copy', copied: 'Copied', source: 'Source', docs: 'Docs', edit: 'Edit this page', version: 'Version', }, }; const branchUrl = 'https://github.com/ant-design/ant-design/edit/master/'; function isVersionNumber(value?: string) { return value && /^\d+\.\d+\.\d+$/.test(value); } const useStyle = createStyles(({ token }) => ({ code: css` cursor: pointer; position: relative; display: inline-flex; align-items: center; column-gap: 4px; border-radius: 4px; padding-inline: ${token.paddingXS}px; transition: all ${token.motionDurationSlow} !important; font-family: ${token.codeFamily}; color: ${token.colorTextSecondary} !important; &:hover { background: ${token.controlItemBgHover}; } a&:hover { text-decoration: underline !important; } `, 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}; `, })); export interface ComponentMetaProps { component: string; source: string | true; filename?: string; version?: string; } const ComponentMeta: React.FC = (props) => { const { component, source, filename, version } = props; const { token } = theme.useToken(); const [locale, lang] = useLocale(locales); const isZhCN = lang === 'cn'; 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 = [ import , {`{ ${component} }`}, from , {`"antd"`} , ; , ]; return ( {importList} ), }, filledSource && { label: locale.source, children: ( {abbrSource} ), }, filename && { label: locale.docs, children: ( {locale.edit} ), }, isVersionNumber(version) && { label: locale.version, children: ( {isZhCN ? `自 ${version} 后支持` : `supported since ${version}`} ), }, ].filter(Boolean) as GetProp } /> ); }; export default ComponentMeta;