Skip to content

Commit 69d501b

Browse files
author
Kenneth
committed
#feature: Custom ConfigProvider, allow passing default props
1 parent aa65736 commit 69d501b

File tree

12 files changed

+137
-13
lines changed

12 files changed

+137
-13
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { useContext } from 'react';
2+
import { ConfigProvider } from '../../organisms';
13
import { PaginationStyled } from './styles';
24
import { RdPaginationComponent } from './types';
35

46
export const Pagination: RdPaginationComponent = props => {
5-
return <PaginationStyled {...props} />;
7+
const { pagination: defaultPaginationProps } = useContext(ConfigProvider.ConfigContext);
8+
const { pageSizeOptions } = defaultPaginationProps || {};
9+
10+
return <PaginationStyled pageSizeOptions={pageSizeOptions} {...props} />;
611
};

src/molecules/Table/Table.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@ import { AnyObject } from 'antd/es/_util/type';
55
import { RowSortable } from './sortable';
66
import { TableStyledFunc } from './styles';
77
import { RdTableProps } from './types';
8+
import { useContext, useMemo } from 'react';
9+
import { ConfigProvider } from '../../organisms';
810

911
export const Table = <RecordType extends AnyObject = AnyObject>(
1012
props: RdTableProps<RecordType>
1113
) => {
12-
const { allowSort = false, onChangeSort, ...antdProps } = props;
14+
const { allowSort = false, pagination, onChangeSort, ...antdProps } = props;
15+
const { table: defaultTableProps } = useContext(ConfigProvider.ConfigContext);
16+
const { pagination: defaultPagination } = defaultTableProps || {};
17+
18+
const paginationWithDefault = useMemo(() => {
19+
return {
20+
...defaultPagination,
21+
...pagination,
22+
};
23+
}, [pagination, defaultPagination]);
24+
1325
const TableStyled = TableStyledFunc<RecordType>();
1426

1527
if (allowSort && props.rowKey) {
@@ -32,11 +44,15 @@ export const Table = <RecordType extends AnyObject = AnyObject>(
3244
items={dataSource.map(i => i[props.rowKey as string])}
3345
strategy={verticalListSortingStrategy}
3446
>
35-
<TableStyled components={{ body: { row: RowSortable } }} {...antdProps} />
47+
<TableStyled
48+
components={{ body: { row: RowSortable } }}
49+
pagination={paginationWithDefault}
50+
{...antdProps}
51+
/>
3652
</SortableContext>
3753
</DndContext>
3854
);
3955
}
4056

41-
return <TableStyled {...antdProps} />;
57+
return <TableStyled pagination={paginationWithDefault} {...antdProps} />;
4258
};
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { ConfigProvider as ConfigProviderAntd } from 'antd';
2-
import { RdConfigProviderCompoundedComponent, RdConfigProviderProps } from './types';
2+
import { ConfigContext } from './context';
3+
import { RdConfigConsumerProps } from './context/types';
4+
import { RdConfigProviderCompoundedComponent, RdConfigProviderInternalComponent } from './types';
35

4-
export const ConfigProviderInternal = ({ ...antdProps }: RdConfigProviderProps) => {
5-
return <ConfigProviderAntd {...antdProps} />;
6+
export const ConfigProviderInternal: RdConfigProviderInternalComponent = props => {
7+
return (
8+
<ConfigContext.Provider
9+
value={{
10+
...(props as RdConfigConsumerProps),
11+
}}
12+
>
13+
<ConfigProviderAntd {...props} />
14+
</ConfigContext.Provider>
15+
);
616
};
717

818
export const ConfigProvider = ConfigProviderInternal as RdConfigProviderCompoundedComponent;
919

10-
ConfigProvider.ConfigContext = ConfigProviderAntd.ConfigContext;
20+
ConfigProvider.ConfigContext = ConfigContext;
1121
ConfigProvider.SizeContext = ConfigProviderAntd.SizeContext;
1222
ConfigProvider.config = ConfigProviderAntd.config;
1323
ConfigProvider.useConfig = ConfigProviderAntd.useConfig;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { PaginationConfig } from 'antd/es/config-provider/context';
2+
import { RdPaginationProps } from '../../../molecules';
3+
4+
//#region Define Ant Design types
5+
type PaginationConfigAntd = PaginationConfig;
6+
//#endregion
7+
8+
//#region Define extended types
9+
interface PaginationConfigExtend extends Pick<RdPaginationProps, 'pageSizeOptions'> {}
10+
//#endregion
11+
12+
//#region Export types
13+
export type RdPaginationConfig = PaginationConfigAntd & PaginationConfigExtend;
14+
//#endregion
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { TableConfig } from 'antd/es/config-provider/context';
2+
import { RdTableProps } from '../../../molecules';
3+
4+
//#region Define Ant Design types
5+
type TableConfigAntd = TableConfig;
6+
//#endregion
7+
8+
//#region Define extended types
9+
interface TableConfigExtend extends Pick<RdTableProps, 'pagination'> {}
10+
//#endregion
11+
12+
//#region Export types
13+
export type RdTableConfig = TableConfigAntd & TableConfigExtend;
14+
//#endregion
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const defaultPrefixCls = 'ant';
2+
export const defaultIconPrefixCls = 'anticon';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defaultPrefixCls } from './constants';
2+
3+
export const defaultGetPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
4+
if (customizePrefixCls) {
5+
return customizePrefixCls;
6+
}
7+
return suffixCls ? `${defaultPrefixCls}-${suffixCls}` : defaultPrefixCls;
8+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import { defaultIconPrefixCls, defaultPrefixCls } from './constants';
3+
import { RdConfigConsumerProps } from './types';
4+
import { defaultGetPrefixCls } from './helper';
5+
6+
export const ConfigContext = React.createContext<RdConfigConsumerProps>({
7+
// We provide a default function for Context without provider
8+
getPrefixCls: defaultGetPrefixCls,
9+
iconPrefixCls: defaultIconPrefixCls,
10+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ConfigConsumerProps } from 'antd/es/config-provider';
2+
import { RdPaginationConfig } from '../configs/pagination';
3+
import { RdTableConfig } from '../configs/table';
4+
5+
//#region Define Ant Design types
6+
type ConfigConsumerPropsAntd = ConfigConsumerProps;
7+
//#endregion
8+
9+
//#region Define extended types
10+
interface ConfigConsumerPropsExtend {
11+
pagination?: RdPaginationConfig;
12+
table?: RdTableConfig;
13+
}
14+
//#endregion
15+
16+
//#region Export types
17+
export type RdConfigConsumerProps = Omit<ConfigConsumerPropsAntd, 'pagination' | 'table'> &
18+
ConfigConsumerPropsExtend;
19+
//#endregion
20+
21+
export type RdConfigContext = React.Context<RdConfigConsumerProps>;
22+
export type RdConfigConsumer = React.Consumer<RdConfigConsumerProps>;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './ConfigProvider';
2-
export * from './types';
2+
export * from './types';
3+
export * from './types.props'

0 commit comments

Comments
 (0)