Skip to content

Commit

Permalink
refactor: Wrap all part with element holder (#233)
Browse files Browse the repository at this point in the history
* chore: tmp

* test: coverage
  • Loading branch information
zombieJ committed May 27, 2022
1 parent a280779 commit 71c56e1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
73 changes: 44 additions & 29 deletions src/Panel.tsx
Expand Up @@ -19,7 +19,7 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
return !shallowEqual(this.props, nextProps);
}

handleItemClick = () => {
onItemClick = () => {
const { onItemClick, panelKey } = this.props;

if (typeof onItemClick === 'function') {
Expand All @@ -29,37 +29,64 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {

handleKeyPress = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' || e.keyCode === 13 || e.which === 13) {
this.handleItemClick();
this.onItemClick();
}
};

renderIcon = () => {
const { showArrow, expandIcon, prefixCls, collapsible } = this.props;
if (!showArrow) {
return null;
}

const iconNode =
typeof expandIcon === 'function' ? expandIcon(this.props) : <i className="arrow" />;

return (
iconNode && (
<div
className={`${prefixCls}-expand-icon`}
onClick={collapsible === 'header' ? this.onItemClick : null}
>
{iconNode}
</div>
)
);
};

renderTitle = () => {
const { header, prefixCls, collapsible } = this.props;

return (
<span
className={`${prefixCls}-header-text`}
onClick={collapsible === 'header' ? this.onItemClick : null}
>
{header}
</span>
);
};

render() {
const {
className,
id,
style,
prefixCls,
header,
headerClass,
children,
isActive,
showArrow,
destroyInactivePanel,
accordion,
forceRender,
openMotion,
expandIcon,
extra,
collapsible,
} = this.props;

const disabled = collapsible === 'disabled';
const collapsibleHeader = collapsible === 'header';

const headerCls = classNames(`${prefixCls}-header`, {
[headerClass]: headerClass,
[`${prefixCls}-header-collapsible-only`]: collapsibleHeader,
});
const itemCls = classNames(
{
[`${prefixCls}-item`]: true,
Expand All @@ -69,7 +96,10 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
className,
);

let icon: any = <i className="arrow" />;
const headerCls = classNames(`${prefixCls}-header`, {
[headerClass]: headerClass,
[`${prefixCls}-header-collapsible-only`]: collapsibleHeader,
});

/** header 节点属性 */
const headerProps: React.HTMLAttributes<HTMLDivElement> = {
Expand All @@ -78,17 +108,8 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
onKeyPress: this.handleKeyPress,
};

if (showArrow && typeof expandIcon === 'function') {
icon = expandIcon(this.props);
}
if (collapsibleHeader) {
icon = (
<span style={{ cursor: 'pointer' }} onClick={() => this.handleItemClick()}>
{icon}
</span>
);
} else {
headerProps.onClick = this.handleItemClick;
if (!collapsibleHeader) {
headerProps.onClick = this.onItemClick;
headerProps.role = accordion ? 'tab' : 'button';
headerProps.tabIndex = disabled ? -1 : 0;
}
Expand All @@ -98,14 +119,8 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
return (
<div className={itemCls} style={style} id={id}>
<div {...headerProps}>
{showArrow && icon}
{collapsibleHeader ? (
<span onClick={this.handleItemClick} className={`${prefixCls}-header-text`}>
{header}
</span>
) : (
header
)}
{this.renderIcon()}
{this.renderTitle()}
{ifExtraExist && <div className={`${prefixCls}-extra`}>{extra}</div>}
</div>
<CSSMotion
Expand Down
21 changes: 16 additions & 5 deletions tests/index.spec.tsx
Expand Up @@ -139,8 +139,7 @@ describe('collapse', () => {
activeKey: ['2'],
};

constructor(props: any) {
super(props);
componentDidMount(): void {
this.setState({
activeKey: ['2'],
});
Expand Down Expand Up @@ -495,7 +494,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(collapse.find('.rc-collapse-header-text').exists()).toBeFalsy();
expect(collapse.find('.rc-collapse-header-text').exists()).toBeTruthy();
collapse.find('.rc-collapse-header').simulate('click');
expect(collapse.find('.rc-collapse-item-active').length).toBe(1);
});
Expand All @@ -522,7 +521,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(collapse.find('.rc-collapse-header-text').exists()).toBeFalsy();
expect(collapse.find('.rc-collapse-header-text').exists()).toBeTruthy();

expect(collapse.find('.rc-collapse-item-disabled').length).toBe(1);

Expand All @@ -538,7 +537,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(collapse.find('.rc-collapse-header-text').exists()).toBeFalsy();
expect(collapse.find('.rc-collapse-header-text').exists()).toBeTruthy();

expect(collapse.find('.rc-collapse-item-disabled').length).toBe(1);

Expand All @@ -559,4 +558,16 @@ describe('collapse', () => {
expect(collapse.find('.rc-collapse-item-active').length).toBe(1);
});
});

it('!showArrow', () => {
const wrapper = mount(
<Collapse>
<Panel header="collapse 1" key="1" showArrow={false}>
first
</Panel>
</Collapse>,
);

expect(wrapper.exists('.rc-collapse-expand-icon')).toBeFalsy();
});
});

1 comment on commit 71c56e1

@vercel
Copy link

@vercel vercel bot commented on 71c56e1 May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.