This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.tsx
191 lines (182 loc) · 5.23 KB
/
code.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const { widget } = figma
const { AutoLayout, Input, Frame, useSyncedState, usePropertyMenu } = widget
type Columns = {
width: number[]
names: string[]
}
function SimpleTable() {
const defaultWidth = [320, 320, 320, 320]
const [postName, setPostName] = useSyncedState('postName', '')
const [columns, setColumns] = useSyncedState<Columns>('colums', {
width: defaultWidth,
names: ['', '', '', '']
})
const tableWidth = 320 * columns.names.length
const [data, setData] = useSyncedState<string[][]>('data', [
['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
['', '', '', '']
])
const borderColor = '#696969'
const headerColor = '#e0ffff'
usePropertyMenu(
[
{
itemType: 'action',
tooltip: 'Add Row',
propertyName: 'addRow'
},
{
itemType: 'action',
tooltip: 'Delete Row',
propertyName: 'deleteLastRow'
},
{
itemType: 'action',
tooltip: 'Add Column',
propertyName: 'addColumn'
},
{
itemType: 'action',
tooltip: 'Delete Column',
propertyName: 'deleteLastColumn'
}
],
({ propertyName }) => {
if (propertyName === 'addRow') {
const emptyRow = ['', '', '', '']
setData((current) => [...current, emptyRow])
} else if (propertyName === 'deleteLastRow') {
setData((current) => {
const newData = [...current]
newData.pop()
return newData
})
} else if (propertyName === 'addColumn') {
setColumns((current) => {
const { names, ...rest } = current
return { names: [...names, ''], ...rest }
})
setData((current) => {
return current.map((row) => [...row, ''])
})
} else if (propertyName === 'deleteLastColumn') {
setColumns((current) => {
const { names, ...rest } = current
if (names.length > 1) names.pop()
return { names: [...names], ...rest }
})
setData((current) => {
if (current.length > 1 && current[0].length > 1) {
return current.map((row) => {
if (row.length > 1) row.pop()
return row
})
} else {
return current
}
})
}
}
)
return (
<AutoLayout
width={tableWidth}
direction="vertical"
verticalAlignItems="center"
>
<AutoLayout width="fill-parent" padding={16}>
<Input
placeholder="table title"
value={postName}
width="fill-parent"
fontSize={32}
onTextEditEnd={(event) => setPostName(event.characters)}
/>
</AutoLayout>
<AutoLayout
width="fill-parent"
direction="horizontal"
verticalAlignItems="center"
fill="#fff"
stroke={borderColor}
>
{columns.names.map((name, i) => {
return (
<>
<AutoLayout width={320} height="fill-parent" padding={16}>
<Input
width={320 - 16 * 2 - 1}
value={name}
fontSize={24}
onTextEditEnd={(e) => {
const { names, ...rest } = columns
const newNames = [...names]
newNames[i] = e.characters
setColumns({ names: newNames, ...rest })
}}
/>
</AutoLayout>
<Frame
height="fill-parent"
width={1}
name="Border2"
fill={borderColor}
/>
</>
)
})}
</AutoLayout>
<AutoLayout direction="vertical" width="fill-parent" stroke={borderColor}>
{data.map((row, i) => {
return (
<>
<Frame
height={1}
width="fill-parent"
name="Border"
fill={borderColor}
/>
<AutoLayout
direction="horizontal"
fill="#fff"
width="fill-parent"
>
{row.map((cell, j) => {
return (
<>
<AutoLayout
width={320}
fill={j > 0 ? '#fff' : headerColor}
padding={16}
>
<Input
width={320 - 16 * 2 - 1}
value={cell}
fontSize={24}
onTextEditEnd={(e) => {
const newData = [...data]
newData[i][j] = e.characters
setData(newData)
}}
/>
</AutoLayout>
<Frame
height="fill-parent"
width={1}
name="Border2"
fill={borderColor}
/>
</>
)
})}
</AutoLayout>
</>
)
})}
</AutoLayout>
</AutoLayout>
)
}
widget.register(SimpleTable)