Skip to content

Commit 720d299

Browse files
authored
docs: demos refactor function component (#299)
* docs: demos refactor function component * chore: update TS type
1 parent da97f2d commit 720d299

File tree

4 files changed

+190
-194
lines changed

4 files changed

+190
-194
lines changed

docs/examples/custom-icon.tsx

Lines changed: 60 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Collapse, { Panel } from 'rc-collapse';
33
import motion from './_util/motionUtil';
44
import '../../assets/index.less';
55

6+
const initLength = 3;
7+
68
const text = `
79
A dog is a type of domesticated animal.
810
Known for its loyalty and faithfulness,
@@ -33,52 +35,42 @@ function expandIcon({ isActive }) {
3335
transform: `rotate(${isActive ? 90 : 0}deg)`,
3436
}}
3537
>
36-
<path d={arrowPath} p-id="5827" />
38+
<path d={arrowPath} />
3739
</svg>
3840
</i>
3941
);
4042
}
4143

42-
class Test extends React.Component {
43-
state = {
44-
time: random(),
45-
accordion: false,
46-
activeKey: ['4'],
47-
};
44+
const App: React.FC = () => {
45+
const [, reRender] = React.useState({});
46+
const [accordion, setAccordion] = React.useState(false);
47+
const [activeKey, setActiveKey] = React.useState<React.Key | React.Key[]>(['4']);
4848

49-
onChange = (activeKey: string) => {
50-
this.setState({
51-
activeKey,
52-
});
53-
};
49+
const time = random();
5450

55-
getItems() {
56-
const items = [];
57-
// eslint-disable-next-line no-plusplus
58-
for (let i = 0, len = 3; i < len; i += 1) {
59-
const key = i + 1;
60-
items.push(
61-
<Panel
62-
header={`This is panel header ${key}`}
63-
key={key}
64-
// disabled={i === 0}
65-
>
66-
<p>{text.repeat(this.state.time)}</p>
67-
</Panel>,
68-
);
69-
}
70-
items.push(
71-
<Panel header="This is panel header 4" key="4">
51+
const panelItems = Array
52+
.from<object, React.ReactNode>(
53+
{ length: initLength },
54+
(_, i) => {
55+
const key = i + 1;
56+
return (
57+
<Panel
58+
header={`This is panel header ${key}`}
59+
key={key}
60+
>
61+
<p>{text.repeat(time)}</p>
62+
</Panel>
63+
)
64+
})
65+
.concat(
66+
<Panel header={`This is panel header ${initLength + 1}`} key={initLength + 1}>
7267
<Collapse defaultActiveKey="1" expandIcon={expandIcon}>
7368
<Panel header="This is panel nest panel" key="1" id="header-test">
7469
<p>{text}</p>
7570
</Panel>
7671
</Collapse>
7772
</Panel>,
78-
);
79-
80-
items.push(
81-
<Panel header="This is panel header 5" key="5">
73+
<Panel header={`This is panel header ${initLength + 2}`} key={initLength + 2}>
8274
<Collapse defaultActiveKey="1">
8375
<Panel header="This is panel nest panel" key="1" id="another-test">
8476
<form>
@@ -87,61 +79,44 @@ class Test extends React.Component {
8779
</form>
8880
</Panel>
8981
</Collapse>
90-
</Panel>,
82+
</Panel>
9183
);
9284

93-
return items;
94-
}
95-
96-
setActivityKey = () => {
97-
this.setState({
98-
activeKey: ['2'],
99-
});
100-
};
10185

102-
toggle = () => {
103-
const { accordion } = this.state;
104-
this.setState({
105-
accordion: !accordion,
106-
});
107-
};
86+
const tools = (
87+
<>
88+
<button type="button" onClick={() => reRender({})}>
89+
reRender
90+
</button>
91+
<br />
92+
<br />
93+
<button type="button" onClick={() => setAccordion(prev => !prev)}>
94+
{accordion ? 'Mode: accordion' : 'Mode: collapse'}
95+
</button>
96+
<br />
97+
<br />
98+
<button type="button" onClick={() => setActiveKey(['2'])}>
99+
active header 2
100+
</button>
101+
<br />
102+
<br />
103+
</>
104+
)
108105

109-
reRender = () => {
110-
this.setState({
111-
time: random(),
112-
});
113-
};
114-
115-
render() {
116-
const { accordion, activeKey } = this.state;
117-
const btn = accordion ? 'Mode: accordion' : 'Mode: collapse';
118-
return (
119-
<div style={{ margin: 20, width: 400 }}>
120-
<button type="button" onClick={this.reRender}>
121-
reRender
122-
</button>
123-
<button type="button" onClick={this.toggle}>
124-
{btn}
125-
</button>
126-
<br />
127-
<br />
128-
<button type="button" onClick={this.setActivityKey}>
129-
active header 2
130-
</button>
131-
<br />
132-
<br />
133-
<Collapse
134-
accordion={accordion}
135-
onChange={this.onChange}
136-
activeKey={activeKey}
137-
expandIcon={expandIcon}
138-
openMotion={motion}
139-
>
140-
{this.getItems()}
141-
</Collapse>
142-
</div>
143-
);
144-
}
106+
return (
107+
<>
108+
{tools}
109+
<Collapse
110+
accordion={accordion}
111+
onChange={setActiveKey}
112+
activeKey={activeKey}
113+
expandIcon={expandIcon}
114+
openMotion={motion}
115+
>
116+
{panelItems}
117+
</Collapse>
118+
</>
119+
)
145120
}
146121

147-
export default Test;
122+
export default App;

docs/examples/fragment.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Fragment } from 'react';
33
import Collapse, { Panel } from 'rc-collapse';
44
import '../../assets/index.less';
55

6-
const Test = () => (
6+
const App = () => (
77
<Collapse>
88
<Panel header="title">content</Panel>
99
<Panel header="title">content</Panel>
@@ -20,4 +20,4 @@ const Test = () => (
2020
</Collapse>
2121
);
2222

23-
export default Test;
23+
export default App;

0 commit comments

Comments
 (0)