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-09-29 15:13:32 +08:00
|
|
|
const CodePreview = ({ 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
|
|
|
|
|
|
|
let content;
|
|
|
|
|
|
|
|
if (langList.length === 1) {
|
|
|
|
content = toReactComponent([
|
|
|
|
'pre',
|
|
|
|
{
|
|
|
|
lang: langList[0],
|
|
|
|
highlighted: codes[langList[0]],
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
} 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}>
|
|
|
|
{toReactComponent([
|
|
|
|
'pre',
|
|
|
|
{
|
|
|
|
lang,
|
|
|
|
highlighted: codes[lang],
|
|
|
|
},
|
|
|
|
])}
|
|
|
|
</TabPane>
|
|
|
|
))}
|
|
|
|
</Tabs>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <div className="highlight">{content}</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CodePreview;
|