2022-11-30 11:05:41 +08:00
|
|
|
import type { FC } from 'react';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-07-20 19:27:33 +08:00
|
|
|
import { createStyles } from 'antd-style';
|
2023-03-12 14:13:03 +08:00
|
|
|
import type { TableProps } from 'antd';
|
|
|
|
import { Table } from 'antd';
|
|
|
|
import { getDesignToken } from 'antd-token-previewer';
|
|
|
|
import tokenMeta from 'antd/es/version/token-meta.json';
|
2022-11-25 14:23:11 +08:00
|
|
|
import useLocale from '../../../hooks/useLocale';
|
2022-12-07 22:36:08 +08:00
|
|
|
import ColorChunk from '../ColorChunk';
|
2022-11-25 14:23:11 +08:00
|
|
|
|
|
|
|
type TokenTableProps = {
|
|
|
|
type: 'seed' | 'map' | 'alias';
|
|
|
|
lang: 'zh' | 'en';
|
|
|
|
};
|
|
|
|
|
|
|
|
type TokenData = {
|
|
|
|
name: string;
|
|
|
|
desc: string;
|
|
|
|
type: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultToken = getDesignToken();
|
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
|
|
|
token: 'Token 名称',
|
|
|
|
description: '描述',
|
|
|
|
type: '类型',
|
|
|
|
value: '默认值',
|
|
|
|
},
|
|
|
|
en: {
|
|
|
|
token: 'Token Name',
|
|
|
|
description: 'Description',
|
|
|
|
type: 'Type',
|
|
|
|
value: 'Default Value',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(({ token, css }) => ({
|
|
|
|
codeSpan: css`
|
2022-11-25 14:23:11 +08:00
|
|
|
margin: 0 1px;
|
|
|
|
padding: 0.2em 0.4em;
|
|
|
|
font-size: 0.9em;
|
|
|
|
background: ${token.siteMarkdownCodeBg};
|
|
|
|
border: 1px solid ${token.colorSplit};
|
|
|
|
border-radius: 3px;
|
|
|
|
font-family: monospace;
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
}));
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-03-12 14:13:03 +08:00
|
|
|
export function useColumns(): Exclude<TableProps<TokenData>['columns'], undefined> {
|
|
|
|
const [locale] = useLocale(locales);
|
2023-07-20 19:27:33 +08:00
|
|
|
const { styles } = useStyle();
|
2023-03-12 14:13:03 +08:00
|
|
|
|
|
|
|
return [
|
2022-11-25 14:23:11 +08:00
|
|
|
{
|
|
|
|
title: locale.token,
|
|
|
|
key: 'name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: locale.description,
|
|
|
|
key: 'desc',
|
|
|
|
dataIndex: 'desc',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: locale.type,
|
|
|
|
key: 'type',
|
|
|
|
dataIndex: 'type',
|
2023-07-20 19:27:33 +08:00
|
|
|
render: (_, record) => <span className={styles.codeSpan}>{record.type}</span>,
|
2022-11-25 14:23:11 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: locale.value,
|
|
|
|
key: 'value',
|
2022-12-07 22:36:08 +08:00
|
|
|
render: (_, record) => {
|
|
|
|
const isColor =
|
|
|
|
typeof record.value === 'string' &&
|
|
|
|
(record.value.startsWith('#') || record.value.startsWith('rgb'));
|
|
|
|
if (isColor) {
|
|
|
|
return <ColorChunk color={record.value}>{record.value}</ColorChunk>;
|
|
|
|
}
|
|
|
|
return typeof record.value !== 'string' ? JSON.stringify(record.value) : record.value;
|
|
|
|
},
|
2022-11-25 14:23:11 +08:00
|
|
|
},
|
|
|
|
];
|
2023-03-12 14:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const TokenTable: FC<TokenTableProps> = ({ type }) => {
|
|
|
|
const [, lang] = useLocale(locales);
|
|
|
|
const columns = useColumns();
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-03-12 14:13:03 +08:00
|
|
|
const data = React.useMemo<TokenData[]>(
|
2022-11-30 11:05:41 +08:00
|
|
|
() =>
|
2023-07-05 17:49:59 +08:00
|
|
|
Object.entries(tokenMeta.global)
|
2022-12-01 20:52:35 +08:00
|
|
|
.filter(([, meta]) => meta.source === type)
|
|
|
|
.map(([token, meta]) => ({
|
|
|
|
name: token,
|
|
|
|
desc: lang === 'cn' ? meta.desc : meta.descEn,
|
|
|
|
type: meta.type,
|
2023-04-12 12:08:38 +08:00
|
|
|
value: defaultToken[token],
|
2022-12-01 20:52:35 +08:00
|
|
|
})),
|
2022-11-30 11:05:41 +08:00
|
|
|
[type, lang],
|
|
|
|
);
|
2022-11-25 14:23:11 +08:00
|
|
|
|
|
|
|
return <Table dataSource={data} columns={columns} pagination={false} bordered />;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TokenTable;
|