Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support icons in selected item also, not only in list of options #337

Merged
merged 9 commits into from
Jun 13, 2024
4 changes: 4 additions & 0 deletions cypress/e2e/examples.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ describe('Add image/icon', () => {
it('has flag icon on scroll', () => {
cy.getVs(id).scrollOptions(700).hasFlagIcon().parent().contains('Option 16');
});

it('has flag icon on selected item', () => {
cy.open(id).selectOption(16).hasSelectedFlagIcon();
});
});

describe('Show values as tags', () => {
Expand Down
4 changes: 4 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ Cypress.Commands.add('hasFlagIcon', { prevSubject: true }, (vsElem) => {
cy.getDropbox(vsElem).find('.vscomp-option').first().find('i.flag');
});

Cypress.Commands.add('hasSelectedFlagIcon', { prevSubject: true }, (vsElem) => {
cy.get(vsElem).find('.vscomp-value').find('i.flag');
});

Cypress.Commands.add('hasValueTags', { prevSubject: true }, (vsElem, labels) => {
labels.forEach((label) => {
cy.get(vsElem).find('.vscomp-value-tag').contains(label);
Expand Down
6 changes: 6 additions & 0 deletions cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ declare namespace Cypress {
*/
hasFlagIcon(): Chainable<any>;

/**
* @example
* cy.hasFlagIcon()
*/
hasSelectedFlagIcon(): Chainable<any>;

/**
* @example
* cy.hasValueTags(['Option 3', 'Option 5'])
Expand Down
1 change: 1 addition & 0 deletions docs/assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ function initPageExamples() {
initVirtualSelect({
ele: '#with-image-select',
labelRenderer: sampleLabelRenderer,
selectedLabelRenderer: sampleLabelRenderer,
});

initVirtualSelect({
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ section.cover {
margin-bottom: 20px;
}

.vscomp-option-text .flag {
.vscomp-value .flag, .vscomp-option-text .flag {
margin-right: 12px;
-webkit-transform: scale(1.4);
transform: scale(1.4);
Expand Down
110 changes: 55 additions & 55 deletions docs/assets/virtual-select.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/virtual-select.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,16 @@ VirtualSelect.init({

## Add image/icon

Use `labelRenderer` callback function to add image, icon, or custom content
Use `labelRenderer` callback function to add image, icon, or custom content to items
Use `selectedLabelRenderer` callback function to add image, icon, or custom content to selected item

<div id="with-image-select"></div>

```js
VirtualSelect.init({
...
labelRenderer: sampleLabelRenderer,
selectedLabelRenderer: sampleLabelRenderer
});

function sampleLabelRenderer(data) {
Expand Down
1 change: 1 addition & 0 deletions docs/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
| onServerSearch | Function | | Callback function to integrate server search |
| searchDelay | Number | 300 | Delay time in milliseconds to trigger onServerSearch callback function |
| labelRenderer | Function | | Callback function to render label, which could be used to add image, icon, or custom content |
| selectedLabelRenderer | Function | | Callback function to render label of selected item when select is closed, similar to labelRenderer |
| ariaLabelledby | String | | ID of the label element to use as a11y attribute aria-labelledby |
| hideValueTooltipOnSelectAll | Boolean | true | Hide value tooltip if all options selected |
| showOptionsOnlyOnSearch | Boolean | false | Show options to select only if search value is not empty |
Expand Down
12 changes: 9 additions & 3 deletions src/virtual-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class VirtualSelect {
let newOptionIconHtml = '';
const markSearchResults = !!(this.markSearchResults && this.searchValue);
let searchRegex;
const { labelRenderer, disableOptionGroupCheckbox, uniqueId, searchGroup } = this;
const { labelRenderer, selectedLabelRenderer, disableOptionGroupCheckbox, uniqueId, searchGroup } = this;
const hasLabelRenderer = typeof labelRenderer === 'function';
const { convertToBoolean } = Utils;
let groupName = '';
Expand Down Expand Up @@ -917,6 +917,7 @@ export class VirtualSelect {
this.popupPosition = options.popupPosition;
this.onServerSearch = options.onServerSearch;
this.labelRenderer = options.labelRenderer;
this.selectedLabelRenderer = options.selectedLabelRenderer;
this.initialSelectedValue = options.selectedValue === 0 ? '0' : options.selectedValue;
this.emptyValue = options.emptyValue;
this.ariaLabelledby = options.ariaLabelledby;
Expand Down Expand Up @@ -1534,7 +1535,7 @@ export class VirtualSelect {
}

setValueText() {
const { multiple, selectedValues, noOfDisplayValues, showValueAsTags, $valueText } = this;
const { multiple, selectedValues, noOfDisplayValues, showValueAsTags, $valueText, selectedLabelRenderer } = this;
const valueText = [];
let valueTooltip = [];
const selectedLength = selectedValues.length;
Expand All @@ -1560,7 +1561,12 @@ export class VirtualSelect {
return true;
}

const { label } = d;
let { label } = d;

if(typeof selectedLabelRenderer === 'function' ){
label = selectedLabelRenderer(d);
}

valueText.push(label);
selectedValuesCount += 1;

Expand Down