feat: CP support collapse expandIcon (#47473)

* feat: CP support Avatar icon

* fix: fix

* fix: fix

* test: fix test case

* fix: fix

* fix: fix type

* Update avatar.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update avatar.tsx

Signed-off-by: lijianan <574980606@qq.com>

---------

Signed-off-by: lijianan <574980606@qq.com>
This commit is contained in:
lijianan 2024-02-20 14:02:43 +08:00 committed by GitHub
parent b96f4c8003
commit cf51b5f34d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 34 additions and 28 deletions

View File

@ -1,4 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import type { AnyObject } from './type'; import type { AnyObject } from './type';
export const { isValidElement } = React; export const { isValidElement } = React;
@ -9,20 +10,20 @@ export function isFragment(child: any): boolean {
type RenderProps = AnyObject | ((originProps: AnyObject) => AnyObject | void); type RenderProps = AnyObject | ((originProps: AnyObject) => AnyObject | void);
export function replaceElement( export function replaceElement<P>(
element: React.ReactNode, element: React.ReactNode,
replacement: React.ReactNode, replacement: React.ReactNode,
props?: RenderProps, props?: RenderProps,
): React.ReactNode { ) {
if (!isValidElement(element)) { if (!isValidElement<P>(element)) {
return replacement; return replacement;
} }
return React.cloneElement( return React.cloneElement<P>(
element, element,
typeof props === 'function' ? props(element.props || {}) : props, typeof props === 'function' ? props(element.props || {}) : props,
); );
} }
export function cloneElement(element: React.ReactNode, props?: RenderProps): React.ReactElement { export function cloneElement<P>(element: React.ReactNode, props?: RenderProps) {
return replaceElement(element, element, props) as React.ReactElement; return replaceElement<P>(element, element, props) as React.ReactElement;
} }

View File

@ -90,29 +90,29 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
} }
// Align with logic position // Align with logic position
const mergedExpandIconPosition = React.useMemo(() => { const mergedExpandIconPosition = React.useMemo<'start' | 'end'>(() => {
if (expandIconPosition === 'left') { if (expandIconPosition === 'left') {
return 'start'; return 'start';
} }
return expandIconPosition === 'right' ? 'end' : expandIconPosition; return expandIconPosition === 'right' ? 'end' : expandIconPosition;
}, [expandIconPosition]); }, [expandIconPosition]);
const renderExpandIcon = (panelProps: PanelProps = {}) => { const mergedExpandIcon = expandIcon ?? collapse?.expandIcon;
const icon = (
expandIcon ? ( const renderExpandIcon = React.useCallback(
expandIcon(panelProps) (panelProps: PanelProps = {}) => {
const icon =
typeof mergedExpandIcon === 'function' ? (
mergedExpandIcon(panelProps)
) : ( ) : (
<RightOutlined rotate={panelProps.isActive ? 90 : undefined} /> <RightOutlined rotate={panelProps.isActive ? 90 : undefined} />
) );
) as React.ReactNode;
return cloneElement(icon, () => ({ return cloneElement(icon, () => ({
className: classNames( className: classNames((icon as React.ReactElement)?.props?.className, `${prefixCls}-arrow`),
(icon as React.ReactElement<any>).props.className,
`${prefixCls}-arrow`,
),
})); }));
}; },
[mergedExpandIcon, prefixCls],
);
const collapseClassName = classNames( const collapseClassName = classNames(
`${prefixCls}-icon-position-${mergedExpandIconPosition}`, `${prefixCls}-icon-position-${mergedExpandIconPosition}`,

View File

@ -273,7 +273,7 @@ describe('ConfigProvider support style and className props', () => {
expect(element).toHaveStyle({ backgroundColor: 'red' }); expect(element).toHaveStyle({ backgroundColor: 'red' });
}); });
it('Should Collapse className works', () => { it('Should Collapse className & expandIcon works', () => {
const items = [ const items = [
{ {
key: '1', key: '1',
@ -285,13 +285,14 @@ describe('ConfigProvider support style and className props', () => {
<ConfigProvider <ConfigProvider
collapse={{ collapse={{
className: 'test-class', className: 'test-class',
expandIcon: (props) => <span className="cp-test-icon">{props.isActive}</span>,
}} }}
> >
<Collapse items={items} /> <Collapse items={items} />
</ConfigProvider>, </ConfigProvider>,
); );
expect(container.querySelector('.ant-collapse')).toHaveClass('test-class'); expect(container.querySelector('.ant-collapse')).toHaveClass('test-class');
expect(container.querySelector<HTMLSpanElement>('.cp-test-icon')).toBeTruthy();
}); });
it('Should Collapse style works', () => { it('Should Collapse style works', () => {

View File

@ -6,6 +6,7 @@ import type { AlertProps } from '../alert';
import type { BadgeProps } from '../badge'; import type { BadgeProps } from '../badge';
import type { ButtonProps } from '../button'; import type { ButtonProps } from '../button';
import type { CardProps } from '../card'; import type { CardProps } from '../card';
import type { CollapseProps } from '../collapse';
import type { DrawerProps } from '../drawer'; import type { DrawerProps } from '../drawer';
import type { FlexProps } from '../flex/interface'; import type { FlexProps } from '../flex/interface';
import type { FormProps } from '../form/Form'; import type { FormProps } from '../form/Form';
@ -82,6 +83,8 @@ export interface ImageConfig extends ComponentStyleConfig {
preview?: Partial<Record<'closeIcon', React.ReactNode>>; preview?: Partial<Record<'closeIcon', React.ReactNode>>;
} }
export type CollapseConfig = ComponentStyleConfig & Pick<CollapseProps, 'expandIcon'>;
export type TourConfig = Pick<TourProps, 'closeIcon'>; export type TourConfig = Pick<TourProps, 'closeIcon'>;
export type ModalConfig = ComponentStyleConfig & export type ModalConfig = ComponentStyleConfig &
@ -148,7 +151,7 @@ export interface ConfigConsumerProps {
calendar?: ComponentStyleConfig; calendar?: ComponentStyleConfig;
carousel?: ComponentStyleConfig; carousel?: ComponentStyleConfig;
cascader?: ComponentStyleConfig; cascader?: ComponentStyleConfig;
collapse?: ComponentStyleConfig; collapse?: CollapseConfig;
typography?: ComponentStyleConfig; typography?: ComponentStyleConfig;
skeleton?: ComponentStyleConfig; skeleton?: ComponentStyleConfig;
spin?: ComponentStyleConfig; spin?: ComponentStyleConfig;

View File

@ -112,7 +112,7 @@ const {
| carousel | Set Carousel common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | carousel | Set Carousel common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| cascader | Set Cascader common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | cascader | Set Cascader common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| checkbox | Set Checkbox common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | checkbox | Set Checkbox common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| collapse | Set Collapse common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | collapse | Set Collapse common props | { className?: string, style?: React.CSSProperties, expandIcon?: (props) => ReactNode } | - | 5.7.0, expandIcon: 5.15.0 |
| colorPicker | Set ColorPicker common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | colorPicker | Set ColorPicker common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| datePicker | Set datePicker common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | datePicker | Set datePicker common props | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| rangePicker | Set rangePicker common props | { className?: string, style?: React.CSSProperties } | - | 5.11.0 | | rangePicker | Set rangePicker common props | { className?: string, style?: React.CSSProperties } | - | 5.11.0 |

View File

@ -24,6 +24,7 @@ import type {
BadgeConfig, BadgeConfig,
ButtonConfig, ButtonConfig,
CardConfig, CardConfig,
CollapseConfig,
ComponentStyleConfig, ComponentStyleConfig,
ConfigConsumerProps, ConfigConsumerProps,
CSPConfig, CSPConfig,
@ -142,7 +143,7 @@ export interface ConfigProviderProps {
calendar?: ComponentStyleConfig; calendar?: ComponentStyleConfig;
carousel?: ComponentStyleConfig; carousel?: ComponentStyleConfig;
cascader?: ComponentStyleConfig; cascader?: ComponentStyleConfig;
collapse?: ComponentStyleConfig; collapse?: CollapseConfig;
divider?: ComponentStyleConfig; divider?: ComponentStyleConfig;
drawer?: DrawerConfig; drawer?: DrawerConfig;
typography?: ComponentStyleConfig; typography?: ComponentStyleConfig;

View File

@ -114,7 +114,7 @@ const {
| carousel | 设置 Carousel 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | carousel | 设置 Carousel 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| cascader | 设置 Cascader 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | cascader | 设置 Cascader 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| checkbox | 设置 Checkbox 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | checkbox | 设置 Checkbox 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| collapse | 设置 Collapse 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | collapse | 设置 Collapse 组件的通用属性 | { className?: string, style?: React.CSSProperties, expandIcon?: (props) => ReactNode } | - | 5.7.0, expandIcon: 5.15.0 |
| colorPicker | 设置 ColorPicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | colorPicker | 设置 ColorPicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| datePicker | 设置 DatePicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 | | datePicker | 设置 DatePicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
| rangePicker | 设置 RangePicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.11.0 | | rangePicker | 设置 RangePicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.11.0 |