/
EditableTable.purs
208 lines (185 loc) · 6.31 KB
/
EditableTable.purs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
module Lumi.Components.EditableTable where
import Prelude
import Color (cssStringHSLA)
import Data.Array as Array
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NonEmptyArray
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Effect (Effect)
import JSS (JSS, jss)
import Lumi.Components.Button as Button
import Lumi.Components.Color (colors)
import Lumi.Components.Column (column_)
import Lumi.Components.Icon (IconType(..), icon_)
import Lumi.Components.Row as Row
import React.Basic (Component, JSX, createComponent, element, empty, makeStateless)
import React.Basic.DOM as R
import React.Basic.DOM.Events (capture_, preventDefault, stopPropagation)
import React.Basic.Events (handler)
type EditableTableProps row =
{ addLabel :: String
, columns :: Array
{ label :: String
, renderCell :: row -> JSX
}
, maxRows :: Int
, onRowAdd :: Effect Unit
, onRowRemove :: row -> Effect Unit
, readonly :: Boolean
, removeCell :: Maybe (row -> Effect Unit) -> row -> JSX
, rows :: Either (Array row) (NonEmptyArray row)
, rowEq :: row -> row -> Boolean
, summary :: JSX
}
editableTableDefaults :: forall row. Eq row => EditableTableProps row
editableTableDefaults =
{ addLabel: "Add row"
, columns: []
, maxRows: top
, onRowAdd: pure unit
, onRowRemove: \_ -> pure unit
, readonly: false
, removeCell: defaultRemoveCell
, rows: Left []
, rowEq: eq
, summary: empty
}
defaultRemoveCell :: forall row. Maybe (row -> Effect Unit) -> row -> JSX
defaultRemoveCell onRowRemove item =
onRowRemove # Array.foldMap \onRowRemove' ->
R.a
{ children: [ icon_ Bin ]
, className: "lumi"
, onClick: capture_ $ onRowRemove' item
, role: "button"
, style: R.css { fontSize: "20px", lineHeight: "20px", textDecoration: "none" }
}
component :: forall row. Component (EditableTableProps row)
component = createComponent "EditableTableExample"
editableTable :: forall row. EditableTableProps row -> JSX
editableTable = makeStateless component render
where
render props =
container
[ header props.columns
, body case props.rows of
Left rows -> map (row_ (not props.readonly)) rows
Right rows ->
if NonEmptyArray.length rows == 1
then
[ row_ false (NonEmptyArray.head rows)
]
else
map (row_ (not props.readonly)) (NonEmptyArray.toArray rows)
, let
lengthRows = case _ of
Left arr -> Array.length arr
Right arr -> NonEmptyArray.length arr
canAddRows = not props.readonly && lengthRows props.rows < props.maxRows
in
footer
canAddRows
props.onRowAdd
props.addLabel
props.summary
(Array.length props.columns + 1)
]
where
row_ = row props.columns props.onRowRemove props.removeCell
container children =
editableTableElement
{ children: R.table { children, className: "lumi" }
}
header columns =
R.thead_
[ R.tr_ $
(columns <#> \column -> R.th_ [ R.text column.label ])
<> [ R.th_ [ {- removal column -} ] ]
]
body =
R.tbody_
row columns onRowRemove removeCell isRemovable item =
R.tr_ $
(cell item <$> columns)
<> [ R.td_
[ if isRemovable
then removeCell (Just onRowRemove) item
else removeCell Nothing item
]
]
cell item column =
R.td_ [ column_ [ column.renderCell item ] ]
footer canAddRows onRowAdd addLabel summary columnCount =
R.tfoot_
[ R.tr_
[ R.td
{ children:
[ Row.row
{ children:
[ summary
, if not canAddRows
then empty
else Button.iconButton Button.iconButtonDefaults
{ title = addLabel
, onPress =
handler
(preventDefault >>> stopPropagation)
\_ -> onRowAdd
, iconLeft = Just Plus
}
]
, style: R.css
{ justifyContent: "space-between"
, flexFlow: "row-reverse wrap"
}
}
]
, colSpan: columnCount
}
]
]
editableTableElement = element $ R.unsafeCreateDOMComponent "lumi-editable-table"
styles :: JSS
styles = jss
{ "@global":
{ "lumi-editable-table":
{ boxSizing: "border-box"
, display: "flex"
, flexFlow: "column"
, alignSelf: "stretch"
, "& > table.lumi":
{ borderCollapse: "separate"
, borderSpacing: "0"
, width: "100%"
, "& thead > tr > th, & tbody > tr > td":
{ borderBottom: [ "1px", "solid", cssStringHSLA colors.black4 ]
}
, "& th, & td":
{ "&:not(:first-child)": { paddingLeft: "8px" }
, "&:not(:last-child)": { paddingRight: "8px" }
, padding: "16px 0"
}
, "& th":
{ color: cssStringHSLA colors.black1
, fontSize: "13px"
, lineHeight: "16px"
, fontWeight: "400"
, textAlign: "left"
, whiteSpace: "nowrap"
, overflow: "hidden"
, textOverflow: "ellipsis"
}
, "& td > lumi-column":
{ "& > input.lumi": { width: "100%" }
, justifyContent: "center"
, minHeight: "32px"
, fontSize: "14px"
, lineHeight: "20px"
, fontWeight: "400"
}
, "& tfoot": { padding: "16px 0" }
}
}
}
}