Skip to content

Commit

Permalink
Merge pull request #265 from chalettu/dialog-sorting-dropdowns
Browse files Browse the repository at this point in the history
Updated support for sorting of drop downs

(cherry picked from commit fa8af25)
  • Loading branch information
himdel committed Feb 21, 2018
1 parent 9f6d974 commit b4dbb34
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/dialog-user/components/dialog-user/dialogField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ describe('Dialog field test', () => {

describe('#convertValuesToArray', () => {
it('converts a string of default values to an array', () => {
dialogCtrl.dialogField.default_value = '["one", "two"]'
dialogCtrl.dialogField.default_value = '["one", "two"]';
dialogCtrl.convertValuesToArray();
expect(dialogCtrl.dialogField.default_value).toEqual(['one', 'two'])
expect(dialogCtrl.dialogField.default_value).toEqual(['one', 'two']);
});
});
});
Expand Down
15 changes: 14 additions & 1 deletion src/dialog-user/services/dialogData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('DialogDataService test', () => {
[5, 'Test2'],
[2, 'Test5']
],
options: { sort_by: 'value', sort_order: 'descending' }
options: { sort_by: 'value', sort_order: 'descending', data_type: 'integer' }
};
const testSorted = dialogData.updateFieldSortOrder(testDropDown);
const expectedResult = [[5, 'Test2'], [2, 'Test5'], [0, 'Test']];
Expand All @@ -144,4 +144,17 @@ describe('DialogDataService test', () => {
const expectedSortedResult = [[5, 'C'], [0, 'B'], [2, 'A']];
expect(testSortedDescription).toEqual(expectedSortedResult);
});
it('should allow a numeric Description field to be sorted in a dropdown', () => {
const testDropDownDescription = {
values: [
['zero', '0'],
['five', '5'],
['two', '2']
],
options: { sort_by: 'description', sort_order: 'descending' }
};
const testSortedDescription = dialogData.updateFieldSortOrder(testDropDownDescription);
const expectedSortedResult = [['five', '5'], ['two', '2'], ['zero', '0']];
expect(testSortedDescription).toEqual(expectedSortedResult);
});
});
29 changes: 22 additions & 7 deletions src/dialog-user/services/dialogData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ export default class DialogDataService {
if (option[0] === String(field.default_value)) {
field.selected = option;
}
if (field.data_type === 'integer') {
dropDownValues.push([parseInt(option[0], 10), option[1]]);
} else {
dropDownValues.push(option);
}
const value = (field.data_type === 'integer' ? parseInt(option[0], 10) : option[0]);
const description = (isNaN(option[1]) ? option[1] : parseInt(option[1], 10));
dropDownValues.push([value, description]);
}
field.values = dropDownValues;
field.values = this.updateFieldSortOrder(field);
Expand All @@ -48,8 +46,25 @@ export default class DialogDataService {
*
**/
private updateFieldSortOrder(data) {
let values = _.sortBy(data.values, data.options.sort_by === 'value' ? 0 : 1);
return data.options.sort_order === 'ascending' ? values : values.reverse();
const SORT_DESCRIPTION = 1;
const SORT_VALUE = 0;
const FIRST_OPTION = 0;
const VALUE = 0;
const sortBy = (data.options.sort_by === 'value' ? SORT_VALUE : SORT_DESCRIPTION);
let tempValues = [...data.values];
let defaultDropdownField = [];
// The following if deals with a empty default option if it exists
if (data.data_type === 'integer' && _.isNaN(tempValues[FIRST_OPTION][VALUE]) ||
_.isNull(tempValues[FIRST_OPTION][VALUE])) {
defaultDropdownField = tempValues.shift();
}
let values = _.sortBy(tempValues, sortBy);
const sortedValues = data.options.sort_order === 'ascending' ? values : values.reverse();
if (defaultDropdownField.length) {
sortedValues.unshift(defaultDropdownField);
}

return sortedValues;
}

/**
Expand Down

0 comments on commit b4dbb34

Please sign in to comment.