@defra/lis-infra-ui-services 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-infra-ui-services",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "UI-Services shared definitions for the livestock taxonomy topology.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -225,6 +225,108 @@ test('shows the results range for the current page', () => {
225
225
  assert.match(html, /Showing 6 to 7 of 7 results/)
226
226
  })
227
227
 
228
+ test('renders the summary above the pagination by default', () => {
229
+ const html = render({
230
+ caption: 'Animals',
231
+ columns,
232
+ rows,
233
+ baseHref: '/animals',
234
+ pagination: {
235
+ currentPage: 1,
236
+ totalPages: 2,
237
+ totalItems: 7,
238
+ itemsPerPage: 5
239
+ }
240
+ })
241
+
242
+ assert.ok(
243
+ html.indexOf('Showing 1 to 5 of 7') < html.indexOf('govuk-pagination')
244
+ )
245
+ assert.doesNotMatch(html, /govuk-grid-row/)
246
+ })
247
+
248
+ test('renders the summary text the caller supplies instead of the results count', () => {
249
+ const html = render({
250
+ caption: 'Animals',
251
+ columns,
252
+ rows,
253
+ baseHref: '/animals',
254
+ summary: { text: 'Showing 1 to 7 of 7 <cattle>' },
255
+ pagination: {
256
+ currentPage: 1,
257
+ totalPages: 1,
258
+ totalItems: 7,
259
+ itemsPerPage: 25
260
+ }
261
+ })
262
+
263
+ assert.match(html, /Showing 1 to 7 of 7 &lt;cattle&gt;</)
264
+ assert.doesNotMatch(html, /results/)
265
+ })
266
+
267
+ test('renders summary html the caller supplies unescaped', () => {
268
+ const html = render({
269
+ caption: 'Animals',
270
+ columns,
271
+ rows,
272
+ baseHref: '/animals',
273
+ summary: { html: 'Showing <strong>7</strong> cattle' },
274
+ pagination: {
275
+ currentPage: 1,
276
+ totalPages: 1,
277
+ totalItems: 7,
278
+ itemsPerPage: 25
279
+ }
280
+ })
281
+
282
+ assert.match(html, /Showing <strong>7<\/strong> cattle</)
283
+ })
284
+
285
+ test('puts the summary beside the pagination, below the table, when positioned below', () => {
286
+ const html = render({
287
+ caption: 'Animals',
288
+ columns,
289
+ rows,
290
+ baseHref: '/animals',
291
+ summary: { position: 'below' },
292
+ pagination: {
293
+ currentPage: 1,
294
+ totalPages: 2,
295
+ totalItems: 7,
296
+ itemsPerPage: 5
297
+ }
298
+ })
299
+
300
+ assert.ok(html.indexOf('</table>') < html.indexOf('govuk-pagination'))
301
+ assert.ok(
302
+ html.indexOf('govuk-pagination') < html.indexOf('Showing 1 to 5 of 7')
303
+ )
304
+ assert.match(
305
+ html,
306
+ /govuk-grid-column-one-third govuk-!-text-align-right">\s*<p class="govuk-body govuk-!-margin-top-2">Showing 1 to 5 of 7 results<\/p>/
307
+ )
308
+ })
309
+
310
+ test('puts the summary alone below the table when positioned below with a single page', () => {
311
+ const html = render({
312
+ caption: 'Animals',
313
+ columns,
314
+ rows,
315
+ baseHref: '/animals',
316
+ summary: { position: 'below' },
317
+ pagination: {
318
+ currentPage: 1,
319
+ totalPages: 1,
320
+ totalItems: 7,
321
+ itemsPerPage: 25
322
+ }
323
+ })
324
+
325
+ assert.ok(html.indexOf('</table>') < html.indexOf('Showing 7 results'))
326
+ assert.doesNotMatch(html, /govuk-grid-row/)
327
+ assert.doesNotMatch(html, /govuk-pagination/)
328
+ })
329
+
228
330
  test('renders pagination items and next but no previous on the first page', () => {
229
331
  const html = render({
230
332
  caption: 'Animals',
@@ -66,11 +66,20 @@
66
66
  {%- set lastItemOnPage = currentPage * itemsPerPage -%}
67
67
  {%- set endItem = totalItems if lastItemOnPage > totalItems else lastItemOnPage -%}
68
68
 
69
- {%- if totalPages > 1 -%}
70
- <p class="govuk-body">Showing {{ startItem }} to {{ endItem }} of {{ totalItems }} results</p>
71
- {%- else -%}
72
- <p class="govuk-body">Showing {{ totalItems }} result{{ "s" if totalItems != 1 else "" }}</p>
73
- {%- endif -%}
69
+ {%- set summary = params.summary or {} -%}
70
+
71
+ {#- the caller may supply the summary wording; otherwise it is the results count -#}
72
+ {%- set summaryContent -%}
73
+ {%- if summary.html -%}
74
+ {{ summary.html | safe }}
75
+ {%- elif summary.text -%}
76
+ {{ summary.text }}
77
+ {%- elif totalPages > 1 -%}
78
+ Showing {{ startItem }} to {{ endItem }} of {{ totalItems }} results
79
+ {%- else -%}
80
+ Showing {{ totalItems }} result{{ "s" if totalItems != 1 else "" }}
81
+ {%- endif -%}
82
+ {%- endset -%}
74
83
 
75
84
  {%- if totalPages > 1 -%}
76
85
  {%- set items = [] -%}
@@ -82,10 +91,33 @@
82
91
  {%- set previousHref = params.baseHref ~ "?" ~ (query | assign({ sort: params.sort, direction: params.direction, page: currentPage - 1 }) | queryString) -%}
83
92
  {%- set nextHref = params.baseHref ~ "?" ~ (query | assign({ sort: params.sort, direction: params.direction, page: currentPage + 1 }) | queryString) -%}
84
93
 
85
- {{ govukPagination({
86
- items: items,
87
- previous: { href: previousHref } if currentPage > 1 else undefined,
88
- next: { href: nextHref } if currentPage < totalPages else undefined
89
- }) }}
94
+ {%- set paginationHtml -%}
95
+ {{ govukPagination({
96
+ items: items,
97
+ previous: { href: previousHref } if currentPage > 1 else undefined,
98
+ next: { href: nextHref } if currentPage < totalPages else undefined
99
+ }) }}
100
+ {%- endset -%}
101
+ {%- endif -%}
102
+
103
+ {#- "below" puts the summary under the table, beside the page links when there are any -#}
104
+ {%- if summary.position == "below" -%}
105
+ {%- if totalPages > 1 -%}
106
+ <div class="govuk-grid-row">
107
+ <div class="govuk-grid-column-two-thirds">
108
+ {{ paginationHtml | safe }}
109
+ </div>
110
+ <div class="govuk-grid-column-one-third govuk-!-text-align-right">
111
+ <p class="govuk-body govuk-!-margin-top-2">{{ summaryContent | safe }}</p>
112
+ </div>
113
+ </div>
114
+ {%- else -%}
115
+ <p class="govuk-body">{{ summaryContent | safe }}</p>
116
+ {%- endif -%}
117
+ {%- else -%}
118
+ <p class="govuk-body">{{ summaryContent | safe }}</p>
119
+ {%- if totalPages > 1 -%}
120
+ {{ paginationHtml | safe }}
121
+ {%- endif -%}
90
122
  {%- endif -%}
91
123
  {%- endif -%}