/
Pagination.purs
172 lines (154 loc) · 5.24 KB
/
Pagination.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
module Lumi.Components.Pagination where
import Prelude
import Color (cssStringHSLA)
import Data.Array as Array
import Data.Foldable (fold, foldMap)
import Data.Maybe (Maybe(..))
import Data.Monoid (guard)
import Effect (Effect)
import JSS (JSS, jss)
import Lumi.Components.Button as Button
import Lumi.Components.Color (colors)
import React.Basic (JSX, createComponent, element, fragment, keyed, makeStateless)
import React.Basic.DOM as R
import React.Basic.Events (handler_)
type PaginationProps =
{ previousLabel :: String
, nextLabel :: String
, currentPage :: Int
, onChange :: Int -> Effect Unit
, pages :: Int
, focusWindow :: Int
, marginPages :: Int
, details :: Maybe JSX
}
pagination :: PaginationProps -> JSX
pagination = makeStateless (createComponent "Pagination") render
where
lumiPagination = element (R.unsafeCreateDOMComponent "lumi-pagination")
lumiPaginationDetails = element (R.unsafeCreateDOMComponent "lumi-pagination-details")
render props =
let
boundedPage = clamp 0 (props.pages - 1) props.currentPage
rangeMin = max 0 (boundedPage - (props.focusWindow / 2))
rangeMax = min (props.pages - 1) (boundedPage + (props.focusWindow / 2))
prevButton =
keyed "prev" $ R.li_
[ Button.button Button.secondary
{ title = props.previousLabel
, onPress = handler_ $ props.onChange (boundedPage - 1)
}
]
nextButton =
keyed "next" $ R.li_
[ Button.button Button.secondary
{ title = props.nextLabel
, onPress = handler_ $ props.onChange (boundedPage + 1)
}
]
pageButtons start end =
fragment $
Array.range start end <#> \i ->
keyed (show i) $ R.li_
[ Button.button Button.secondary
{ title = show (i + 1)
, onPress = handler_ $ props.onChange i
, buttonState = if i == props.currentPage then Button.Disabled else Button.Enabled
}
]
leftMargin = props.marginPages - 1
rightMargin = props.pages - props.marginPages
separator = R.li_ [ R.text "..." ]
in
lumiPagination
{ className: "lumi"
, role: "navigation"
, "aria-label": "Pagination"
, children:
[ R.ul_ $ fold
-- Previous page button
[ guard (props.currentPage > 0)
[ prevButton
]
-- Left margin
, guard (rangeMin > 0)
[ pageButtons 0 (min (rangeMin - 1) leftMargin)
]
, guard (rangeMin > leftMargin + 1)
[ separator
]
-- Middle pages
, [ pageButtons rangeMin rangeMax
]
-- Right margin
, guard (rangeMax < rightMargin - 1)
[ separator
]
, guard (rangeMax < props.pages - 1)
[ pageButtons (max (rangeMax + 1) rightMargin) (props.pages - 1)
]
-- Next page button
, guard (props.currentPage < props.pages - 1)
[ nextButton
]
]
, props.details # foldMap \details ->
lumiPaginationDetails
{ children: [ details ]
}
]
}
defaults :: PaginationProps
defaults =
{ previousLabel: "Prev"
, nextLabel: "Next"
, currentPage: 0
, onChange: mempty
, pages: 0
, focusWindow: 2
, marginPages: 2
, details: Nothing
}
styles :: JSS
styles = jss
{ "@global":
{ "lumi-pagination":
{ alignSelf: "stretch"
, display: "flex"
, flexFlow: "row wrap"
, alignItems: "center"
, justifyContent: "flex-start"
, padding: "16px 0"
, "& > ul":
{ padding: "0"
, margin: "0 20px 0 0"
, listStyle: "none"
, display: "flex"
, flexFlow: "row nowrap"
, alignItems: "center"
, "& > li":
{ margin: "0 4px"
, "&:first-child": { marginLeft: "0" }
, "&:last-child": { marginRight: "0" }
, "& > button.lumi[data-color=\"secondary\"]":
{ minWidth: "0"
, paddingLeft: "10px"
, paddingRight: "10px"
, fontSize: lumiPaginationFontSize
, color: cssStringHSLA colors.black1
, "&:disabled":
{ color: cssStringHSLA colors.black
, background: cssStringHSLA colors.black3
}
}
}
}
, "& > lumi-pagination-details":
{ color: cssStringHSLA colors.black1
, fontSize: lumiPaginationFontSize
}
}
}
}
where
lumiPaginationFontSize = "12.5px"