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 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
|
|
|
if (langList.length === 1) {
|
2023-08-07 13:47:28 +08:00
|
|
|
return toReactComponent([
|
|
|
|
'pre',
|
|
|
|
{ lang: langList[0], highlighted: codes[langList[0]], className: 'highlight' },
|
|
|
|
]);
|
2019-06-23 12:49:28 +08:00
|
|
|
}
|
2023-08-07 13:47:28 +08:00
|
|
|
return (
|
|
|
|
<Tabs
|
|
|
|
centered
|
|
|
|
className="highlight"
|
|
|
|
onChange={onCodeTypeChange}
|
|
|
|
items={langList.map((lang) => ({
|
|
|
|
label: LANGS[lang],
|
|
|
|
key: lang,
|
|
|
|
children: toReactComponent(['pre', { lang, highlighted: codes[lang] }]),
|
|
|
|
}))}
|
|
|
|
/>
|
|
|
|
);
|
2019-06-23 12:49:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CodePreview;
|