import React from 'react'; import { EditOutlined, GithubOutlined, HistoryOutlined } from '@ant-design/icons'; import type { GetProp } from 'antd'; import { Descriptions, theme, Tooltip, Typography, Space } 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'; import ComponentChangelog from '../../common/ComponentChangelog'; const locales = { cn: { import: '使用', copy: '复制', copied: '已复制', source: '源码', docs: '文档', edit: '编辑此页', changelog: '更新日志', version: '版本', }, en: { import: 'Import', copy: 'Copy', copied: 'Copied', source: 'Source', docs: 'Docs', edit: 'Edit this page', changelog: 'Changelog', 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: ${token.paddingXXS}px; border-radius: ${token.borderRadiusSM}px; padding-inline: ${token.paddingXXS}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}; margin-inline-end: 0.5em; `, 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]); const transformComponentName = (componentName: string) => { if (componentName === 'Notifiction' || componentName === 'Message') { return componentName.toLowerCase(); } return componentName; }; // ======================== Render ======================== const importList = [ import , {`{ ${transformComponentName( component, )} }`}, from , {`"antd"`} , ; , ]; return ( {importList} ), }, filledSource && { label: locale.source, children: ( {abbrSource} ), }, filename && { label: locale.docs, children: ( {locale.edit} {locale.changelog} ), }, isVersionNumber(version) && { label: locale.version, children: ( {isZhCN ? `自 ${version} 后支持` : `supported since ${version}`} ), }, ].filter(Boolean) as GetProp } /> ); }; export default ComponentMeta;