@@ -3,6 +3,8 @@ import Collapse, { Panel } from 'rc-collapse';
3
3
import motion from './_util/motionUtil' ;
4
4
import '../../assets/index.less' ;
5
5
6
+ const initLength = 3 ;
7
+
6
8
const text = `
7
9
A dog is a type of domesticated animal.
8
10
Known for its loyalty and faithfulness,
@@ -33,52 +35,42 @@ function expandIcon({ isActive }) {
33
35
transform : `rotate(${ isActive ? 90 : 0 } deg)` ,
34
36
} }
35
37
>
36
- < path d = { arrowPath } p-id = "5827" />
38
+ < path d = { arrowPath } />
37
39
</ svg >
38
40
</ i >
39
41
) ;
40
42
}
41
43
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' ] ) ;
48
48
49
- onChange = ( activeKey : string ) => {
50
- this . setState ( {
51
- activeKey,
52
- } ) ;
53
- } ;
49
+ const time = random ( ) ;
54
50
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 } >
72
67
< Collapse defaultActiveKey = "1" expandIcon = { expandIcon } >
73
68
< Panel header = "This is panel nest panel" key = "1" id = "header-test" >
74
69
< p > { text } </ p >
75
70
</ Panel >
76
71
</ Collapse >
77
72
</ 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 } >
82
74
< Collapse defaultActiveKey = "1" >
83
75
< Panel header = "This is panel nest panel" key = "1" id = "another-test" >
84
76
< form >
@@ -87,61 +79,44 @@ class Test extends React.Component {
87
79
</ form >
88
80
</ Panel >
89
81
</ Collapse >
90
- </ Panel > ,
82
+ </ Panel >
91
83
) ;
92
84
93
- return items ;
94
- }
95
-
96
- setActivityKey = ( ) => {
97
- this . setState ( {
98
- activeKey : [ '2' ] ,
99
- } ) ;
100
- } ;
101
85
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
+ )
108
105
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
+ )
145
120
}
146
121
147
- export default Test ;
122
+ export default App ;
0 commit comments