Skip to content

Commit 6da15ec

Browse files
committed
Fix for PTNFLY-539 "AngJS Simple Filter - Attr. Selector should be single select#"
1 parent 64cb229 commit 6da15ec

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

dist/angular-patternfly.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ angular.module('patternfly.charts').directive('pfUtilizationChart', function ()
19991999
* <li>.id - (String) Optional unique Id for the filter field, useful for comparisons
20002000
* <li>.title - (String) The title to display for the filter field
20012001
* <li>.placeholder - (String) Text to display when no filter value has been entered
2002-
* <li>.filterType - (String) The filter input field type (any html input type, or 'select' for a select box)
2002+
* <li>.filterType - (String) The filter input field type (any html input type, or 'select' for a single select box)
20032003
* <li>.filterValues - (Array) List of valid select values used when filterType is 'select'
20042004
* </ul>
20052005
* <li>.appliedFilters - (Array) List of the currently applied filters
@@ -2169,13 +2169,23 @@ angular.module('patternfly.filters').directive('pfSimpleFilter', function () {
21692169
return foundFilter !== undefined;
21702170
};
21712171

2172+
$scope.enforceSingleSelect = function (filter) {
2173+
_.remove($scope.config.appliedFilters, {title: filter.title});
2174+
};
2175+
21722176
$scope.addFilter = function (field, value) {
21732177
var newFilter = {
21742178
id: field.id,
21752179
title: field.title,
2180+
type: field.filterType,
21762181
value: value
21772182
};
21782183
if (!$scope.filterExists(newFilter)) {
2184+
2185+
if (newFilter.type === 'select') {
2186+
$scope.enforceSingleSelect(newFilter);
2187+
}
2188+
21792189
$scope.config.appliedFilters.push(newFilter);
21802190

21812191
if ($scope.config.onFilterChange) {

dist/angular-patternfly.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/filters/simple-filter-directive.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* <li>.id - (String) Optional unique Id for the filter field, useful for comparisons
1414
* <li>.title - (String) The title to display for the filter field
1515
* <li>.placeholder - (String) Text to display when no filter value has been entered
16-
* <li>.filterType - (String) The filter input field type (any html input type, or 'select' for a select box)
16+
* <li>.filterType - (String) The filter input field type (any html input type, or 'select' for a single select box)
1717
* <li>.filterValues - (Array) List of valid select values used when filterType is 'select'
1818
* </ul>
1919
* <li>.appliedFilters - (Array) List of the currently applied filters
@@ -183,13 +183,23 @@ angular.module('patternfly.filters').directive('pfSimpleFilter', function () {
183183
return foundFilter !== undefined;
184184
};
185185

186+
$scope.enforceSingleSelect = function (filter) {
187+
_.remove($scope.config.appliedFilters, {title: filter.title});
188+
};
189+
186190
$scope.addFilter = function (field, value) {
187191
var newFilter = {
188192
id: field.id,
189193
title: field.title,
194+
type: field.filterType,
190195
value: value
191196
};
192197
if (!$scope.filterExists(newFilter)) {
198+
199+
if (newFilter.type === 'select') {
200+
$scope.enforceSingleSelect(newFilter);
201+
}
202+
193203
$scope.config.appliedFilters.push(newFilter);
194204

195205
if ($scope.config.onFilterChange) {

test/filters/simple-filter.spec.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ describe('Directive: pfSimpleFilter', function () {
22
var $scope;
33
var $compile;
44
var element;
5+
var isoScope;
56

67
// load the controller's module
78
beforeEach(function () {
@@ -15,9 +16,11 @@ describe('Directive: pfSimpleFilter', function () {
1516

1617
var compileHTML = function (markup, scope) {
1718
element = angular.element(markup);
18-
$compile(element)(scope);
19+
var el = $compile(element)(scope);
1920

2021
scope.$digest();
22+
23+
isoScope = el.isolateScope();
2124
};
2225

2326
beforeEach(function () {
@@ -104,6 +107,51 @@ describe('Directive: pfSimpleFilter', function () {
104107
expect(items.length).toBe($scope.filterConfig.fields[2].filterValues.length + 1); // +1 for the null value
105108
});
106109

110+
it ('should enforce single selection for select dropdowns, accumative for others', function() {
111+
$scope.filterConfig.appliedFilters = [
112+
{
113+
id: 'birthMonth',
114+
title: 'Birth Month',
115+
type: 'select',
116+
value: 'February'
117+
}
118+
];
119+
120+
var newFilter = {
121+
id: 'birthMonth',
122+
title: 'Birth Month',
123+
filterType: 'select'
124+
};
125+
126+
isoScope.addFilter(newFilter, "April");
127+
128+
expect($scope.filterConfig.appliedFilters.length).toBe(1);
129+
expect($scope.filterConfig.appliedFilters[0].value).toBe("April");
130+
131+
//Accumative for other types
132+
133+
$scope.filterConfig.appliedFilters = [
134+
{
135+
id: 'address',
136+
title: 'Address',
137+
type: 'text',
138+
value: 'New York'
139+
}
140+
];
141+
142+
newFilter = {
143+
id: 'address',
144+
title: 'Address',
145+
filterType: 'text'
146+
};
147+
148+
isoScope.addFilter(newFilter, 'Paris');
149+
150+
expect($scope.filterConfig.appliedFilters.length).toBe(2);
151+
expect($scope.filterConfig.appliedFilters[0].value).toBe("New York");
152+
expect($scope.filterConfig.appliedFilters[1].value).toBe("Paris");
153+
});
154+
107155
it ('should clear a filter when the close button is clicked', function () {
108156
var closeButtons;
109157

0 commit comments

Comments
 (0)