mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-07 17:44:35 +08:00
feat: inherit rc-pagination props, more customizable (#32132)
* feat: inherit rc-pagination props, more customizable close #32128, #32129 * chore: CustomSelect.Option is required by rc-pagination * fix: LocaleReceivier types issue * fix: lint issue, pageSize must be presented
This commit is contained in:
parent
af422c9e46
commit
b5fffcab87
@ -3,14 +3,21 @@ import defaultLocaleData from './default';
|
|||||||
import LocaleContext from './context';
|
import LocaleContext from './context';
|
||||||
import { Locale } from '.';
|
import { Locale } from '.';
|
||||||
|
|
||||||
export interface LocaleReceiverProps<C extends keyof Locale = keyof Locale> {
|
export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
|
||||||
|
|
||||||
|
export interface LocaleReceiverProps<C extends LocaleComponentName = LocaleComponentName> {
|
||||||
componentName: C;
|
componentName: C;
|
||||||
defaultLocale?: Locale[C] | (() => Locale[C]);
|
defaultLocale?: Locale[C] | (() => Locale[C]);
|
||||||
children: (locale: Exclude<Locale[C], undefined>, localeCode?: string, fullLocale?: object) => React.ReactNode;
|
children: (
|
||||||
|
locale: Exclude<Locale[C], undefined>,
|
||||||
|
localeCode?: string,
|
||||||
|
fullLocale?: object,
|
||||||
|
) => React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default class LocaleReceiver<
|
||||||
export default class LocaleReceiver<C extends keyof Locale = keyof Locale> extends React.Component<LocaleReceiverProps<C>> {
|
C extends LocaleComponentName = LocaleComponentName,
|
||||||
|
> extends React.Component<LocaleReceiverProps<C>> {
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
componentName: 'global',
|
componentName: 'global',
|
||||||
};
|
};
|
||||||
@ -19,8 +26,7 @@ export default class LocaleReceiver<C extends keyof Locale = keyof Locale> exten
|
|||||||
|
|
||||||
getLocale(): Exclude<Locale[C], undefined> {
|
getLocale(): Exclude<Locale[C], undefined> {
|
||||||
const { componentName, defaultLocale } = this.props;
|
const { componentName, defaultLocale } = this.props;
|
||||||
const locale =
|
const locale = defaultLocale || defaultLocaleData[componentName ?? 'global'];
|
||||||
defaultLocale || defaultLocaleData[componentName ?? 'global'];
|
|
||||||
const antLocale = this.context;
|
const antLocale = this.context;
|
||||||
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
||||||
return {
|
return {
|
||||||
@ -44,8 +50,7 @@ export default class LocaleReceiver<C extends keyof Locale = keyof Locale> exten
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type LocaleComponent = keyof Locale;
|
export function useLocaleReceiver<T extends LocaleComponentName>(
|
||||||
export function useLocaleReceiver<T extends LocaleComponent>(
|
|
||||||
componentName: T,
|
componentName: T,
|
||||||
defaultLocale?: Locale[T] | Function,
|
defaultLocale?: Locale[T] | Function,
|
||||||
): [Locale[T]] {
|
): [Locale[T]] {
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import RcPagination, { PaginationLocale } from 'rc-pagination';
|
import RcPagination, {
|
||||||
|
PaginationLocale,
|
||||||
|
PaginationProps as RcPaginationProps,
|
||||||
|
} from 'rc-pagination';
|
||||||
import enUS from 'rc-pagination/lib/locale/en_US';
|
import enUS from 'rc-pagination/lib/locale/en_US';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
||||||
@ -13,36 +16,16 @@ import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
||||||
|
|
||||||
export interface PaginationProps {
|
export interface PaginationProps extends RcPaginationProps {
|
||||||
total?: number;
|
|
||||||
defaultCurrent?: number;
|
|
||||||
disabled?: boolean;
|
|
||||||
current?: number;
|
|
||||||
defaultPageSize?: number;
|
|
||||||
pageSize?: number;
|
|
||||||
onChange?: (page: number, pageSize?: number) => void;
|
|
||||||
hideOnSinglePage?: boolean;
|
|
||||||
showSizeChanger?: boolean;
|
|
||||||
pageSizeOptions?: string[];
|
|
||||||
onShowSizeChange?: (current: number, size: number) => void;
|
|
||||||
showQuickJumper?: boolean | { goButton?: React.ReactNode };
|
showQuickJumper?: boolean | { goButton?: React.ReactNode };
|
||||||
showTitle?: boolean;
|
|
||||||
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
|
|
||||||
size?: 'default' | 'small';
|
size?: 'default' | 'small';
|
||||||
responsive?: boolean;
|
responsive?: boolean;
|
||||||
simple?: boolean;
|
|
||||||
style?: React.CSSProperties;
|
|
||||||
locale?: Partial<PaginationLocale>;
|
|
||||||
className?: string;
|
|
||||||
prefixCls?: string;
|
|
||||||
selectPrefixCls?: string;
|
|
||||||
itemRender?: (
|
itemRender?: (
|
||||||
page: number,
|
page: number,
|
||||||
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
|
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
|
||||||
originalElement: React.ReactElement<HTMLElement>,
|
originalElement: React.ReactElement<HTMLElement>,
|
||||||
) => React.ReactNode;
|
) => React.ReactNode;
|
||||||
role?: string;
|
role?: string;
|
||||||
showLessItems?: boolean;
|
|
||||||
totalBoundaryShowSizeChanger?: number;
|
totalBoundaryShowSizeChanger?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,6 +43,7 @@ const Pagination: React.FC<PaginationProps> = ({
|
|||||||
className,
|
className,
|
||||||
size,
|
size,
|
||||||
locale: customLocale,
|
locale: customLocale,
|
||||||
|
selectComponentClass,
|
||||||
...restProps
|
...restProps
|
||||||
}) => {
|
}) => {
|
||||||
const { xs } = useBreakpoint();
|
const { xs } = useBreakpoint();
|
||||||
@ -124,12 +108,12 @@ const Pagination: React.FC<PaginationProps> = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<RcPagination
|
<RcPagination
|
||||||
|
{...getIconsProps()}
|
||||||
{...restProps}
|
{...restProps}
|
||||||
prefixCls={prefixCls}
|
prefixCls={prefixCls}
|
||||||
selectPrefixCls={selectPrefixCls}
|
selectPrefixCls={selectPrefixCls}
|
||||||
{...getIconsProps()}
|
|
||||||
className={extendedClassName}
|
className={extendedClassName}
|
||||||
selectComponentClass={isSmall ? MiniSelect : Select}
|
selectComponentClass={selectComponentClass || (isSmall ? MiniSelect : Select)}
|
||||||
locale={locale}
|
locale={locale}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { render, mount } from 'enzyme';
|
import { render, mount } from 'enzyme';
|
||||||
import Pagination from '..';
|
import Pagination from '..';
|
||||||
|
import Select from '../../select';
|
||||||
import ConfigProvider from '../../config-provider';
|
import ConfigProvider from '../../config-provider';
|
||||||
import mountTest from '../../../tests/shared/mountTest';
|
import mountTest from '../../../tests/shared/mountTest';
|
||||||
import rtlTest from '../../../tests/shared/rtlTest';
|
import rtlTest from '../../../tests/shared/rtlTest';
|
||||||
@ -55,4 +56,17 @@ describe('Pagination', () => {
|
|||||||
wrapper.find('.ant-select-item-option').at(1).simulate('click');
|
wrapper.find('.ant-select-item-option').at(1).simulate('click');
|
||||||
expect(onChange).toHaveBeenCalledWith(1, 20);
|
expect(onChange).toHaveBeenCalledWith(1, 20);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support custom selectComponentClass', () => {
|
||||||
|
const CustomSelect = ({ className, ...props }) => (
|
||||||
|
<Select className={`${className} custom-select`} {...props} />
|
||||||
|
);
|
||||||
|
|
||||||
|
CustomSelect.Option = Select.Option;
|
||||||
|
|
||||||
|
const wrapper = mount(
|
||||||
|
<Pagination defaultCurrent={1} total={500} selectComponentClass={CustomSelect} />,
|
||||||
|
);
|
||||||
|
expect(wrapper.find('.custom-select').length).toBeTruthy();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -218,7 +218,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
|
|||||||
|
|
||||||
// Trigger pagination events
|
// Trigger pagination events
|
||||||
if (pagination && pagination.onChange) {
|
if (pagination && pagination.onChange) {
|
||||||
pagination.onChange(1, changeInfo.pagination!.pageSize);
|
pagination.onChange(1, changeInfo.pagination!.pageSize!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user