1
1
import { useState } from "react" ;
2
- import { GridWrapper , GridSection , GridArea , GridItem , Alignment } from "react-align" ;
2
+ import {
3
+ GridWrapper ,
4
+ GridSection ,
5
+ GridArea ,
6
+ GridItem ,
7
+ Alignment ,
8
+ } from "react-align" ;
3
9
4
- type Item = { id : string , location : string , index : number , extended : boolean ; extendable : boolean ; } ;
10
+ type Item = {
11
+ id : string ;
12
+ location : string ;
13
+ index : number ;
14
+ extended : boolean ;
15
+ extendable : boolean ;
16
+ } ;
5
17
6
18
const initItems : Item [ ] = [
7
19
{ id : "A" , location : "1" , index : 0 , extended : false , extendable : false } ,
@@ -19,23 +31,32 @@ const initAlignments: Record<string, Alignment> = {
19
31
"7" : "start" ,
20
32
"8" : "start" ,
21
33
"9" : "start" ,
22
- }
34
+ } ;
23
35
24
36
const hoz = ( l : string ) => l === "4" || l === "6" ;
25
37
const ver = ( l : string ) => l === "2" || l === "8" ;
26
38
const ItemComponent = ( item : Item , i : number ) => (
27
- < GridItem id = { item . id } index = { i } key = { item . id } extendable = { item . extendable } extended = { item . extended } >
39
+ < GridItem
40
+ id = { item . id }
41
+ index = { i }
42
+ key = { item . id }
43
+ extendable = { item . extendable }
44
+ extended = { item . extended }
45
+ >
28
46
< div
29
47
style = { {
30
48
fontSize : "24px" ,
31
49
width : item . extended && hoz ( item . location ) ? "100%" : "200px" ,
32
50
height : item . extended && ver ( item . location ) ? "100%" : "50px" ,
33
51
background : "#ddd" ,
34
52
padding : "10px" ,
35
- boxSizing : "border-box"
36
- } } > { item . id } </ div >
53
+ boxSizing : "border-box" ,
54
+ } }
55
+ >
56
+ { item . id }
57
+ </ div >
37
58
</ GridItem >
38
- )
59
+ ) ;
39
60
40
61
function App ( ) {
41
62
const [ edit , setEdit ] = useState ( false ) ;
@@ -46,13 +67,16 @@ function App() {
46
67
< GridWrapper
47
68
editing = { edit }
48
69
onMove = { ( ...args ) => {
49
- console . log ( ' move' , ...args ) ;
50
- setItems ( items => {
51
- const target = items . find ( i => i . id === args [ 0 ] ) ;
70
+ console . log ( " move" , ...args ) ;
71
+ setItems ( ( items ) => {
72
+ const target = items . find ( ( i ) => i . id === args [ 0 ] ) ;
52
73
if ( ! target ) return items ;
53
- const sibilings = items . filter ( i => i . location === args [ 1 ] ) ;
74
+ const sibilings = items . filter ( ( i ) => i . location === args [ 1 ] ) ;
54
75
if ( args [ 1 ] === args [ 3 ] ) {
55
- sibilings . splice ( sibilings . findIndex ( i => i . id === args [ 0 ] ) , 1 ) ;
76
+ sibilings . splice (
77
+ sibilings . findIndex ( ( i ) => i . id === args [ 0 ] ) ,
78
+ 1
79
+ ) ;
56
80
}
57
81
sibilings . splice ( args [ 2 ] , 0 , target ) ;
58
82
target . location = args [ 1 ] ;
@@ -63,23 +87,24 @@ function App() {
63
87
} ) ;
64
88
} }
65
89
onAlignChange = { ( ...args ) => {
66
- console . log ( ' alignmentChange' , ...args ) ;
67
- setAlignments ( a => ( {
90
+ console . log ( " alignmentChange" , ...args ) ;
91
+ setAlignments ( ( a ) => ( {
68
92
...a ,
69
- [ args [ 0 ] ] : args [ 1 ]
93
+ [ args [ 0 ] ] : args [ 1 ] ,
70
94
} ) ) ;
71
95
} }
72
96
onExtend = { ( ...args ) => {
73
- console . log ( ' extend' , ...args ) ;
74
- setItems ( items => {
75
- const target = items . find ( i => i . id === args [ 0 ] ) ;
97
+ console . log ( " extend" , ...args ) ;
98
+ setItems ( ( items ) => {
99
+ const target = items . find ( ( i ) => i . id === args [ 0 ] ) ;
76
100
if ( ! target ) return items ;
77
101
target . extended = args [ 1 ] ;
78
102
return [ ...items ] ;
79
103
} ) ;
80
- } } >
104
+ } }
105
+ >
81
106
< GridSection >
82
- { [ "1" , "2" , "3" ] . map ( l => (
107
+ { [ "1" , "2" , "3" ] . map ( ( l ) => (
83
108
< GridArea
84
109
id = { l }
85
110
key = { l }
@@ -88,32 +113,42 @@ function App() {
88
113
vertical = { l === "2" }
89
114
stretch = { l === "2" }
90
115
editorStyle = { { background : l === "2" ? "blue" : "red" } }
91
- iconColor = { l === "2" ? "red" : "blue" } >
92
- { items . filter ( i => i . location === l ) . sort ( ( a , b ) => a . index - b . index ) . map ( ItemComponent ) }
116
+ iconColor = { l === "2" ? "red" : "blue" }
117
+ >
118
+ { items
119
+ . filter ( ( i ) => i . location === l )
120
+ . sort ( ( a , b ) => a . index - b . index )
121
+ . map ( ItemComponent ) }
93
122
</ GridArea >
94
123
) ) }
95
124
</ GridSection >
96
125
< GridSection stretch >
97
- { [ "4" , "5" , "6" ] . map ( l => l == "5" ? (
98
- < div
99
- key = { l }
100
- style = { { flex : "auto" } } >
101
- < button onClick = { ( ) => setEdit ( e => ! e ) } > { edit ? "Finish" : "Start" } editing</ button >
102
- </ div >
103
- ) : (
104
- < GridArea
105
- id = { l }
106
- key = { l }
107
- align = { alignments [ l ] }
108
- end = { l === "6" }
109
- editorStyle = { { background : "red" } }
110
- iconColor = { "blue" } >
111
- { items . filter ( i => i . location === l ) . sort ( ( a , b ) => a . index - b . index ) . map ( ItemComponent ) }
112
- </ GridArea >
113
- ) ) }
126
+ { [ "4" , "5" , "6" ] . map ( ( l ) =>
127
+ l == "5" ? (
128
+ < div key = { l } style = { { flex : "auto" } } >
129
+ < button onClick = { ( ) => setEdit ( ( e ) => ! e ) } >
130
+ { edit ? "Finish" : "Start" } editing
131
+ </ button >
132
+ </ div >
133
+ ) : (
134
+ < GridArea
135
+ id = { l }
136
+ key = { l }
137
+ align = { alignments [ l ] }
138
+ end = { l === "6" }
139
+ editorStyle = { { background : "red" } }
140
+ iconColor = { "blue" }
141
+ >
142
+ { items
143
+ . filter ( ( i ) => i . location === l )
144
+ . sort ( ( a , b ) => a . index - b . index )
145
+ . map ( ItemComponent ) }
146
+ </ GridArea >
147
+ )
148
+ ) }
114
149
</ GridSection >
115
150
< GridSection >
116
- { [ "7" , "8" , "9" ] . map ( l => (
151
+ { [ "7" , "8" , "9" ] . map ( ( l ) => (
117
152
< GridArea
118
153
id = { l }
119
154
key = { l }
@@ -122,13 +157,17 @@ function App() {
122
157
vertical = { l === "8" }
123
158
stretch = { l === "8" }
124
159
editorStyle = { { background : l === "8" ? "blue" : "red" } }
125
- iconColor = { l === "8" ? "red" : "blue" } >
126
- { items . filter ( i => i . location === l ) . sort ( ( a , b ) => a . index - b . index ) . map ( ItemComponent ) }
160
+ iconColor = { l === "8" ? "red" : "blue" }
161
+ >
162
+ { items
163
+ . filter ( ( i ) => i . location === l )
164
+ . sort ( ( a , b ) => a . index - b . index )
165
+ . map ( ItemComponent ) }
127
166
</ GridArea >
128
167
) ) }
129
168
</ GridSection >
130
169
</ GridWrapper >
131
- )
170
+ ) ;
132
171
}
133
172
134
- export default App
173
+ export default App ;
0 commit comments