-
Notifications
You must be signed in to change notification settings - Fork 5
/
CardGrid.purs
191 lines (174 loc) · 5.85 KB
/
CardGrid.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
module Lumi.Components.CardGrid
( cardGrid
, styles
) where
import Prelude
import Data.Array as Array
import Data.Foldable (foldMap)
import Data.Maybe (Maybe(..), fromMaybe, isJust)
import Data.Monoid (guard)
import Effect (Effect)
import JSS (JSS, jss)
import Lumi.Components.Card (card, cardMinWidth)
import Lumi.Components.Input (CheckboxState(..), checkbox, input)
import Lumi.Components.Size (Size(..))
import React.Basic (JSX, createComponent, element, keyed, make, readProps)
import React.Basic.DOM as R
import React.Basic.DOM.Events (stopPropagation, targetChecked)
import React.Basic.Events (handler)
import Unsafe.Reference (unsafeRefEq)
import Web.HTML.History (URL)
cardGrid
:: { items :: Array
{ key :: String
, title :: String
, subtitle :: String
, children :: Array JSX
, href :: Maybe URL
}
, onNavigate :: URL -> Effect Unit
, selection :: Maybe
{ selectedKeys :: Array String
, onSelect :: Array String -> Effect Unit
}
}
-> JSX
cardGrid =
make (createComponent "CardGrid")
{ initialState: unit
, shouldUpdate
, render
}
where
shouldUpdate { props } { nextProps } =
not (unsafeRefEq props.items nextProps.items) ||
fromMaybe true ado
{ selectedKeys } <- props.selection
{ selectedKeys: nextSelectedKeys } <- nextProps.selection
in not (unsafeRefEq selectedKeys nextSelectedKeys)
lumiCardGrid = element (R.unsafeCreateDOMComponent "lumi-card-grid")
render self@{ props } =
lumiCardGrid
{ className: "lumi"
, children: props.items <#> \{ key, title, subtitle, children, href } ->
case props.selection of
Nothing ->
keyed key $ cardGridCell
{ title
, subtitle
, children
, href
, onNavigate: props.onNavigate
, selectable: false
, selected: false
, onSelect: mempty
}
Just { selectedKeys, onSelect } ->
keyed key $ cardGridCell
{ title
, subtitle
, children
, href
, onNavigate: props.onNavigate
, selectable: isJust props.selection
, selected: Array.elem key selectedKeys
, onSelect: \selected -> do
props' <- readProps self
fromMaybe mempty do
s <- props'.selection
pure $
if selected
then s.onSelect (Array.snoc s.selectedKeys key)
else s.onSelect (Array.filter (_ /= key) s.selectedKeys)
}
}
cardGridCell
:: { title :: String
, subtitle :: String
, children :: Array JSX
, href :: Maybe URL
, onNavigate :: URL -> Effect Unit
, selectable :: Boolean
, selected :: Boolean
, onSelect :: Boolean -> Effect Unit
}
-> JSX
cardGridCell =
make (createComponent "CardGridCell")
{ initialState: unit
, shouldUpdate
, render
}
where
shouldUpdate { props } { nextProps } =
props.selected /= nextProps.selected
lumiCardGridCell = element (R.unsafeCreateDOMComponent "lumi-card-grid-cell")
lumiCardGridCheckbox = element (R.unsafeCreateDOMComponent "lumi-card-grid-checkbox")
render { props } =
lumiCardGridCell
{ children:
[ card
{ title: props.title
, subtitle: props.subtitle
, children: props.children
, href: props.href
, onNavigate: props.onNavigate
, selected: props.selectable && props.selected
}
, guard (props.selectable) $
lumiCardGridCheckbox
{ onClick: handler stopPropagation mempty
, "data-selected": props.selected
, children:
[ input checkbox
{ style = R.css { margin: "0" }
, size = Medium
, disabled = false
, checked = if props.selected then On else Off
, onChange = handler targetChecked $ foldMap props.onSelect
}
]
}
]
}
styles :: JSS
styles = jss
{ "@global":
{ "lumi-card-grid":
{ display: "grid"
, gridGap: "20px"
, gridTemplateColumns: "repeat(auto-fill, minmax(" <> cardMinWidth <> ", 1fr))"
, "@media (max-width: 448px)":
{ gridGap: "12px"
, gridTemplateColumns: "repeat(auto-fill, minmax(" <> cardMinWidth <> " - 32px, 1fr))"
}
, alignSelf: "stretch"
, "& > lumi-card-grid-cell":
{ display: "flex"
, flexFlow: "column nowrap"
, position: "relative"
, "& > lumi-card":
{ flex: "1"
, "@media (max-width: 448px)":
{ minWidth: cardMinWidth <> " - 32px"
}
}
, "& > lumi-card-grid-checkbox":
{ position: "absolute"
, top: "16px"
, left: "16px"
, visibility: "hidden"
, "@media (max-width: 860px)":
{ visibility: "visible"
}
, "&[data-selected=\"true\"]":
{ visibility: "visible"
}
}
, "&:hover > lumi-card-grid-checkbox":
{ visibility: "visible"
}
}
}
}
}