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

[Bug]: Selects label dissapear #393

Open
3 tasks done
baccarg opened this issue Jul 9, 2023 · 6 comments
Open
3 tasks done

[Bug]: Selects label dissapear #393

baccarg opened this issue Jul 9, 2023 · 6 comments
Labels
bug Something isn't working component: select

Comments

@baccarg
Copy link

baccarg commented Jul 9, 2023

Before submitting...

Context

Label display not showing in SELECT element
Screenshot 2023-07-09 at 17-24-05 alumnos

Current Behavior

No response

Expected behavior

No response

Possible Solutions or Causes

my code:

Sel. una opción Option 1 Option 2 Option 3 Provincia Sel. Provincia

Steps to reproduce

No response

Your Environment

  • Version used: 2.0.1 alpha
  • Browser Name and version: Firefox / Chrome
  • Operating System and version (desktop or mobile): Windows 11
  • Additional information you want to tell us: In code console cant see "label" element, its disappeared...Im try deleting all javascript includes but keep doing this.
@baccarg baccarg added the bug Something isn't working label Jul 9, 2023
@baccarg
Copy link
Author

baccarg commented Jul 9, 2023

My code:
<div class="input-field outlined col-12 col-md-4"> <select required class="validate" name="id_provincia" id="id_provincia"> <option value="">Sel. una opción</option> <option value="">San Luis</option> </select> <label for="id_provincia">Provincia</label> <span class="helper-text">Sel. Provincia</span> </div>

@baccarg
Copy link
Author

baccarg commented Jul 10, 2023

Ok, im find the bug.

If you initialize a < select > and initialize again < select > the label tag dissapear.

@baccarg baccarg changed the title [Bug]: [Bug]: Selects label dissapear Jul 13, 2023
@philipraets
Copy link

I've also have seen this, and created a reinitialize function, which rebuilds the label and re-initializes the select element:

function reInitializeSelectElements(select) {
	if (select instanceof NodeList) {
		select.forEach(element => {
			var selectInstance = M.FormSelect.getInstance(element);
			var labelTekst = selectInstance.labelEl.innerHTML;
			selectInstance.destroy();
			var labelFor = element.getAttribute("id");
			var itemLabel = document.createElement("Label");
			itemLabel.setAttribute("for", labelFor);
			itemLabel.innerHTML = labelTekst;
			element.parentNode.appendChild(itemLabel);
		});
	} else {
		var selectInstance = M.FormSelect.getInstance(select);
		var labelTekst = selectInstance.labelEl.innerHTML;
		selectInstance.destroy();
		var labelFor = select.getAttribute("id");
		var itemLabel = document.createElement("Label");
		itemLabel.setAttribute("for", labelFor);
		itemLabel.innerHTML = labelTekst;
		select.parentNode.appendChild(itemLabel);
	}

	M.FormSelect.init(select, {
		dropdownOptions: {
			container: document.body
		}
	});
}

usage:

reInitializeSelectElements(document.querySelector("select[name='id_provincia']"));

or

reInitializeSelectElements(document.querySelectorAll("select"));

@Corbett000
Copy link

Can you give me a situation where you would want to initialize the same element twice? I think it is a mistake to initialize the element twice.

I looked at the code behind the select elements and I think I see where the problem is if somebody does want to take to fixing this in the future.
The culprit seems to be this line at the beginning of the _setupDropdown() function:
this.labelEl = this.el.parentElement.querySelector('label');

Because the original html label is changed and moved around when the select element is initialized, this.el.parentElement.querySelector('label') resolves to null when the element is reinitialized.

This could possibly be solved by checking if this.labelEl is null and then looking for the label in the wrapper div instead.

@philipraets
Copy link

if you dynamically need to change the value of the select element, you will need to reinitialize the select element, otherwise the the change is not visible.

@wuda-io wuda-io mentioned this issue Jul 19, 2023
4 tasks
@Lord-Leonard
Copy link

Lord-Leonard commented Sep 4, 2023

I am currently working arround this by keeping a reference to the instance inside the costom web component in which the options are initialized. Whenever I need to interact with the Select thingy, I talk to the web component and implement a function in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working component: select
Projects
None yet
Development

No branches or pull requests

5 participants