Skip to content

Commit 02d5d2b

Browse files
authored
fix(filters): Compound Operator alt texts should work with custom list (#1541)
* fix(filters): Compound Operator alt texts should work with custom list
1 parent 803fbee commit 02d5d2b

File tree

8 files changed

+126
-21
lines changed

8 files changed

+126
-21
lines changed

docs/column-functionalities/filters/compound-filters.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,17 @@ this.columnDefinitions = [
181181
### Compound Operator Alternate Texts
182182
You can change any of the compound operator text or description shown in the select dropdown list by using `compoundOperatorAltTexts` to provide alternate texts.
183183

184-
**Note** make sure to not use more than 2 or 3 characters for the operator text, exceeding that will require CSS style changes.
184+
The texts are separated into 2 groups (`numeric` or `text`) so that the alternate texts can be applied to all assigned filters, hence the type will vary depending on which Filter you choose as shown below:
185+
- `numeric`
186+
- `Filters.compoundDate`
187+
- `Filters.compoundInputNumber`
188+
- `Filters.compoundSlider`
189+
- `text`
190+
- `Filters.compoundInput`
191+
- `Filters.compoundInputPassword`
192+
- `Filters.compoundInputText`
193+
194+
> **Note** avoid using text with more than 2 or 3 characters for the operator text (which is roughly the width of the compound operator select dropdown), exceeding this limit will require CSS style changes.
185195
186196
```ts
187197
this.gridOptions = {

packages/common/src/filters/__tests__/compoundDateFilter.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ describe('CompoundDateFilter', () => {
516516
filterArguments.searchTerms = ['2000-01-01T05:00:00.000Z'];
517517
gridOptionMock.compoundOperatorAltTexts = {
518518
numeric: { '=': { operatorAlt: 'eq', descAlt: 'alternate numeric equal description' } },
519-
text: { '=': { operatorAlt: 'eq', descAlt: 'alternate text equal description' } }
520519
};
521520

522521
filter.init(filterArguments);
@@ -531,6 +530,33 @@ describe('CompoundDateFilter', () => {
531530
expect(removeExtraSpaces(filterOperatorElm[0][6].textContent!)).toBe('<> Not equal to');
532531
});
533532

533+
it('should have custom compound operator list including alternate texts and show up in the operator select dropdown options list', () => {
534+
mockColumn.outputType = null as any;
535+
filterArguments.searchTerms = ['2000-01-01T05:00:00.000Z'];
536+
mockColumn.filter!.compoundOperatorList = [
537+
{ operator: '', desc: '' },
538+
{ operator: '=', desc: 'Equal to' },
539+
{ operator: '<', desc: 'Less than' },
540+
{ operator: '>', desc: 'Greater than' },
541+
{ operator: 'Custom', desc: 'SQL LIKE' },
542+
];
543+
gridOptionMock.compoundOperatorAltTexts = {
544+
numeric: {
545+
'=': { operatorAlt: 'eq', descAlt: 'alternate numeric equal description' },
546+
'Custom': { operatorAlt: '%', descAlt: 'alternate SQL LIKE' }
547+
}
548+
};
549+
550+
filter.init(filterArguments);
551+
const filterOperatorElm = divContainer.querySelectorAll<HTMLSelectElement>('.input-group-prepend.operator select');
552+
553+
expect(filterOperatorElm[0][0].title).toBe('');
554+
expect(removeExtraSpaces(filterOperatorElm[0][1].textContent!)).toBe('eq alternate numeric equal description');
555+
expect(removeExtraSpaces(filterOperatorElm[0][2].textContent!)).toBe('< Less than');
556+
expect(removeExtraSpaces(filterOperatorElm[0][3].textContent!)).toBe('> Greater than');
557+
expect(removeExtraSpaces(filterOperatorElm[0][4].textContent!)).toBe('% alternate SQL LIKE');
558+
});
559+
534560
describe('with French I18N translations', () => {
535561
beforeEach(() => {
536562
gridOptionMock.enableTranslate = true;

packages/common/src/filters/__tests__/compoundInputFilter.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,35 @@ describe('CompoundInputFilter', () => {
413413
expect(removeExtraSpaces(filterOperatorElm[0][5].textContent!)).toBe('*z Ends With');
414414
});
415415

416+
it('should have custom compound operator list including alternate texts and show up in the operator select dropdown options list', () => {
417+
mockColumn.outputType = null as any;
418+
filterArguments.searchTerms = ['xyz'];
419+
mockColumn.filter!.compoundOperatorList = [
420+
{ operator: '', desc: '' },
421+
{ operator: '=', desc: 'Equal to' },
422+
{ operator: '<', desc: 'Less than' },
423+
{ operator: '>', desc: 'Greater than' },
424+
{ operator: 'Custom', desc: 'SQL LIKE' },
425+
];
426+
jest.spyOn(gridStub, 'getOptions').mockReturnValue({
427+
...gridOptionMock, compoundOperatorAltTexts: {
428+
text: {
429+
'=': { operatorAlt: 'eq', descAlt: 'alternate numeric equal description' },
430+
'Custom': { operatorAlt: '%', descAlt: 'alternate SQL LIKE' }
431+
}
432+
}
433+
});
434+
435+
filter.init(filterArguments);
436+
const filterOperatorElm = divContainer.querySelectorAll<HTMLSelectElement>('.search-filter.filter-duration select');
437+
438+
expect(filterOperatorElm[0][0].title).toBe('');
439+
expect(removeExtraSpaces(filterOperatorElm[0][1].textContent!)).toBe('eq alternate numeric equal description');
440+
expect(removeExtraSpaces(filterOperatorElm[0][2].textContent!)).toBe('< Less than');
441+
expect(removeExtraSpaces(filterOperatorElm[0][3].textContent!)).toBe('> Greater than');
442+
expect(removeExtraSpaces(filterOperatorElm[0][4].textContent!)).toBe('% alternate SQL LIKE');
443+
});
444+
416445
describe('with French I18N translations', () => {
417446
beforeEach(() => {
418447
gridOptionMock.enableTranslate = true;

packages/common/src/filters/__tests__/compoundSliderFilter.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,33 @@ describe('CompoundSliderFilter', () => {
395395
expect(removeExtraSpaces(filterOperatorElm[0][6].textContent!)).toBe('<> Not equal to');
396396
});
397397

398+
it('should have custom compound operator list including alternate texts and show up in the operator select dropdown options list', () => {
399+
mockColumn.outputType = null as any;
400+
filterArguments.searchTerms = ['9'];
401+
mockColumn.filter!.compoundOperatorList = [
402+
{ operator: '', desc: '' },
403+
{ operator: '=', desc: 'Equal to' },
404+
{ operator: '<', desc: 'Less than' },
405+
{ operator: '>', desc: 'Greater than' },
406+
{ operator: 'Custom', desc: 'SQL LIKE' },
407+
];
408+
gridOptionMock.compoundOperatorAltTexts = {
409+
numeric: {
410+
'=': { operatorAlt: 'eq', descAlt: 'alternate numeric equal description' },
411+
'Custom': { operatorAlt: '%', descAlt: 'alternate SQL LIKE' }
412+
}
413+
};
414+
415+
filter.init(filterArguments);
416+
const filterOperatorElm = divContainer.querySelectorAll<HTMLSelectElement>('.input-group-prepend.operator select');
417+
418+
expect(filterOperatorElm[0][0].title).toBe('');
419+
expect(removeExtraSpaces(filterOperatorElm[0][1].textContent!)).toBe('eq alternate numeric equal description');
420+
expect(removeExtraSpaces(filterOperatorElm[0][2].textContent!)).toBe('< Less than');
421+
expect(removeExtraSpaces(filterOperatorElm[0][3].textContent!)).toBe('> Greater than');
422+
expect(removeExtraSpaces(filterOperatorElm[0][4].textContent!)).toBe('% alternate SQL LIKE');
423+
});
424+
398425
describe('with French I18N translations', () => {
399426
beforeEach(() => {
400427
gridOptionMock.enableTranslate = true;

packages/common/src/filters/dateFilter.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
GridOption,
1919
OperatorDetail,
2020
} from '../interfaces/index';
21-
import { buildSelectOperator, compoundOperatorNumeric } from './filterUtilities';
21+
import { applyOperatorAltTextWhenExists, buildSelectOperator, compoundOperatorNumeric } from './filterUtilities';
2222
import { formatDateByFieldType, mapTempoDateFormatWithFieldType } from '../services/dateUtils';
2323
import { mapOperatorToShorthandDesignation } from '../services/utilities';
2424
import type { TranslaterService } from '../services/translater.service';
@@ -407,11 +407,17 @@ export class DateFilter implements Filter {
407407

408408
/** Get the available operator option values to populate the operator select dropdown list */
409409
protected getOperatorOptionValues(): OperatorDetail[] {
410+
let operatorList: OperatorDetail[];
410411
if (this.columnFilter?.compoundOperatorList) {
411-
return this.columnFilter.compoundOperatorList;
412+
operatorList = this.columnFilter.compoundOperatorList;
412413
} else {
413-
return compoundOperatorNumeric(this.gridOptions, this.translaterService);
414+
operatorList = compoundOperatorNumeric(this.gridOptions, this.translaterService);
414415
}
416+
417+
// add alternate texts when provided
418+
applyOperatorAltTextWhenExists(this.gridOptions, operatorList, 'numeric');
419+
420+
return operatorList;
415421
}
416422

417423
/**

packages/common/src/filters/filterUtilities.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ export function compoundOperatorString(gridOptions: GridOption, translaterServic
133133
{ operator: '*z', desc: getOutputText('ENDS_WITH', 'TEXT_ENDS_WITH', 'Ends with', gridOptions, translaterService) },
134134
];
135135

136-
// add alternate texts when provided
137-
applyOperatorAltTextWhenExists(gridOptions, operatorList, 'text');
138-
139136
return operatorList;
140137
}
141138

@@ -151,14 +148,11 @@ export function compoundOperatorNumeric(gridOptions: GridOption, translaterServi
151148
{ operator: '<>', desc: getOutputText('NOT_EQUAL_TO', 'TEXT_NOT_EQUAL_TO', 'Not equal to', gridOptions, translaterService) }
152149
];
153150

154-
// add alternate texts when provided
155-
applyOperatorAltTextWhenExists(gridOptions, operatorList, 'numeric');
156-
157151
return operatorList;
158152
}
159153

160154
// internal function to apply Operator detail alternate texts when they exists
161-
function applyOperatorAltTextWhenExists(gridOptions: GridOption, operatorDetailList: OperatorDetail[], filterType: 'text' | 'numeric') {
155+
export function applyOperatorAltTextWhenExists(gridOptions: GridOption, operatorDetailList: OperatorDetail[], filterType: 'text' | 'numeric') {
162156
if (gridOptions.compoundOperatorAltTexts) {
163157
for (const opDetail of operatorDetailList) {
164158
if (gridOptions.compoundOperatorAltTexts.hasOwnProperty(filterType)) {

packages/common/src/filters/inputFilter.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
OperatorDetail,
1212
} from '../interfaces/index';
1313
import { FieldType, OperatorType, type OperatorString, type SearchTerm } from '../enums/index';
14-
import { buildSelectOperator, compoundOperatorNumeric, compoundOperatorString } from './filterUtilities';
14+
import { applyOperatorAltTextWhenExists, buildSelectOperator, compoundOperatorNumeric, compoundOperatorString } from './filterUtilities';
1515
import { mapOperatorToShorthandDesignation, type TranslaterService, } from '../services';
1616
import { type SlickGrid } from '../core/index';
1717

@@ -219,25 +219,31 @@ export class InputFilter implements Filter {
219219
/** Get the available operator option values to populate the operator select dropdown list */
220220
protected getCompoundOperatorOptionValues(): OperatorDetail[] {
221221
const type = (this.columnDef.type && this.columnDef.type) ? this.columnDef.type : FieldType.string;
222-
let optionValues = [];
222+
let operatorList: OperatorDetail[];
223+
let listType: 'text' | 'numeric' = 'text';
223224

224225
if (this.columnFilter?.compoundOperatorList) {
225-
return this.columnFilter.compoundOperatorList;
226+
operatorList = this.columnFilter.compoundOperatorList;
226227
} else {
227228
switch (type) {
228229
case FieldType.string:
229230
case FieldType.text:
230231
case FieldType.readonly:
231232
case FieldType.password:
232-
optionValues = compoundOperatorString(this.gridOptions, this.translaterService);
233+
listType = 'text';
234+
operatorList = compoundOperatorString(this.gridOptions, this.translaterService);
233235
break;
234236
default:
235-
optionValues = compoundOperatorNumeric(this.gridOptions, this.translaterService);
237+
listType = 'numeric';
238+
operatorList = compoundOperatorNumeric(this.gridOptions, this.translaterService);
236239
break;
237240
}
238241
}
239242

240-
return optionValues;
243+
// add alternate texts when provided
244+
applyOperatorAltTextWhenExists(this.gridOptions, operatorList, listType);
245+
246+
return operatorList;
241247
}
242248

243249
/**

packages/common/src/filters/sliderFilter.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
} from '../interfaces/index';
2020
import type { TranslaterService } from '../services/translater.service';
2121
import { mapOperatorToShorthandDesignation } from '../services/utilities';
22-
import { buildSelectOperator, compoundOperatorNumeric } from './filterUtilities';
22+
import { applyOperatorAltTextWhenExists, buildSelectOperator, compoundOperatorNumeric } from './filterUtilities';
2323

2424
const DEFAULT_SLIDER_TRACK_FILLED_COLOR = '#86bff8';
2525
const GAP_BETWEEN_SLIDER_HANDLES = 0;
@@ -374,10 +374,17 @@ export class SliderFilter implements Filter {
374374

375375
/** Get the available operator option values to populate the operator select dropdown list */
376376
protected getOperatorOptionValues(): OperatorDetail[] {
377+
let operatorList: OperatorDetail[];
377378
if (this.columnFilter.compoundOperatorList) {
378-
return this.columnFilter.compoundOperatorList;
379+
operatorList = this.columnFilter.compoundOperatorList;
380+
} else {
381+
operatorList = compoundOperatorNumeric(this.gridOptions, this.translaterService);
379382
}
380-
return compoundOperatorNumeric(this.gridOptions, this.translaterService);
383+
384+
// add alternate texts when provided
385+
applyOperatorAltTextWhenExists(this.gridOptions, operatorList, 'numeric');
386+
387+
return operatorList;
381388
}
382389

383390
/** handle value change event triggered, trigger filter callback & update "filled" class name */

0 commit comments

Comments
 (0)