Skip to content

Commit f26aed0

Browse files
MattinaYangyangchen7
andauthored
feat: add icon to collapsible (#232)
* feat: add icon to collapsible * refactor: test icon can be trigger Co-authored-by: yangchen7 <yangchen7@360.cn>
1 parent e590e79 commit f26aed0

7 files changed

Lines changed: 52 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ If `accordion` is true, only one panel can be open. Opening another panel will
196196
</tr>
197197
<tr>
198198
<td>collapsible</td>
199-
<td>'header' | 'disabled'</td>
199+
<td>'header' | 'icon' | 'disabled'</td>
200200
<th>-</th>
201201
<td>specify whether the panel be collapsible or the area of collapsible.</td>
202202
</tr>

assets/index.less

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
.@{prefixCls}-header-text {
6565
cursor: pointer;
6666
}
67+
.@{prefixCls}-expand-icon {
68+
cursor: pointer;
69+
}
70+
}
71+
.@{prefixCls}-icon-collapsible-only {
72+
cursor: default;
73+
.@{prefixCls}-expand-icon {
74+
cursor: pointer;
75+
}
6776
}
6877
}
6978

docs/examples/simple.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Test extends React.Component {
9494
};
9595

9696
handleCollapsibleChange = (e: any) => {
97-
const values = [undefined, 'header', 'disabled'];
97+
const values = [undefined, 'header', 'icon', 'disabled'];
9898
this.setState({
9999
collapsible: values[e.target.value],
100100
});
@@ -116,7 +116,8 @@ class Test extends React.Component {
116116
<select onChange={this.handleCollapsibleChange}>
117117
<option value={0}>default</option>
118118
<option value={1}>header</option>
119-
<option value={2}>disabled</option>
119+
<option value={2}>icon</option>
120+
<option value={3}>disabled</option>
120121
</select>
121122
</p>
122123
<br />

src/Collapse.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable react/prop-types */
2-
import * as React from 'react';
32
import classNames from 'classnames';
4-
import shallowEqual from 'shallowequal';
53
import toArray from 'rc-util/lib/Children/toArray';
6-
import CollapsePanel from './Panel';
4+
import * as React from 'react';
5+
import shallowEqual from 'shallowequal';
76
import type { CollapseProps, CollapsibleType } from './interface';
7+
import CollapsePanel from './Panel';
88

99
function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
1010
let currentActiveKey = activeKey;

src/Panel.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable react/prop-types */
2-
import * as React from 'react';
32
import classNames from 'classnames';
43
import CSSMotion from 'rc-motion';
4+
import * as React from 'react';
55
import shallowEqual from 'shallowequal';
6-
import PanelContent from './PanelContent';
76
import type { CollapsePanelProps } from './interface';
7+
import PanelContent from './PanelContent';
88

99
class CollapsePanel extends React.Component<CollapsePanelProps, any> {
1010
static defaultProps = {
@@ -46,7 +46,7 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
4646
iconNode && (
4747
<div
4848
className={`${prefixCls}-expand-icon`}
49-
onClick={collapsible === 'header' ? this.onItemClick : null}
49+
onClick={collapsible === 'header' || collapsible === 'icon' ? this.onItemClick : null}
5050
>
5151
{iconNode}
5252
</div>
@@ -87,6 +87,7 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
8787

8888
const disabled = collapsible === 'disabled';
8989
const collapsibleHeader = collapsible === 'header';
90+
const collapsibleIcon = collapsible === 'icon';
9091

9192
const itemCls = classNames(
9293
{
@@ -100,6 +101,7 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
100101
const headerCls = classNames(`${prefixCls}-header`, {
101102
[headerClass]: headerClass,
102103
[`${prefixCls}-header-collapsible-only`]: collapsibleHeader,
104+
[`${prefixCls}-icon-collapsible-only`]: collapsibleIcon,
103105
});
104106

105107
/** header 节点属性 */
@@ -110,7 +112,7 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
110112
onKeyPress: this.handleKeyPress,
111113
};
112114

113-
if (!collapsibleHeader) {
115+
if (!collapsibleHeader && !collapsibleIcon) {
114116
headerProps.onClick = this.onItemClick;
115117
headerProps.role = accordion ? 'tab' : 'button';
116118
headerProps.tabIndex = disabled ? -1 : 0;

src/interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type * as React from 'react';
21
import type { CSSMotionProps } from 'rc-motion';
2+
import type * as React from 'react';
33

4-
export type CollapsibleType = 'header' | 'disabled';
4+
export type CollapsibleType = 'header' | 'disabled' | 'icon';
55

66
export interface CollapseProps {
77
prefixCls?: string;

tests/index.spec.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import React, { Fragment } from 'react';
21
import type { ReactWrapper } from 'enzyme';
32
import { mount } from 'enzyme';
4-
53
import KeyCode from 'rc-util/lib/KeyCode';
6-
4+
import React, { Fragment } from 'react';
75
import Collapse, { Panel } from '../src/index';
86

97
describe('collapse', () => {
@@ -540,6 +538,20 @@ describe('collapse', () => {
540538
collapse.find('.rc-collapse-header-text').simulate('click');
541539
expect(collapse.find('.rc-collapse-item-active').length).toBe(1);
542540
});
541+
it('should work when value is icon', () => {
542+
const collapse = mount(
543+
<Collapse collapsible="icon">
544+
<Panel header="collapse 1" key="1">
545+
first
546+
</Panel>
547+
</Collapse>,
548+
);
549+
expect(collapse.find('.rc-collapse-expand-icon').exists()).toBeTruthy();
550+
collapse.find('.rc-collapse-header').simulate('click');
551+
expect(collapse.find('.rc-collapse-item-active').length).toBe(0);
552+
collapse.find('.rc-collapse-expand-icon').simulate('click');
553+
expect(collapse.find('.rc-collapse-item-active').length).toBe(1);
554+
});
543555

544556
it('should disabled when value is disabled', () => {
545557
const collapse = mount(
@@ -585,6 +597,19 @@ describe('collapse', () => {
585597
collapse.find('.rc-collapse-header .arrow').simulate('click');
586598
expect(collapse.find('.rc-collapse-item-active').length).toBe(1);
587599
});
600+
601+
it('header not trigger when collapsible equal icon', () => {
602+
const collapse = mount(
603+
<Collapse collapsible="icon">
604+
<Panel header="collapse 1" key="1">
605+
first
606+
</Panel>
607+
</Collapse>,
608+
);
609+
610+
collapse.find('.rc-collapse-header-text').simulate('click');
611+
expect(collapse.find('.rc-collapse-item-active').length).toBe(0);
612+
});
588613
});
589614

590615
it('!showArrow', () => {

0 commit comments

Comments
 (0)