Skip to content

Commit 390aaa4

Browse files
committed
TRP-787: Add ability to not move GB to top of dropdownlist
1 parent 254d2c7 commit 390aaa4

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

API_DOCS.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,13 @@ Returns **([string][70] | [undefined][75])** The slug version of the country's n
352352
## sortCountryList
353353

354354
Sorts a list of country objects by their `displayName` property.
355-
Moves the country with `countryCode` 'GB' (United Kingdom) to the top of the list if present.
355+
Conditional to move the country with `countryCode` 'GB' (United Kingdom) to the top of the list if present.
356+
The GB move to top is optional as there are some instances where this is not required.
356357

357358
### Parameters
358359

359-
* `list` **[Array][74]<[Object][69]>** The array of country objects to sort.
360-
Each object should have at least `displayName` and `countryCode` properties.
360+
* `list` **[boolean][72]** Whether the GB country entry should move to top. Each object should have at least `displayName` and `countryCode` properties.
361+
* `moveGbToTop` (optional, default `true`)
361362

362363
### Examples
363364

@@ -389,6 +390,7 @@ Optionally uses Welsh display names based on the `isWelsh` flag.
389390
* `list` **[Array][74]<[Object][69]>** The array of country objects to transform.
390391
Each object should have `countryCode`, `displayName`, and `displayNameWelsh` properties.
391392
* `isWelsh` **[boolean][72]** Whether to use the Welsh display name (`displayNameWelsh`) instead of the default (`displayName`).
393+
* `moveGbToTop` (optional, default `true`)
392394

393395
### Examples
394396

@@ -419,6 +421,7 @@ A wrapper function around `dropdownList(list, isWelsh)`.
419421
### Parameters
420422

421423
* `isWelsh` **[boolean][72]** Whether to use the Welsh display name (`displayNameWelsh`) instead of the default (`displayName`).
424+
* `moveGbToTop` (optional, default `true`)
422425

423426
Returns **[Array][74]<[Object][69]>** An array of objects formatted for use in dropdowns,
424427
each containing `value`, `text`, and `label` properties.

hmpo-countries-lib-7.1.2.tgz

15.8 KB
Binary file not shown.

lib/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ class CountriesCachedModel {
409409
* // { value: 'FR', text: 'France', label: 'France' }
410410
* // ]
411411
*/
412-
dropdownList(list, isWelsh) {
413-
list = this.sortCountryList(list);
412+
dropdownList(list, isWelsh, moveGbToTop = true) {
413+
list = this.sortCountryList(list, moveGbToTop);
414414
return list.map(i => ({
415415
value: i.countryCode,
416416
text: isWelsh ? i.displayNameWelsh : i.displayName,
@@ -428,8 +428,8 @@ class CountriesCachedModel {
428428
* @returns {Array<Object>} - An array of objects formatted for use in dropdowns,
429429
* each containing `value`, `text`, and `label` properties.
430430
*/
431-
dropdownListBirthCountries(isWelsh) {
432-
return this.dropdownList(this.getBirthCountries(), isWelsh);
431+
dropdownListBirthCountries(isWelsh, moveGbToTop = true) {
432+
return this.dropdownList(this.getBirthCountries(moveGbToTop), isWelsh, moveGbToTop);
433433
}
434434

435435
/**

test/lib/spec.index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,17 @@ describe('CountriesCachedModel', () => {
564564
});
565565

566566
describe('dropdownList', () => {
567+
it('turns a list of countries into sorted drop down select box options', () => {
568+
const items = instance.dropdownList(countries, false, false);
569+
570+
items.should.deep.equal([
571+
{ value: 'BA', text: 'Bar', label: 'Bar' },
572+
{ value: 'AA', text: 'Foo', label: 'Foo' },
573+
{ value: 'NA', text: 'Narnia', label: 'Narnia' },
574+
{ value: 'GB', text: 'United Kingdom', label: 'United Kingdom' },
575+
]);
576+
});
577+
567578
it('turns a list of countries into sorted drop down select box options with GB at the top', () => {
568579
const items = instance.dropdownList(countries, false);
569580

@@ -594,6 +605,12 @@ describe('CountriesCachedModel', () => {
594605
items.should.deep.equal([unitedKingdom, bar, foo, narnia]);
595606
});
596607

608+
it('turns a list of countries into sorted list with GB at the top by default when flag not provided', () => {
609+
const items = instance.sortCountryList(countries);
610+
611+
items.should.deep.equal([unitedKingdom, bar, foo, narnia]);
612+
});
613+
597614
it('turns a list of countries into sorted list', () => {
598615
const items = instance.sortCountryList(countries, false);
599616

0 commit comments

Comments
 (0)