11import React from 'react' ;
2- import toArray , { type Option } from 'rc-util/es/Children/toArray' ;
2+ import toArray , { type Option } from '@ rc-component/ util/es/Children/toArray' ;
33
44const initialState = {
55 'no-children' : false ,
@@ -8,45 +8,56 @@ const initialState = {
88 'nested-children' : false ,
99 'array-of-children' : false ,
1010 'is-falsy' : false ,
11- ' fragment' : false ,
12- } as const
11+ fragment : false ,
12+ } as const ;
1313
14- type Action = keyof typeof initialState
14+ type Action = keyof typeof initialState ;
1515
16- const Wrapper = ( { children, ...options } : { children ?: React . ReactNode } & Option ) => {
16+ const Wrapper = ( {
17+ children,
18+ ...options
19+ } : { children ?: React . ReactNode } & Option ) => {
1720 window . console . log ( toArray ( children , options ) ) ;
1821
1922 return children as any ;
2023} ;
2124
22- const DemoBox = React . memo ( ( { children, name } : { children ?: any , name : Action } ) => {
23- const [ keepEmpty , setkeepEmpty ] = React . useState ( false ) ;
24- const [ show , setShow ] = React . useState ( false )
25- return (
26- < React . Fragment key = { name } >
27- < label style = { { display : 'inline-block' , width : 180 } } >
28- < input type = "checkbox" checked = { show } onChange = { ( ) => setShow ( prev => ! prev ) } />
29- { name }
30- </ label >
31- < label >
32- < input
33- type = "checkbox"
34- checked = { keepEmpty }
35- onChange = { ( ) => setkeepEmpty ( prev => ! prev ) }
36- disabled = { ! show }
37- />
38- keepEmpty={ keepEmpty ? 'true' : 'false' }
39- </ label >
40- < br />
41- { show && React . cloneElement < any > ( children , { keepEmpty } ) }
42- < br />
43- </ React . Fragment >
44- )
45- } )
25+ const DemoBox = React . memo (
26+ ( { children, name } : { children ?: any ; name : Action } ) => {
27+ const [ keepEmpty , setkeepEmpty ] = React . useState ( false ) ;
28+ const [ show , setShow ] = React . useState ( false ) ;
29+ return (
30+ < React . Fragment key = { name } >
31+ < label style = { { display : 'inline-block' , width : 180 } } >
32+ < input
33+ type = "checkbox"
34+ checked = { show }
35+ onChange = { ( ) => setShow ( prev => ! prev ) }
36+ />
37+ { name }
38+ </ label >
39+ < label >
40+ < input
41+ type = "checkbox"
42+ checked = { keepEmpty }
43+ onChange = { ( ) => setkeepEmpty ( prev => ! prev ) }
44+ disabled = { ! show }
45+ />
46+ keepEmpty={ keepEmpty ? 'true' : 'false' }
47+ </ label >
48+ < br />
49+ { show && React . cloneElement < any > ( children , { keepEmpty } ) }
50+ < br />
51+ </ React . Fragment >
52+ ) ;
53+ } ,
54+ ) ;
4655
4756const App = ( ) => (
4857 < >
49- < p onClick = { window . console . clear } > Press F12 to open the console (Click Clear Console)</ p >
58+ < p onClick = { window . console . clear } >
59+ Press F12 to open the console (Click Clear Console)
60+ </ p >
5061
5162 < DemoBox name = "no-children" >
5263 < Wrapper />
@@ -59,42 +70,42 @@ const App = () => (
5970 </ DemoBox >
6071
6172 < DemoBox name = "multiple-children" >
62- < Wrapper >
73+ < Wrapper >
6374 < p > 1</ p >
6475 < span > 2</ span >
6576 </ Wrapper >
6677 </ DemoBox >
6778
6879 < DemoBox name = "nested-children" >
69- < Wrapper >
80+ < Wrapper >
7081 < ul >
71- { Array . from ( { length : 5 } , ( _ , i ) => < li key = { i } > { i } </ li > ) }
82+ { Array . from ( { length : 5 } , ( _ , i ) => (
83+ < li key = { i } > { i } </ li >
84+ ) ) }
7285 </ ul >
7386 </ Wrapper >
7487 </ DemoBox >
7588
7689 < DemoBox name = "array-of-children" >
77- < Wrapper >
78- {
90+ < Wrapper >
91+ { [
7992 [
8093 [
81- [
82- < span key = "1" > 111</ span > ,
83- < span key = "2" > 222</ span > ,
84- < span key = "3" > 333</ span >
85- ] ,
86- < span key = "4" > 444</ span > ,
87- < span key = "5" > 555</ span >
94+ < span key = "1" > 111</ span > ,
95+ < span key = "2" > 222</ span > ,
96+ < span key = "3" > 333</ span > ,
8897 ] ,
89- < span key = "6" > 666</ span > ,
90- < span key = "7" > 777</ span >
91- ]
92- }
98+ < span key = "4" > 444</ span > ,
99+ < span key = "5" > 555</ span > ,
100+ ] ,
101+ < span key = "6" > 666</ span > ,
102+ < span key = "7" > 777</ span > ,
103+ ] }
93104 </ Wrapper >
94105 </ DemoBox >
95106
96107 < DemoBox name = "is-falsy" >
97- < Wrapper >
108+ < Wrapper >
98109 { null }
99110 { undefined }
100111 { false }
@@ -106,18 +117,16 @@ const App = () => (
106117 </ DemoBox >
107118
108119 < DemoBox name = "fragment" >
109- < Wrapper >
120+ < Wrapper >
110121 < React . Fragment />
111- < React . Fragment > A</ React . Fragment >
112- < React . Fragment >
122+ < React . Fragment > A</ React . Fragment >
123+ < React . Fragment >
113124 < React . Fragment />
114- < React . Fragment > B</ React . Fragment >
125+ < React . Fragment > B</ React . Fragment >
115126 </ React . Fragment >
116127 </ Wrapper >
117128 </ DemoBox >
118129 </ >
119130) ;
120131
121-
122-
123- export default App ;
132+ export default App ;
0 commit comments