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

Add support for using choicesjs-stencil web component within Stencil components. #877

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add support for using choicesjs-stencil web component within Stencil components. #877

wants to merge 1 commit into from

Conversation

sadamia
Copy link

@sadamia sadamia commented Jul 8, 2020

I'm trying to use choicesjs-stencil within StencilJS web component, when I initialize the component with option single, the dropdown doesn't show up, the problem is in click handler:

  _onClick({ target }: Pick<MouseEvent, 'target'>): void {
    const clickWasWithinContainer = this.containerOuter.element.contains(
      target as Node,
    );

    if (clickWasWithinContainer) {

https://github.com/jshjohnson/Choices/blob/master/src/scripts/choices.ts#L1735

The target variable is the web component itself instead of being div.choices__item element because,
the original target is hidden as "internal implementation", and instead the event is re-targeted to the host element.

As a solution instead of relaying on the event.target I tried this solution

  _onClick(event: MouseEvent): void {
    const path = eventPath(event);
    const target = (path && path[0]) || event.target;
    const clickWasWithinContainer = this.containerOuter.element.contains(
      target as Node,
    );

where I am getting the target from the event path.

To reproduce this issue please follow this steps:

git clone https://github.com/sadamia/choicesjs-stencil-example.git
cd choicesjs-stencil-example
npm install
npm run start

Inspect the component from dev tools, find the click handler attach breakpoint, you will see that the target is the web component itself
screen

Types of changes

  • Chore (tooling change or documentation change)
  • Refactor (non-breaking change which maintains existing functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

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

Successfully merging this pull request may close these issues.

None yet

1 participant