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]: sp-slider only adds one sp-slider-handle when multiple are defined #2035

Closed
1 task done
peterjanes opened this issue Jan 7, 2022 · 10 comments · Fixed by #2040
Closed
1 task done

[Bug]: sp-slider only adds one sp-slider-handle when multiple are defined #2035

peterjanes opened this issue Jan 7, 2022 · 10 comments · Fixed by #2040
Assignees

Comments

@peterjanes
Copy link

peterjanes commented Jan 7, 2022

Code of conduct

  • I agree to follow this project's code of conduct.

Impacted component(s)

sp-slider, sp-slider-handle

Expected behavior

Adding an sp-slider with multiple handles from an HTML template should add all of the handles.

Actual behavior

I haven't nailed down the exact conditions, but sometimes when adding sp-sliders dynamically from an HTML template only one instance of sp-slider-handle will be added because the handle map key is undefined. (I finally tracked down the behaviour when I started getting "First slider handle cannot have attribute min='previous'" after adding max="next" to the first sp-slider-handle and min="previous" to the second.)

Screenshots

No response

What browsers are you seeing the problem in?

Firefox, Chrome

How can we reproduce this issue?

I haven't been able to come up with a reduced test case, but the issue is 100% reproducible in my (currently non-public) app and seems to be due to some instances of a field not being renamed. (handle.name is used instead of handle.handleName in HandleController.ts.) I'll create an associated pull request for review.

Sample code that illustrates the problem

No response

Logs taken while reproducing problem

No response

@peterjanes peterjanes added bug Something isn't working triage An issue needing triage labels Jan 7, 2022
@Westbrook Westbrook added API Component: Range Slider Needs repro and removed triage An issue needing triage labels Jan 7, 2022
@Westbrook
Copy link
Collaborator

@peterjanes thanks for sharing this issue!

Before we can fully review changes in support of what you're experiencing, we will need a reproduction case so that we can compare your findings with ours and ensure that all users of this element are being appropriately served. I'd suggest you start from something like https://webcomponents.dev/edit/collection/fO75441E1Q5ZlI0e9pgq/U7LQv7LsAVBwJayJXG3B/src/index.ts and see if you can isolate the issue you are experiencing.

@peterjanes
Copy link
Author

peterjanes commented Jan 7, 2022

Yeah, I've been working on a reproduce case for a couple of days but can't get it to fail. Figured I'd submit the PR that fixes it (or appears to) as it seems like a pretty obvious update; handle.name is only set when handle.handleName isn't set (and only used in a log message), and the undefined handle.handleName is immediately used as the map key (so only the last handle added will ever be present).

@peterjanes
Copy link
Author

Here's my current non-failing reproduce case, which mirrors what the live code does:

<!DOCTYPE html>
<script src="https://jspm.dev/@spectrum-web-components/bundle/elements.js" type="module"></script>

<div>
<sp-theme scale="medium" color="light">
</sp-theme>
</div>

<template id="rangeTemplate">
  <sp-slider variant="range" step="1">
    <sp-slider-handle slot="handle" name="min" label="Minimum" max="next"></sp-slider-handle>
    <sp-slider-handle slot="handle" name="max" label="Maximum" min="previous"></sp-slider-handle>
  </sp-slider>
</template>

<script module>
async function foo() {
  const res = await fetch('.');
  const rangeGroups = new Map();
  rangeGroups.set('group1', [{id: 'range1.1', min: 10, max: 90}, {id: 'range1.2', min: 15, max: 85}]);
  rangeGroups.set('group2', [{id: 'range2.1', min: 20, max: 80}, {id: 'range2.2', min: 25, max: 75}]);

  const rangesList = document.createElement('div');
  const rangeTemplate = document.querySelector('#rangeTemplate');

  rangeGroups.forEach((rangeGroup, key) => {
    const rangeList = document.createElement('div');
    rangeGroup.forEach((range) => {
      const rangeElement = rangeTemplate.content.cloneNode(true);
      const slider = rangeElement.querySelector('sp-slider');
      slider.setAttribute('id', range.id);
      slider.setAttribute('label', range.id);
      slider.setAttribute('min', String(range.min));
      slider.setAttribute('max', String(range.max));
      slider.querySelector('[name=min]').setAttribute('value', String(range.min));
      slider.querySelector('[name=max]').setAttribute('value', String(range.max));
      rangeList.appendChild(rangeElement);
    });
    rangesList.appendChild(rangeList);
  });
  document.querySelector('sp-theme').appendChild(rangesList);
  return 'done';
}
foo().then(console.log);
</script>

@Westbrook
Copy link
Collaborator

I assume you're not using <script src="https://jspm.dev/@spectrum-web-components/bundle/elements.js" type="module"></script> in your production code. Is there anything about how you're importing the elements that you can share, along with which versions you're leveraging?

@peterjanes
Copy link
Author

In the live app I've reduced that whole thing to

function createElement(html) {
  const temp = document.createElement('template');
  temp.innerHTML = html.trim();
  return temp.content.firstChild;
}

async function foo() {
  await fetch('.');
  document.body.appendChild(
      createElement(`<div>
  <sp-slider variant="range" step="1" id="price" name="price" label="Max Price" min="35425" max="86610">
    <sp-slider-handle slot="handle" name="min" label="Minimum" max="next" value="35425"></sp-slider-handle>
    <sp-slider-handle slot="handle" name="max" label="Maximum" min="previous" value="86610"></sp-slider-handle>
  </sp-slider>
</div>`);
}

foo();

and it still fails.

The app loads the dependencies with

import '@spectrum-web-components/theme/theme-light';
import '@spectrum-web-components/theme/scale-medium';
import '@spectrum-web-components/theme/sp-theme';
import '@spectrum-web-components/button/sp-button';
import '@spectrum-web-components/slider/sp-slider';
import '@spectrum-web-components/slider/sp-slider-handle';

from

    "@spectrum-web-components/button": "^0.16.1",
    "@spectrum-web-components/slider": "^0.12.1",
    "@spectrum-web-components/theme": "^0.9.1",

@Westbrook
Copy link
Collaborator

How is JS loaded into the app? For instance this (working demo from above) uses <script type="module"> which works different that a <script>, a dynamic import, etc.

@peterjanes
Copy link
Author

<script type="module" src="script.js"></script>

@peterjanes
Copy link
Author

I'm going to try again to strip down the app to something I can share instead of trying to build up a test case. Will let you know.

@peterjanes
Copy link
Author

https://home.peterjanes.ca/~peterj/2022/bug-2035/ has a fairly minimal failing test case, with no recompilation, subsetting, etc. of the Spectrum components.

@hunterloftis
Copy link
Contributor

Thanks for the minimal example @peterjanes - I've used it to add a test reproducing the issue so that we can figure out a root cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants