mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-04 00:49:39 +08:00
c5cae926ff
* docs: improve code previewer style * docs: simplify dom structure
38 lines
942 B
TypeScript
38 lines
942 B
TypeScript
import React from 'react';
|
|
import { Tabs } from 'antd';
|
|
|
|
const LANGS = {
|
|
tsx: 'TypeScript',
|
|
jsx: 'JavaScript',
|
|
};
|
|
|
|
interface CodePreviewProps {
|
|
codes?: Record<PropertyKey, string>;
|
|
toReactComponent?: (node: any) => React.ReactNode;
|
|
onCodeTypeChange?: (activeKey: string) => void;
|
|
}
|
|
|
|
const CodePreview: React.FC<CodePreviewProps> = ({ toReactComponent, codes, onCodeTypeChange }) => {
|
|
const langList = Object.keys(codes).sort().reverse();
|
|
if (langList.length === 1) {
|
|
return toReactComponent([
|
|
'pre',
|
|
{ lang: langList[0], highlighted: codes[langList[0]], className: 'highlight' },
|
|
]);
|
|
}
|
|
return (
|
|
<Tabs
|
|
centered
|
|
className="highlight"
|
|
onChange={onCodeTypeChange}
|
|
items={langList.map((lang) => ({
|
|
label: LANGS[lang],
|
|
key: lang,
|
|
children: toReactComponent(['pre', { lang, highlighted: codes[lang] }]),
|
|
}))}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default CodePreview;
|