-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTable.elm
218 lines (184 loc) · 5.65 KB
/
Table.elm
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
209
210
211
212
213
214
215
216
217
218
{--
SPDX-License-Identifier: Apache-2.0
--}
module Components.Table exposing
( Column
, Columns
, Config
, Row
, Rows
, view
, viewIconCell
, viewItemCell
, viewListCell
, viewListItemCell
)
import Html
exposing
( Html
, caption
, div
, span
, table
, tbody
, td
, text
, tfoot
, th
, thead
, tr
)
import Html.Attributes
exposing
( attribute
, class
, classList
, scope
)
import String.Extra
import Utils.Helpers as Util
-- TYPES
{-| Column : string alias for table column headers.
-}
type alias Column =
( Maybe String, String )
{-| Columns : list of columns.
-}
type alias Columns =
List Column
{-| Rows : object containing render data and msg.
-}
type alias Row data msg =
{ data : data
, display : data -> Html msg
}
{-| Rows : list of rows with render data and msg.
-}
type alias Rows data msg =
List (Row data msg)
{-| Config : configurations for rendering the data table.
-}
type alias Config data msg =
{ label : String
, testLabel : String
, noRows : Html msg
, columns : Columns
, rows : Rows data msg
, headerElement : Maybe (Html msg)
}
-- VIEW
{-| view : renders data table.
-}
view : Config data msg -> Html msg
view { label, testLabel, noRows, columns, rows, headerElement } =
let
numRows =
List.length rows
numColumns =
List.length columns
in
table [ class "table-base", Util.testAttribute <| testLabel ++ "-table" ]
[ caption []
[ div []
[ text label
, Maybe.withDefault (text "") headerElement
]
]
, thead []
[ tr [] <|
List.map
(\( className, col ) ->
th
[ class <| Maybe.withDefault "" className
, scope "col"
]
[ text <| String.Extra.toTitleCase col ]
)
columns
]
, viewFooter noRows numRows numColumns
, tbody [] <| List.map (\row_ -> row_.display row_.data) rows
]
{-| viewFooter : renders data table footer.
-}
viewFooter : Html msg -> Int -> Int -> Html msg
viewFooter noRows numRows numColumns =
if numRows == 0 then
tfoot [ class "no-rows" ] [ tr [] [ td [ attribute "colspan" <| String.fromInt numColumns ] [ noRows ] ] ]
else
text ""
{-| viewListCell : takes list of items, text for none and className and renders a table cell.
-}
viewListCell : { dataLabel : String, items : List String, none : String, itemWrapperClassList : List ( String, Bool ) } -> Html msg
viewListCell { dataLabel, items, none, itemWrapperClassList } =
if List.length items == 0 then
span
[ class "single-item"
, Util.testAttribute <| "cell-list-item-" ++ dataLabel
]
[ text none ]
else
items
|> List.sort
|> List.map
(\item ->
div
[ classList itemWrapperClassList
, Util.testAttribute <| "cell-list-item-" ++ dataLabel
]
[ span
[ class "list-item" ]
[ text item ]
]
)
|> div []
{-| viewListItemCell : takes classlist and children elements and renders a list item cell element.
-}
viewListItemCell : { dataLabel : String, parentClassList : List ( String, Bool ), itemWrapperClassList : List ( String, Bool ), itemClassList : List ( String, Bool ), children : List (Html msg) } -> Html msg
viewListItemCell { dataLabel, parentClassList, itemWrapperClassList, itemClassList, children } =
td
[ attribute "data-label" dataLabel
, class "break-word"
, classList parentClassList
, Util.testAttribute <| "cell-" ++ dataLabel
]
[ div [ classList itemWrapperClassList ]
[ span
[ class "list-item"
, classList itemClassList
]
children
]
]
{-| viewItemCell : takes classlist and children elements and renders a cell element.
-}
viewItemCell : { dataLabel : String, parentClassList : List ( String, Bool ), itemClassList : List ( String, Bool ), children : List (Html msg) } -> Html msg
viewItemCell { dataLabel, parentClassList, itemClassList, children } =
td
[ attribute "data-label" dataLabel
, class "break-word"
, classList parentClassList
, Util.testAttribute <| "cell-" ++ dataLabel
]
[ span
[ class "single-item"
, classList itemClassList
]
children
]
{-| viewIconCell : takes classlist and children elements and renders a cell icon element.
-}
viewIconCell : { dataLabel : String, parentClassList : List ( String, Bool ), itemWrapperClassList : List ( String, Bool ), itemClassList : List ( String, Bool ), children : List (Html msg) } -> Html msg
viewIconCell { dataLabel, parentClassList, itemWrapperClassList, itemClassList, children } =
td
[ attribute "data-label" dataLabel
, class "break-word"
, class "table-icon"
, classList parentClassList
, Util.testAttribute <| "cell-" ++ dataLabel
]
[ div
[ classList itemWrapperClassList
]
[ div [ classList itemClassList ] children ]
]