mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 03:22:59 +08:00
chore: use React.useContext replace <Context.ConfigConsumer /> (#40091)
* chore: use React.useContext replace <Context.ConfigConsumer /> * add
This commit is contained in:
parent
b242dde04c
commit
6b4f94785c
@ -14,7 +14,7 @@ import RcCascader from 'rc-cascader';
|
|||||||
import omit from 'rc-util/lib/omit';
|
import omit from 'rc-util/lib/omit';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import DisabledContext from '../config-provider/DisabledContext';
|
import DisabledContext from '../config-provider/DisabledContext';
|
||||||
import type { SizeType } from '../config-provider/SizeContext';
|
import type { SizeType } from '../config-provider/SizeContext';
|
||||||
import SizeContext from '../config-provider/SizeContext';
|
import SizeContext from '../config-provider/SizeContext';
|
||||||
@ -187,7 +187,9 @@ const Cascader = React.forwardRef((props: CascaderProps<any>, ref: React.Ref<Cas
|
|||||||
}
|
}
|
||||||
|
|
||||||
// =================== No Found ====================
|
// =================== No Found ====================
|
||||||
const mergedNotFoundContent = notFoundContent || (renderEmpty || defaultRenderEmpty)('Cascader');
|
const mergedNotFoundContent = notFoundContent || renderEmpty?.('Cascader') || (
|
||||||
|
<DefaultRenderEmpty componentName="Cascader" />
|
||||||
|
);
|
||||||
|
|
||||||
// ==================== Prefix =====================
|
// ==================== Prefix =====================
|
||||||
const rootPrefixCls = getPrefixCls();
|
const rootPrefixCls = getPrefixCls();
|
||||||
|
@ -1,34 +1,33 @@
|
|||||||
import * as React from 'react';
|
import React, { useContext } from 'react';
|
||||||
|
import { ConfigContext } from '.';
|
||||||
import type { ConfigConsumerProps } from '.';
|
import type { ConfigConsumerProps } from '.';
|
||||||
import { ConfigConsumer } from '.';
|
|
||||||
import Empty from '../empty';
|
import Empty from '../empty';
|
||||||
|
|
||||||
const defaultRenderEmpty = (componentName?: string): React.ReactNode => (
|
interface EmptyProps {
|
||||||
<ConfigConsumer>
|
componentName?: string;
|
||||||
{({ getPrefixCls }: ConfigConsumerProps) => {
|
}
|
||||||
const prefix = getPrefixCls('empty');
|
|
||||||
|
|
||||||
|
const DefaultRenderEmpty: React.FC<EmptyProps> = (props) => {
|
||||||
|
const { componentName } = props;
|
||||||
|
const { getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
|
||||||
|
const prefix = getPrefixCls('empty');
|
||||||
switch (componentName) {
|
switch (componentName) {
|
||||||
case 'Table':
|
case 'Table':
|
||||||
case 'List':
|
case 'List':
|
||||||
return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />;
|
return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />;
|
||||||
|
|
||||||
case 'Select':
|
case 'Select':
|
||||||
case 'TreeSelect':
|
case 'TreeSelect':
|
||||||
case 'Cascader':
|
case 'Cascader':
|
||||||
case 'Transfer':
|
case 'Transfer':
|
||||||
case 'Mentions':
|
case 'Mentions':
|
||||||
return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} className={`${prefix}-small`} />;
|
return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} className={`${prefix}-small`} />;
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
default:
|
default:
|
||||||
// Should never hit if we take all the component into consider.
|
// Should never hit if we take all the component into consider.
|
||||||
return <Empty />;
|
return <Empty />;
|
||||||
}
|
}
|
||||||
}}
|
};
|
||||||
</ConfigConsumer>
|
|
||||||
);
|
|
||||||
|
|
||||||
export type RenderEmptyHandler = typeof defaultRenderEmpty;
|
export type RenderEmptyHandler = (componentName?: string) => React.ReactNode;
|
||||||
|
|
||||||
export default defaultRenderEmpty;
|
export default DefaultRenderEmpty;
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
// eslint-disable-next-line import/no-named-as-default
|
// eslint-disable-next-line import/no-named-as-default
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import type { RenderEmptyHandler } from '../config-provider';
|
|
||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import { Row } from '../grid';
|
import { Row } from '../grid';
|
||||||
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
||||||
import type { PaginationConfig } from '../pagination';
|
import type { PaginationConfig } from '../pagination';
|
||||||
@ -143,13 +142,8 @@ function List<T>({
|
|||||||
|
|
||||||
const isSomethingAfterLastItem = () => !!(loadMore || pagination || footer);
|
const isSomethingAfterLastItem = () => !!(loadMore || pagination || footer);
|
||||||
|
|
||||||
const renderEmptyFunc = (prefixCls: string, renderEmptyHandler: RenderEmptyHandler) => (
|
|
||||||
<div className={`${prefixCls}-empty-text`}>
|
|
||||||
{(locale && locale.emptyText) || renderEmptyHandler('List')}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
||||||
|
|
||||||
// Style
|
// Style
|
||||||
const [wrapSSR, hashId] = useStyle(prefixCls);
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
||||||
|
|
||||||
@ -253,7 +247,7 @@ function List<T>({
|
|||||||
}
|
}
|
||||||
}, [grid?.column, currentBreakpoint]);
|
}, [grid?.column, currentBreakpoint]);
|
||||||
|
|
||||||
let childrenContent = isLoading && <div style={{ minHeight: 53 }} />;
|
let childrenContent: React.ReactNode = isLoading && <div style={{ minHeight: 53 }} />;
|
||||||
if (splitDataSource.length > 0) {
|
if (splitDataSource.length > 0) {
|
||||||
const items = splitDataSource.map((item: T, index: number) => renderInnerItem(item, index));
|
const items = splitDataSource.map((item: T, index: number) => renderInnerItem(item, index));
|
||||||
childrenContent = grid ? (
|
childrenContent = grid ? (
|
||||||
@ -268,7 +262,13 @@ function List<T>({
|
|||||||
<ul className={`${prefixCls}-items`}>{items}</ul>
|
<ul className={`${prefixCls}-items`}>{items}</ul>
|
||||||
);
|
);
|
||||||
} else if (!children && !isLoading) {
|
} else if (!children && !isLoading) {
|
||||||
childrenContent = renderEmptyFunc(prefixCls, renderEmpty || defaultRenderEmpty);
|
childrenContent = (
|
||||||
|
<div className={`${prefixCls}-empty-text`}>
|
||||||
|
{(locale && locale.emptyText) || renderEmpty?.('List') || (
|
||||||
|
<DefaultRenderEmpty componentName="List" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const paginationPosition = paginationProps.position || 'bottom';
|
const paginationPosition = paginationProps.position || 'bottom';
|
||||||
|
@ -9,7 +9,7 @@ import { composeRef } from 'rc-util/lib/ref';
|
|||||||
// eslint-disable-next-line import/no-named-as-default
|
// eslint-disable-next-line import/no-named-as-default
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import { FormItemInputContext } from '../form/context';
|
import { FormItemInputContext } from '../form/context';
|
||||||
import genPurePanel from '../_util/PurePanel';
|
import genPurePanel from '../_util/PurePanel';
|
||||||
import Spin from '../spin';
|
import Spin from '../spin';
|
||||||
@ -118,13 +118,12 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
|
|||||||
setFocused(false);
|
setFocused(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getNotFoundContent = () => {
|
const notFoundContentEle = React.useMemo<React.ReactNode>(() => {
|
||||||
if (notFoundContent !== undefined) {
|
if (notFoundContent !== undefined) {
|
||||||
return notFoundContent;
|
return notFoundContent;
|
||||||
}
|
}
|
||||||
|
return renderEmpty?.('Select') || <DefaultRenderEmpty componentName="Select" />;
|
||||||
return (renderEmpty || defaultRenderEmpty)('Select');
|
}, [notFoundContent, renderEmpty]);
|
||||||
};
|
|
||||||
|
|
||||||
const getOptions = () => {
|
const getOptions = () => {
|
||||||
if (loading) {
|
if (loading) {
|
||||||
@ -148,12 +147,7 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
|
|||||||
]
|
]
|
||||||
: options;
|
: options;
|
||||||
|
|
||||||
const getFilterOption = (): any => {
|
const mentionsfilterOption = loading ? loadingFilterOption : filterOption;
|
||||||
if (loading) {
|
|
||||||
return loadingFilterOption;
|
|
||||||
}
|
|
||||||
return filterOption;
|
|
||||||
};
|
|
||||||
|
|
||||||
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
|
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
|
||||||
|
|
||||||
@ -174,12 +168,12 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
|
|||||||
const mentions = (
|
const mentions = (
|
||||||
<RcMentions
|
<RcMentions
|
||||||
prefixCls={prefixCls}
|
prefixCls={prefixCls}
|
||||||
notFoundContent={getNotFoundContent()}
|
notFoundContent={notFoundContentEle}
|
||||||
className={mergedClassName}
|
className={mergedClassName}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
direction={direction}
|
direction={direction}
|
||||||
{...restProps}
|
{...restProps}
|
||||||
filterOption={getFilterOption()}
|
filterOption={mentionsfilterOption}
|
||||||
onFocus={onFocus}
|
onFocus={onFocus}
|
||||||
onBlur={onBlur}
|
onBlur={onBlur}
|
||||||
dropdownClassName={classNames(popupClassName, hashId)}
|
dropdownClassName={classNames(popupClassName, hashId)}
|
||||||
|
@ -8,7 +8,7 @@ import type { BaseOptionType, DefaultOptionType } from 'rc-select/lib/Select';
|
|||||||
import omit from 'rc-util/lib/omit';
|
import omit from 'rc-util/lib/omit';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import DisabledContext from '../config-provider/DisabledContext';
|
import DisabledContext from '../config-provider/DisabledContext';
|
||||||
import type { SizeType } from '../config-provider/SizeContext';
|
import type { SizeType } from '../config-provider/SizeContext';
|
||||||
import SizeContext from '../config-provider/SizeContext';
|
import SizeContext from '../config-provider/SizeContext';
|
||||||
@ -135,7 +135,7 @@ const InternalSelect = <OptionType extends BaseOptionType | DefaultOptionType =
|
|||||||
} else if (mode === 'combobox') {
|
} else if (mode === 'combobox') {
|
||||||
mergedNotFound = null;
|
mergedNotFound = null;
|
||||||
} else {
|
} else {
|
||||||
mergedNotFound = (renderEmpty || defaultRenderEmpty)('Select');
|
mergedNotFound = renderEmpty?.('Select') || <DefaultRenderEmpty componentName="Select" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===================== Icons =====================
|
// ===================== Icons =====================
|
||||||
|
@ -6,7 +6,7 @@ import omit from 'rc-util/lib/omit';
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import type { ConfigConsumerProps } from '../config-provider/context';
|
import type { ConfigConsumerProps } from '../config-provider/context';
|
||||||
import { ConfigContext } from '../config-provider/context';
|
import { ConfigContext } from '../config-provider/context';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import type { SizeType } from '../config-provider/SizeContext';
|
import type { SizeType } from '../config-provider/SizeContext';
|
||||||
import SizeContext from '../config-provider/SizeContext';
|
import SizeContext from '../config-provider/SizeContext';
|
||||||
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
||||||
@ -519,6 +519,11 @@ function InternalTable<RecordType extends object = any>(
|
|||||||
className,
|
className,
|
||||||
hashId,
|
hashId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const emptyText = (locale && locale.emptyText) || renderEmpty?.('Table') || (
|
||||||
|
<DefaultRenderEmpty componentName="Table" />
|
||||||
|
);
|
||||||
|
|
||||||
return wrapSSR(
|
return wrapSSR(
|
||||||
<div ref={ref} className={wrapperClassNames} style={style}>
|
<div ref={ref} className={wrapperClassNames} style={style}>
|
||||||
<Spin spinning={false} {...spinProps}>
|
<Spin spinning={false} {...spinProps}>
|
||||||
@ -538,7 +543,7 @@ function InternalTable<RecordType extends object = any>(
|
|||||||
data={pageData}
|
data={pageData}
|
||||||
rowKey={getRowKey}
|
rowKey={getRowKey}
|
||||||
rowClassName={internalRowClassName}
|
rowClassName={internalRowClassName}
|
||||||
emptyText={(locale && locale.emptyText) || (renderEmpty || defaultRenderEmpty)('Table')}
|
emptyText={emptyText}
|
||||||
// Internal
|
// Internal
|
||||||
internalHooks={INTERNAL_HOOKS}
|
internalHooks={INTERNAL_HOOKS}
|
||||||
internalRefs={internalRefs as any}
|
internalRefs={internalRefs as any}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||||
import type { ChangeEvent, CSSProperties } from 'react';
|
import type { ChangeEvent, CSSProperties } from 'react';
|
||||||
import type { ConfigConsumerProps, RenderEmptyHandler } from '../config-provider';
|
import type { ConfigConsumerProps } from '../config-provider';
|
||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import type { FormItemStatusContextProps } from '../form/context';
|
import type { FormItemStatusContextProps } from '../form/context';
|
||||||
import { FormItemInputContext } from '../form/context';
|
import { FormItemInputContext } from '../form/context';
|
||||||
import LocaleReceiver from '../locale/LocaleReceiver';
|
import LocaleReceiver from '../locale/LocaleReceiver';
|
||||||
@ -201,12 +201,6 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
|
|||||||
const getTitles = (transferLocale: TransferLocale): React.ReactNode[] =>
|
const getTitles = (transferLocale: TransferLocale): React.ReactNode[] =>
|
||||||
titles ?? transferLocale.titles ?? [];
|
titles ?? transferLocale.titles ?? [];
|
||||||
|
|
||||||
const getLocale = (transferLocale: TransferLocale, renderEmpty: RenderEmptyHandler) => ({
|
|
||||||
...transferLocale,
|
|
||||||
notFoundContent: renderEmpty('Transfer'),
|
|
||||||
...locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleLeftScroll = (e: React.SyntheticEvent<HTMLUListElement>) => {
|
const handleLeftScroll = (e: React.SyntheticEvent<HTMLUListElement>) => {
|
||||||
onScroll?.('left', e);
|
onScroll?.('left', e);
|
||||||
};
|
};
|
||||||
@ -338,6 +332,12 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
|
|||||||
const { getPrefixCls, renderEmpty, direction } = configContext;
|
const { getPrefixCls, renderEmpty, direction } = configContext;
|
||||||
const { hasFeedback, status } = formItemContext;
|
const { hasFeedback, status } = formItemContext;
|
||||||
|
|
||||||
|
const getLocale = (transferLocale: TransferLocale) => ({
|
||||||
|
...transferLocale,
|
||||||
|
notFoundContent: renderEmpty?.('Transfer') || <DefaultRenderEmpty componentName="Transfer" />,
|
||||||
|
...locale,
|
||||||
|
});
|
||||||
|
|
||||||
const prefixCls = getPrefixCls('transfer', customizePrefixCls);
|
const prefixCls = getPrefixCls('transfer', customizePrefixCls);
|
||||||
const mergedStatus = getMergedStatus(status, customStatus);
|
const mergedStatus = getMergedStatus(status, customStatus);
|
||||||
const mergedPagination = !children && pagination;
|
const mergedPagination = !children && pagination;
|
||||||
@ -359,7 +359,7 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
|
|||||||
return (
|
return (
|
||||||
<LocaleReceiver componentName="Transfer" defaultLocale={defaultLocale.Transfer}>
|
<LocaleReceiver componentName="Transfer" defaultLocale={defaultLocale.Transfer}>
|
||||||
{(contextLocale) => {
|
{(contextLocale) => {
|
||||||
const listLocale = getLocale(contextLocale, renderEmpty || defaultRenderEmpty);
|
const listLocale = getLocale(contextLocale);
|
||||||
const [leftTitle, rightTitle] = getTitles(listLocale);
|
const [leftTitle, rightTitle] = getTitles(listLocale);
|
||||||
return (
|
return (
|
||||||
<TransferFC prefixCls={prefixCls} className={cls} style={style}>
|
<TransferFC prefixCls={prefixCls} className={cls} style={style}>
|
||||||
|
@ -238,7 +238,7 @@ const TransferList = <RecordType extends KeyWiseTransferItem>(
|
|||||||
|
|
||||||
const checkBox = (
|
const checkBox = (
|
||||||
<Checkbox
|
<Checkbox
|
||||||
disabled={dataSource.length === 0 || props.disabled}
|
disabled={dataSource.length === 0 || disabled}
|
||||||
checked={checkStatus === 'all'}
|
checked={checkStatus === 'all'}
|
||||||
indeterminate={checkStatus === 'part'}
|
indeterminate={checkStatus === 'part'}
|
||||||
className={`${prefixCls}-checkbox`}
|
className={`${prefixCls}-checkbox`}
|
||||||
|
@ -5,8 +5,9 @@ import RcTreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode } from 'rc-tr
|
|||||||
import type { BaseOptionType, DefaultOptionType } from 'rc-tree-select/lib/TreeSelect';
|
import type { BaseOptionType, DefaultOptionType } from 'rc-tree-select/lib/TreeSelect';
|
||||||
import omit from 'rc-util/lib/omit';
|
import omit from 'rc-util/lib/omit';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import type { Placement } from 'rc-select/lib/BaseSelect';
|
||||||
import { ConfigContext } from '../config-provider';
|
import { ConfigContext } from '../config-provider';
|
||||||
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
||||||
import DisabledContext from '../config-provider/DisabledContext';
|
import DisabledContext from '../config-provider/DisabledContext';
|
||||||
import type { SizeType } from '../config-provider/SizeContext';
|
import type { SizeType } from '../config-provider/SizeContext';
|
||||||
import SizeContext from '../config-provider/SizeContext';
|
import SizeContext from '../config-provider/SizeContext';
|
||||||
@ -160,7 +161,7 @@ const InternalTreeSelect = <OptionType extends BaseOptionType | DefaultOptionTyp
|
|||||||
if (notFoundContent !== undefined) {
|
if (notFoundContent !== undefined) {
|
||||||
mergedNotFound = notFoundContent;
|
mergedNotFound = notFoundContent;
|
||||||
} else {
|
} else {
|
||||||
mergedNotFound = (renderEmpty || defaultRenderEmpty)('Select');
|
mergedNotFound = renderEmpty?.('Select') || <DefaultRenderEmpty componentName="Select" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Render =====================
|
// ==================== Render =====================
|
||||||
@ -173,13 +174,11 @@ const InternalTreeSelect = <OptionType extends BaseOptionType | DefaultOptionTyp
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
// ===================== Placement =====================
|
// ===================== Placement =====================
|
||||||
const getPlacement = () => {
|
const getPlacement = (): Placement => {
|
||||||
if (placement !== undefined) {
|
if (placement !== undefined) {
|
||||||
return placement;
|
return placement;
|
||||||
}
|
}
|
||||||
return direction === 'rtl'
|
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
|
||||||
? ('bottomRight' as SelectCommonPlacement)
|
|
||||||
: ('bottomLeft' as SelectCommonPlacement);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const mergedSize = compactSize || customizeSize || size;
|
const mergedSize = compactSize || customizeSize || size;
|
||||||
|
Loading…
Reference in New Issue
Block a user