Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加headerRender #231

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions examples/custom-collapse-header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import * as React from 'react';
import Collapse, { Panel } from '../src';
import motion from './_util/motionUtil';
import '../assets/index.less';

const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;

function random() {
return parseInt((Math.random() * 10).toString(), 10) + 1;
}

function customHeaderRender({ header, isActive, extra }) {
return (
<div style={{ display: 'flex' }}>
<div>
{header}
<span style={{ marginLeft: 10 }}>{isActive ? '收起' : '展开'}</span>
</div>
<div style={{ marginLeft: 20 }}>{extra}</div>
</div>
);
}

class Test extends React.Component {
state = {
time: random(),
accordion: false,
activeKey: ['4'],
collapsible: undefined,
};

onChange = (activeKey: string) => {
this.setState({
activeKey,
});
};

getItems() {
const items = [];
// eslint-disable-next-line no-plusplus
for (let i = 0, len = 3; i < len; i++) {
const key = i + 1;
items.push(
<Panel
header={`This is panel header ${key}`}
key={key}
collapsible={i === 0 ? 'disabled' : undefined}
>
<p>{text.repeat(this.state.time)}</p>
</Panel>,
);
}
items.push(
<Panel header="This is panel header 4" key="4">
<Collapse defaultActiveKey="1">
<Panel header="This is panel nest panel" key="41" id="header-test">
<p>{text}</p>
</Panel>
</Collapse>
</Panel>,
);

items.push(
<Panel header="This is panel header 5" key="5">
<Collapse defaultActiveKey="1">
<Panel header="This is panel nest panel" key="51" id="another-test">
<form>
<label htmlFor="test">Name:&nbsp;</label>
<input type="text" id="test" />
</form>
</Panel>
</Collapse>
</Panel>,
);

items.push(
<Panel header="This is panel header 6" key="6" extra={<span>Extra Node</span>}>
<p>Panel with extra</p>
</Panel>,
);

return items;
}

setActivityKey = () => {
this.setState({
activeKey: ['2'],
});
};

reRender = () => {
this.setState({
time: random(),
});
};

toggle = () => {
const { accordion } = this.state;
this.setState({
accordion: !accordion,
});
};

handleCollapsibleChange = (e: any) => {
const values = [undefined, 'header', 'disabled'];
this.setState({
collapsible: values[e.target.value],
});
};

render() {
const { accordion, activeKey, collapsible } = this.state;
const btn = accordion ? 'Mode: accordion' : 'Mode: collapse';
return (
<div style={{ margin: 20, width: 400 }}>
<button type="button" onClick={this.reRender}>
reRender
</button>
<button type="button" onClick={this.toggle}>
{btn}
</button>
<p>
collapsible:
<select onChange={this.handleCollapsibleChange}>
<option value={0}>default</option>
<option value={1}>header</option>
<option value={2}>disabled</option>
</select>
</p>
<br />
<br />
<button type="button" onClick={this.setActivityKey}>
active header 2
</button>
<br />
<br />
<Collapse
collapsible={collapsible}
accordion={accordion}
onChange={this.onChange}
activeKey={activeKey}
openMotion={motion}
// 获取panel props来自定义渲染header
// 比panel header上的其他配置项优先级高
headerRender={customHeaderRender}
>
{this.getItems()}
</Collapse>
</div>
);
}
}

export default Test;
2 changes: 2 additions & 0 deletions src/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Collapse extends React.Component<CollapseProps, CollapseState> {
accordion,
destroyInactivePanel: rootDestroyInactivePanel,
expandIcon,
headerRender,
collapsible,
} = this.props;
// If there is no key provide, use the panel order as default key
Expand Down Expand Up @@ -116,6 +117,7 @@ class Collapse extends React.Component<CollapseProps, CollapseState> {
children: child.props.children,
onItemClick: mergeCollapsible === 'disabled' ? null : this.onClickItem,
expandIcon,
headerRender,
collapsible: mergeCollapsible,
};

Expand Down
13 changes: 10 additions & 3 deletions src/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
accordion,
forceRender,
openMotion,
headerRender,
extra,
collapsible,
...rest
Expand Down Expand Up @@ -121,9 +122,15 @@ class CollapsePanel extends React.Component<CollapsePanelProps, any> {
return (
<div {...rest} className={itemCls} style={style} id={id}>
<div {...headerProps}>
{this.renderIcon()}
{this.renderTitle()}
{ifExtraExist && <div className={`${prefixCls}-extra`}>{extra}</div>}
{headerRender && typeof headerRender === 'function' ? (
headerRender(this.props)
) : (
<>
{this.renderIcon()}
{this.renderTitle()}
{ifExtraExist && <div className={`${prefixCls}-extra`}>{extra}</div>}
</>
)}
</div>
<CSSMotion
visible={isActive}
Expand Down
2 changes: 2 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CollapseProps {
expandIcon?: (props: object) => React.ReactNode;
collapsible?: CollapsibleType;
children?: React.ReactNode;
headerRender?: (props: object) => React.ReactNode;
}

export interface CollapsePanelProps extends React.DOMAttributes<HTMLDivElement> {
Expand All @@ -34,6 +35,7 @@ export interface CollapsePanelProps extends React.DOMAttributes<HTMLDivElement>
extra?: string | React.ReactNode;
onItemClick?: (panelKey: string | number) => void;
expandIcon?: (props: object) => React.ReactNode;
headerRender?: (props: object) => React.ReactNode;
panelKey?: string | number;
role?: string;
collapsible?: CollapsibleType;
Expand Down
45 changes: 45 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,49 @@ describe('collapse', () => {
wrapper.find('.target').simulate('click');
expect(clickHandler).toHaveBeenCalled();
});

describe('customHeader', () => {
const customHeaderRender = ({ header, isActive, extra }) => {
return (
<div style={{ display: 'flex' }}>
<div>
{header}
<span style={{ marginLeft: 10 }}>{isActive ? '收起' : '展开'}</span>
</div>
<div style={{ marginLeft: 20 }}>{extra}</div>
</div>
);
};

let collapse: ReactWrapper;
beforeEach(() => {
collapse = mount(
<Collapse onChange={onChange} headerRender={customHeaderRender}>
<Panel header="collapse 1" key="1" collapsible="disabled">
first
</Panel>
<Panel header="collapse 2" key="2" extra={<span>ExtraSpan</span>}>
second
</Panel>
<Panel header="collapse 3" key="3" className="important">
third
</Panel>
</Collapse>,
);
});

it('should toggle show on panel', () => {
let header = collapse.find('.rc-collapse-header').at(1);
header.simulate('click');
jest.runAllTimers();
collapse.update();
expect(collapse.find('.rc-collapse-content-active').length).toBe(1);
expect(collapse.find('.rc-collapse-item-active').length).toBe(1);
header = collapse.find('.rc-collapse-header').at(1);
header.simulate('click');
jest.runAllTimers();
collapse.update();
expect(collapse.find('.rc-collapse-content-active').exists()).toBeFalsy();
expect(collapse.find('.rc-collapse-item-active').exists()).toBeFalsy();
});
});