Skip to content

[Bug]: selected options lose their optgroup when option hideSelected:false #938

@picov

Description

@picov

Bug description

A little sample to describe the problem.

We have a basic multiple select with optgroups and some preselected options.
We need to keep the selected options in the dropdown menu (hideSelected:false) and we need to render them according to the class of their optgroup.

HTML

<select id="select-gear" multiple placeholder="Select gear...">
	<option value="">Select something...</option>
	<optgroup label="Group#1" class="optgrp1cssclass">
		<option value="pitons">Pitons</option>
		<option value="cams">Cams</option>
		<option value="nuts" selected>Nuts</option>
	</optgroup>
	<optgroup label="Group#2" class="optgrp2cssclass">
		<option value="dog">Dog</option>
		<option value="cat" selected>Cat</option>
	</optgroup>
</select>

TS INITIALIZATION (v 2.4.3)

new TomSelect('#select-gear', {
	hideSelected: false, // Keep selected options in dropdown menu
	render: {
		option: function (data, escape) {
			const parentEl = data.$option.parentElement;
			if (parentEl.nodeName === "OPTGROUP") {
				console.log("Optgroup classname for option '" + data.text + "' is: " + parentEl.className);
			} else {
				console.log("No optgroup for option  '" + data.text + "' -> could not read optgroup className!");
			}
			return '<div>- ' + escape(data.text) + '</div>';
		}
	}
});

CONSOLE LOG:

[Log] Optgroup classname for option 'Pitons' is: optgrp1cssclass 
[Log] Optgroup classname for option 'Cams' is: optgrp1cssclass
[Log] No optgroup for option  'Nuts' -> could not read optgroup className!
[Log] Optgroup classname for option 'Dog' is: optgrp2cssclass
[Log] No optgroup for option  'Cat' -> could not read optgroup className! 

The parent element of all selected items INCORRECTLY become the SELECT with a DOM structure like this:

<select id="select-gear" multiple="multiple" placeholder="Select something...">
	<option value="">Select something...</option>
	<optgroup label="Group#1" class="optgrp1cssclass">
		<option value="pitons">Pitons</option>
		<option value="cams">Cams</option>
		
	</optgroup>
	<optgroup label="Group#2" class="optgrp2cssclass">
		<option value="dog">Dog</option>
		
	</optgroup>
	<option value="nuts" selected="">Nuts</option>
	<option value="cat" selected="">Cat</option>
</select>

Expected behavior

Selected items should keep the information about their original optgroup because with the option hideSelected: false they're still rendered in that position and should be possible to custom render theme like the others.

Steps to reproduce

Run the sample code provided or edit the examples provided inside the "Option Groups" page of the TS website in CodePen or JSFiddle.

Additional context

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions