Skip to content

Commit

Permalink
docs: demos refactor function component (#299)
Browse files Browse the repository at this point in the history
* docs: demos refactor function component

* chore: update TS type
  • Loading branch information
Wxh16144 authored Jan 13, 2023
1 parent da97f2d commit 720d299
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 194 deletions.
145 changes: 60 additions & 85 deletions docs/examples/custom-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Collapse, { Panel } from 'rc-collapse';
import motion from './_util/motionUtil';
import '../../assets/index.less';

const initLength = 3;

const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
Expand Down Expand Up @@ -33,52 +35,42 @@ function expandIcon({ isActive }) {
transform: `rotate(${isActive ? 90 : 0}deg)`,
}}
>
<path d={arrowPath} p-id="5827" />
<path d={arrowPath} />
</svg>
</i>
);
}

class Test extends React.Component {
state = {
time: random(),
accordion: false,
activeKey: ['4'],
};
const App: React.FC = () => {
const [, reRender] = React.useState({});
const [accordion, setAccordion] = React.useState(false);
const [activeKey, setActiveKey] = React.useState<React.Key | React.Key[]>(['4']);

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

getItems() {
const items = [];
// eslint-disable-next-line no-plusplus
for (let i = 0, len = 3; i < len; i += 1) {
const key = i + 1;
items.push(
<Panel
header={`This is panel header ${key}`}
key={key}
// disabled={i === 0}
>
<p>{text.repeat(this.state.time)}</p>
</Panel>,
);
}
items.push(
<Panel header="This is panel header 4" key="4">
const panelItems = Array
.from<object, React.ReactNode>(
{ length: initLength },
(_, i) => {
const key = i + 1;
return (
<Panel
header={`This is panel header ${key}`}
key={key}
>
<p>{text.repeat(time)}</p>
</Panel>
)
})
.concat(
<Panel header={`This is panel header ${initLength + 1}`} key={initLength + 1}>
<Collapse defaultActiveKey="1" expandIcon={expandIcon}>
<Panel header="This is panel nest panel" key="1" id="header-test">
<p>{text}</p>
</Panel>
</Collapse>
</Panel>,
);

items.push(
<Panel header="This is panel header 5" key="5">
<Panel header={`This is panel header ${initLength + 2}`} key={initLength + 2}>
<Collapse defaultActiveKey="1">
<Panel header="This is panel nest panel" key="1" id="another-test">
<form>
Expand All @@ -87,61 +79,44 @@ class Test extends React.Component {
</form>
</Panel>
</Collapse>
</Panel>,
</Panel>
);

return items;
}

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

toggle = () => {
const { accordion } = this.state;
this.setState({
accordion: !accordion,
});
};
const tools = (
<>
<button type="button" onClick={() => reRender({})}>
reRender
</button>
<br />
<br />
<button type="button" onClick={() => setAccordion(prev => !prev)}>
{accordion ? 'Mode: accordion' : 'Mode: collapse'}
</button>
<br />
<br />
<button type="button" onClick={() => setActiveKey(['2'])}>
active header 2
</button>
<br />
<br />
</>
)

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

render() {
const { accordion, activeKey } = 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>
<br />
<br />
<button type="button" onClick={this.setActivityKey}>
active header 2
</button>
<br />
<br />
<Collapse
accordion={accordion}
onChange={this.onChange}
activeKey={activeKey}
expandIcon={expandIcon}
openMotion={motion}
>
{this.getItems()}
</Collapse>
</div>
);
}
return (
<>
{tools}
<Collapse
accordion={accordion}
onChange={setActiveKey}
activeKey={activeKey}
expandIcon={expandIcon}
openMotion={motion}
>
{panelItems}
</Collapse>
</>
)
}

export default Test;
export default App;
4 changes: 2 additions & 2 deletions docs/examples/fragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Fragment } from 'react';
import Collapse, { Panel } from 'rc-collapse';
import '../../assets/index.less';

const Test = () => (
const App = () => (
<Collapse>
<Panel header="title">content</Panel>
<Panel header="title">content</Panel>
Expand All @@ -20,4 +20,4 @@ const Test = () => (
</Collapse>
);

export default Test;
export default App;
Loading

1 comment on commit 720d299

@vercel
Copy link

@vercel vercel bot commented on 720d299 Jan 13, 2023

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.