Skip to content

Commit

Permalink
fix(layout): support configprovide darkAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Dec 8, 2022
1 parent a9a71d9 commit dabf05e
Show file tree
Hide file tree
Showing 10 changed files with 2,701 additions and 525 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"devDependencies": {
"@ant-design/antd-theme-variable": "^1.0.0",
"@ant-design/cssinjs": "^0.0.0-alpha.38",
"@ant-design/cssinjs": "^1.0.0",
"@ant-design/icons": "^4.6.2",
"@babel/core": "^7.16.0",
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const genSettingDrawerStyle: GenerateStyle<SettingDrawerToken> = (token) => {
height: '36px',
overflow: 'hidden',
borderRadius: '4px',
boxShadow: token.boxShadowCard,
boxShadow: token.boxShadow,
cursor: 'pointer',
fontSize: 56,
lineHeight: '56px',
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"> 1%"
],
"dependencies": {
"@ant-design/cssinjs": "^0.0.0-alpha.38",
"@ant-design/cssinjs": "^1.0.0",
"@babel/runtime": "^7.18.0",
"@ctrl/tinycolor": "^3.4.0",
"rc-util": "^5.0.1",
Expand Down
20 changes: 15 additions & 5 deletions packages/provider/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import zhTW from './locale/zh_TW';
import type { DeepPartial, ProTokenType } from './typing/layoutToken';
import { getLayoutDesignToken } from './typing/layoutToken';
import type { ProAliasToken } from './useStyle';
import { darkAlgorithm, defaultAlgorithm } from './useStyle';
import { useToken } from './useStyle';
import { proTheme } from './useStyle';
import { emptyTheme, defaultToken } from './useStyle/token';
import { merge } from './utils/merge';

Expand Down Expand Up @@ -322,7 +321,7 @@ const ConfigProVoidContainer: React.FC<{
}> = (props) => {
const { children, dark, valueTypeMap, autoClearCache = false, token: propsToken } = props;
const { locale, getPrefixCls, ...restConfig } = useContext(AntdConfigProvider.ConfigContext);
const tokenContext = useToken?.();
const tokenContext = proTheme.useToken?.();

const proProvide = useContext(ProConfigContext);

Expand Down Expand Up @@ -456,15 +455,26 @@ export const ProConfigProvider: React.FC<{
Object.keys(props).sort().join('-') === 'children-needDeps';

if (isNullProvide) return <>{props.children}</>;

const mergeAlgorithm = () => {
const isDark = dark ?? proProvide.dark;
if (isDark && !Array.isArray(theme?.algorithm)) {
return [proTheme.darkAlgorithm, theme?.algorithm].filter(Boolean);
}
if (isDark && Array.isArray(theme?.algorithm)) {
return [proTheme.darkAlgorithm, ...(theme?.algorithm || [])].filter(Boolean);
}
return theme?.algorithm;
};
// 自动注入 antd 的配置
const configProvider = {
...rest,
locale: locale || zh_CN,
theme: {
...theme,
algorithm: dark ?? proProvide.dark ? darkAlgorithm : defaultAlgorithm,
algorithm: mergeAlgorithm(),
},
};
} as typeof theme;

return (
<AntdConfigProvider {...configProvider}>
Expand Down
14 changes: 5 additions & 9 deletions packages/provider/src/useStyle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ export type GenerateStyle<
ReturnType = CSSInterpolation,
> = (token: ComponentToken) => ReturnType;

/**
* 如果 antd 里面没有,就用我 mock 的,这样 antd@4 和 antd@5 可以兼容
*/
const { useToken, darkAlgorithm, compactAlgorithm, defaultAlgorithm } = {
...batToken,
// @ts-ignore
...(antdTheme || {}),
} as any;
const genTheme = (): typeof antdTheme => {
if (typeof antdTheme === 'undefined') return batToken as any;
return antdTheme;
};

export { useToken, darkAlgorithm, compactAlgorithm, defaultAlgorithm };
export const proTheme = genTheme();

export type UseStyleResult = {
wrapSSR: (node: React.ReactElement) => React.ReactElement;
Expand Down
231 changes: 231 additions & 0 deletions packages/table/src/demos/theme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import { EllipsisOutlined, PlusOutlined } from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable, TableDropdown } from '@ant-design/pro-components';
import { Button, ConfigProvider, Dropdown, Space, Tag, theme } from 'antd';
import { useRef } from 'react';
import request from 'umi-request';

type GithubIssueItem = {
url: string;
id: number;
number: number;
title: string;
labels: {
name: string;
color: string;
}[];
state: string;
comments: number;
created_at: string;
updated_at: string;
closed_at?: string;
};

const columns: ProColumns<GithubIssueItem>[] = [
{
dataIndex: 'index',
valueType: 'indexBorder',
width: 48,
},
{
title: '标题',
dataIndex: 'title',
copyable: true,
ellipsis: true,
tip: '标题过长会自动收缩',
formItemProps: {
rules: [
{
required: true,
message: '此项为必填项',
},
],
},
},
{
disable: true,
title: '状态',
dataIndex: 'state',
filters: true,
onFilter: true,
ellipsis: true,
valueType: 'select',
valueEnum: {
all: { text: '超长'.repeat(50) },
open: {
text: '未解决',
status: 'Error',
},
closed: {
text: '已解决',
status: 'Success',
disabled: true,
},
processing: {
text: '解决中',
status: 'Processing',
},
},
},
{
disable: true,
title: '标签',
dataIndex: 'labels',
search: false,
renderFormItem: (_, { defaultRender }) => {
return defaultRender(_);
},
render: (_, record) => (
<Space>
{record.labels.map(({ name, color }) => (
<Tag color={color} key={name}>
{name}
</Tag>
))}
</Space>
),
},
{
title: '创建时间',
key: 'showTime',
dataIndex: 'created_at',
valueType: 'date',
sorter: true,
hideInSearch: true,
},
{
title: '创建时间',
dataIndex: 'created_at',
valueType: 'dateRange',
hideInTable: true,
search: {
transform: (value) => {
return {
startTime: value[0],
endTime: value[1],
};
},
},
},
{
title: '操作',
valueType: 'option',
key: 'option',
render: (text, record, _, action) => [
<a
key="editable"
onClick={() => {
action?.startEditable?.(record.id);
}}
>
编辑
</a>,
<a href={record.url} target="_blank" rel="noopener noreferrer" key="view">
查看
</a>,
<TableDropdown
key="actionGroup"
onSelect={() => action?.reload()}
menus={[
{ key: 'copy', name: '复制' },
{ key: 'delete', name: '删除' },
]}
/>,
],
},
];

export default () => {
const actionRef = useRef<ActionType>();
const themeConfig = {
token: {
colorPrimary: 'red',
borderRadius: 4,
// TODO 可以验证下是否透明也行
colorBgElevated: 'white',
},
algorithm: [theme.darkAlgorithm, theme.compactAlgorithm],
};
return (
<ConfigProvider theme={themeConfig}>
<ProTable<GithubIssueItem>
columns={columns}
actionRef={actionRef}
cardBordered
request={async (params = {}, sort, filter) => {
console.log(sort, filter);
return request<{
data: GithubIssueItem[];
}>('https://proapi.azurewebsites.net/github/issues', {
params,
});
}}
editable={{
type: 'multiple',
}}
columnsState={{
persistenceKey: 'pro-table-singe-demos',
persistenceType: 'localStorage',
onChange(value) {
console.log('value: ', value);
},
}}
rowKey="id"
search={{
labelWidth: 'auto',
}}
options={{
setting: {
listsHeight: 400,
},
}}
form={{
// 由于配置了 transform,提交的参与与定义的不同这里需要转化一下
syncToUrl: (values, type) => {
if (type === 'get') {
return {
...values,
created_at: [values.startTime, values.endTime],
};
}
return values;
},
}}
pagination={{
pageSize: 5,
onChange: (page) => console.log(page),
}}
dateFormatter="string"
headerTitle="高级表格"
toolBarRender={() => [
<Button key="button" icon={<PlusOutlined />} type="primary">
新建
</Button>,
<Dropdown
key="menu"
menu={{
items: [
{
label: '1st item',
key: '1',
},
{
label: '2nd item',
key: '1',
},
{
label: '3rd item',
key: '1',
},
],
}}
>
<Button>
<EllipsisOutlined />
</Button>
</Dropdown>,
]}
/>
</ConfigProvider>
);
};
5 changes: 5 additions & 0 deletions packages/table/src/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ ProTable 的诞生是为了解决项目中需要写很多 table 的样板代码
### 查询表格

<code src="./demos/single.tsx" background="#f5f5f5" height="610px" title="查询表格"/>

### 黑色主紧凑主题

<code src="./demos/theme.tsx" background="#141414" height="610px" title="查询表格"/>

<code src="./demos/single-test.tsx" debug background="#f5f5f5" height="500px" title="查询表格" />

### 查询(无按钮)表格
Expand Down
3 changes: 1 addition & 2 deletions tests/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import MockDate from 'mockdate';
import React from 'react';
import tableData from './table/mock.data.json';

import { defaultConfig } from 'antd/es/theme/index';
import { defaultConfig } from 'antd/es/theme/internal';

defaultConfig.hashed = false;

jest.mock('antd', () => {
const antd = jest.requireActual('antd');
antd.theme.defaultConfig.hashed = false;

return antd;
});

Expand Down
Loading

0 comments on commit dabf05e

Please sign in to comment.