Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 增加预览文件支持解析 tsx 以及 jsx #438

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions site/web/Demo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ import React from 'react';
import { Link, useLocation } from 'react-router-dom';
import Button from 'tdesign-mobile-react/button';

export const demoFiles = import.meta.globEager('../../src/**/_example/*.jsx');
export const jsxDemoFiles = import.meta.globEager('../../src/**/_example/*.jsx');
export const tsxDemoFiles = import.meta.globEager('../../src/**/_example/*.tsx');

const demoObject = {};
Object.keys(demoFiles).forEach((key) => {
const match = key.match(/([\w-]+)._example.([\w-]+).jsx/);
const [, componentName, demoName] = match;

demoObject[`${componentName}/${demoName}`] = demoFiles[key].default;
if (demoObject[componentName]) {
demoObject[componentName].push(demoName);
} else {
demoObject[componentName] = [demoName];
}
});
function handleDemoComponents(allDemoFiles, ext) {
const regexp = new RegExp(`([\\w-]+)._example.([\\w-]+).${ext}`);
Object.keys(allDemoFiles).forEach((key) => {
const match = key.match(regexp);
const [, componentName, demoName] = match;

demoObject[`${componentName}/${demoName}`] = allDemoFiles[key].default;
if (demoObject[componentName]) {
demoObject[componentName].push(demoName);
} else {
demoObject[componentName] = [demoName];
}
});
}

handleDemoComponents(jsxDemoFiles, 'jsx');
handleDemoComponents(tsxDemoFiles, 'tsx');

export default function Demo() {
const location = useLocation();
Expand All @@ -24,17 +32,19 @@ export default function Demo() {
const demoList = demoObject[componentName];
const demoFunc = demoObject[`${componentName}/${demoName}`];

return demoFunc ? demoFunc() : (
return demoFunc ? (
demoFunc()
) : (
<ul style={{ margin: '48px 200px' }}>
{
demoList.map(demoName => (
<li key={demoName}>
<Link to={`/mobile-react/demos/${componentName}/${demoName}`}>
<Button style={{ fontSize: 18 }} variant="text">{demoName}</Button>
</Link>
</li>
))
}
{demoList.map((demoName) => (
<li key={demoName}>
<Link to={`/mobile-react/demos/${componentName}/${demoName}`}>
<Button style={{ fontSize: 18 }} variant="text">
{demoName}
</Button>
</Link>
</li>
))}
</ul>
);
}
Loading