Skip to content

Commit

Permalink
fix(common): Select Filter/Editor enableRenderHtml was wrong (#1096)
Browse files Browse the repository at this point in the history
- fixes an issue identified in Angular-Slickgrid as per this issue ghiscoding/Angular-Slickgrid#1240
- the problem was when I moved to the new multiple-select-vanilla lib, I used the wrong flag, we need to use `renderOptionLabelAsHtml` when we want text labels and `useSelectOptionLabelToHtml` when we want values to be rendered as HTML, so the issue was that the flags were inversed and that is why it was showing the values instead of the text labels
  • Loading branch information
ghiscoding committed Sep 5, 2023
1 parent d9f2b8d commit 1f09eef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
13 changes: 11 additions & 2 deletions examples/vite-demo-vanilla-bundle/src/examples/example07.ts
Expand Up @@ -128,7 +128,12 @@ export default class Example7 {
id: 'completed', nameKey: 'COMPLETED', field: 'completed', formatter: Formatters.checkmarkMaterial,
filterable: true, sortable: true,
filter: {
collection: [{ value: '', label: '' }, { value: true, label: 'True' }, { value: false, label: 'False' }],
enableRenderHtml: true,
collection: [
{ value: '', label: '' },
{ value: true, label: 'True', labelPrefix: `<i class="mdi mdi-check mdi-v-align-middle"></i> ` },
{ value: false, label: 'False', labelPrefix: `<i class="mdi mdi-close mdi-v-align-middle"></i> ` }
],
model: Filters.singleSelect
},
editor: {
Expand All @@ -138,8 +143,12 @@ export default class Example7 {
// collection: [{ value: true, label: 'True' }, { value: false, label: 'False' }],

// Select Editor can also support collection that are async, it could be a Promise (shown below) or Fetch result
enableRenderHtml: true,
collectionAsync: new Promise<any>(resolve => setTimeout(() => {
resolve([{ value: true, label: 'True' }, { value: false, label: 'False' }]);
resolve([
{ value: true, label: 'True', labelPrefix: `<i class="mdi mdi-check mdi-v-align-middle"></i> ` },
{ value: false, label: 'False', labelPrefix: `<i class="mdi mdi-close mdi-v-align-middle"></i> ` }
]);
}, 250)),
},
},
Expand Down
Expand Up @@ -87,6 +87,8 @@ describe('SingleSelectEditor', () => {
const editorCount = document.body.querySelectorAll('select.ms-filter.editor-gender').length;

expect(editorCount).toBe(1);
expect(editor.selectOptions.renderOptionLabelAsHtml).toBeFalsy();
expect(editor.selectOptions.useSelectOptionLabelToHtml).toBeFalsy();
});

it('should hide the DOM element div wrapper when the "hide" method is called', () => {
Expand Down Expand Up @@ -245,6 +247,8 @@ describe('SingleSelectEditor', () => {
const editorListElm = divContainer.querySelectorAll<HTMLInputElement>(`[data-name=editor-gender].ms-drop ul>li span`);
editorBtnElm.click();

expect(editor.selectOptions.renderOptionLabelAsHtml).toBeTruthy();
expect(editor.selectOptions.useSelectOptionLabelToHtml).toBeFalsy();
expect(editor.getValue()).toEqual('');
expect(editorListElm.length).toBe(2);
expect(editorListElm[0].innerHTML).toBe('<i class="fa fa-check"></i> True');
Expand Down
6 changes: 5 additions & 1 deletion packages/common/src/editors/selectEditor.ts
Expand Up @@ -121,7 +121,7 @@ export class SelectEditor implements Editor {
minHeight: 25,
name: this.elementName,
single: true,
useSelectOptionLabelToHtml: this.columnEditor?.enableRenderHtml ?? false,
renderOptionLabelAsHtml: this.columnEditor?.enableRenderHtml ?? false,
sanitizer: (dirtyHtml: string) => sanitizeTextByAvailableSanitizer(this.gridOptions, dirtyHtml),
onClick: () => this._isValueTouched = true,
onCheckAll: () => this._isValueTouched = true,
Expand Down Expand Up @@ -209,6 +209,10 @@ export class SelectEditor implements Editor {
return this._msInstance;
}

get selectOptions() {
return this.defaultOptions;
}

/**
* The current selected values (multiple select) from the collection
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/filters/__tests__/selectFilter.spec.ts
Expand Up @@ -490,6 +490,8 @@ describe('SelectFilter', () => {
filterBtnElm.click();
filter.msInstance?.close();

expect(filter.selectOptions.renderOptionLabelAsHtml).toBeTruthy();
expect(filter.selectOptions.useSelectOptionLabelToHtml).toBeFalsy();
expect(filterListElm.length).toBe(2);
expect(filterListElm[0].innerHTML).toBe('<i class="fa fa-check"></i> True');
});
Expand Down Expand Up @@ -529,6 +531,8 @@ describe('SelectFilter', () => {
filterOkElm.click();
filter.msInstance?.close();

expect(filter.selectOptions.renderOptionLabelAsHtml).toBeFalsy();
expect(filter.selectOptions.useSelectOptionLabelToHtml).toBeFalsy();
expect(filterListElm.length).toBe(3);
expect(filterFilledElms.length).toBe(1);
expect(filterListElm[0].value).toBe('');
Expand Down
8 changes: 6 additions & 2 deletions packages/common/src/filters/selectFilter.ts
Expand Up @@ -80,7 +80,7 @@ export class SelectFilter implements Filter {

/** Getter for the Grid Options pulled through the Grid Object */
get gridOptions(): GridOption {
return (this.grid && this.grid.getOptions) ? this.grid.getOptions() : {};
return this.grid?.getOptions() ?? {};
}

/** Getter to know what would be the default operator when none is specified */
Expand All @@ -97,6 +97,10 @@ export class SelectFilter implements Filter {
return this._msInstance;
}

get selectOptions() {
return this.defaultOptions;
}

/** Getter for the Filter Operator */
get operator(): OperatorType | OperatorString {
return this.columnFilter?.operator ?? this.defaultOperator;
Expand Down Expand Up @@ -416,7 +420,7 @@ export class SelectFilter implements Filter {
filter: false, // input search term on top of the select option list
maxHeight: 275,
single: true,
useSelectOptionLabelToHtml: this.columnFilter?.enableRenderHtml ?? false,
renderOptionLabelAsHtml: this.columnFilter?.enableRenderHtml ?? false,
sanitizer: (dirtyHtml: string) => sanitizeTextByAvailableSanitizer(this.gridOptions, dirtyHtml),
// we will subscribe to the onClose event for triggering our callback
// also add/remove "filled" class for styling purposes
Expand Down

0 comments on commit 1f09eef

Please sign in to comment.