ant-design/.dumi/theme/common/CodePreview.tsx

40 lines
1019 B
TypeScript
Raw Normal View History

import React from 'react';
import { Tabs } from 'antd';
const { TabPane } = Tabs;
const LANGS = {
tsx: 'TypeScript',
jsx: 'JavaScript',
};
2022-12-07 16:49:45 +08:00
interface CodePreviewProps {
codes?: Record<PropertyKey, string>;
toReactComponent?: (node: any) => React.ReactNode;
onCodeTypeChange?: (activeKey: string) => void;
}
const CodePreview: React.FC<CodePreviewProps> = ({ toReactComponent, codes, onCodeTypeChange }) => {
2020-10-21 10:35:06 +08:00
const langList = Object.keys(codes).sort().reverse();
2022-12-07 16:49:45 +08:00
let content: React.ReactNode;
if (langList.length === 1) {
2022-12-07 16:49:45 +08:00
content = toReactComponent(['pre', { lang: langList[0], highlighted: codes[langList[0]] }]);
} else {
content = (
2022-09-29 15:13:32 +08:00
<Tabs centered onChange={onCodeTypeChange}>
{langList.map((lang) => (
<TabPane tab={LANGS[lang]} key={lang}>
2022-12-07 16:49:45 +08:00
{toReactComponent(['pre', { lang, highlighted: codes[lang] }])}
</TabPane>
))}
</Tabs>
);
}
return <div className="highlight">{content}</div>;
};
export default CodePreview;