2019-06-23 12:49:28 +08:00
|
|
|
import React from 'react';
|
2019-07-03 20:14:39 +08:00
|
|
|
import { Tabs } from 'antd';
|
2019-06-23 12:49:28 +08:00
|
|
|
|
|
|
|
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();
|
2019-06-23 12:49:28 +08:00
|
|
|
|
2022-12-07 16:49:45 +08:00
|
|
|
let content: React.ReactNode;
|
2019-06-23 12:49:28 +08:00
|
|
|
|
|
|
|
if (langList.length === 1) {
|
2022-12-07 16:49:45 +08:00
|
|
|
content = toReactComponent(['pre', { lang: langList[0], highlighted: codes[langList[0]] }]);
|
2019-06-23 12:49:28 +08:00
|
|
|
} else {
|
|
|
|
content = (
|
2022-09-29 15:13:32 +08:00
|
|
|
<Tabs centered onChange={onCodeTypeChange}>
|
2022-11-19 13:47:33 +08:00
|
|
|
{langList.map((lang) => (
|
2019-06-23 12:49:28 +08:00
|
|
|
<TabPane tab={LANGS[lang]} key={lang}>
|
2022-12-07 16:49:45 +08:00
|
|
|
{toReactComponent(['pre', { lang, highlighted: codes[lang] }])}
|
2019-06-23 12:49:28 +08:00
|
|
|
</TabPane>
|
|
|
|
))}
|
|
|
|
</Tabs>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <div className="highlight">{content}</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CodePreview;
|