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

Fix constructor #693

Merged
merged 21 commits into from
Oct 29, 2019
Merged

Fix constructor #693

merged 21 commits into from
Oct 29, 2019

Conversation

jshjohnson
Copy link
Collaborator

@jshjohnson jshjohnson commented Oct 24, 2019

Description

As highlighted by @tinovyatkin here, the ability to pass multiple elements to the Choices constructor and get multiple instances back was buggy at best. This PR removes that functionality so the Choices constructor will only ever return one instance and that instance will only be associated with one element. To achieve the same behaviour, you would need to do the following:

const elements = Array.from(document.querySelectorAll('.your-class'));

const instances = elements.map((element) => {
  return new Choices(element);
});

or even simpler:

const elements = Array.from(
  document.querySelectorAll(".your-class"),
  element => new Choices(element)
);

I've also updated the constructor to throw an error if no valid element is passed or no valid element is found in the DOM. Previously this would have failed silently.

How Has This Been Tested?

Constructor tests have been added with this change.

Types of changes

  • 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.

README.md Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Oct 24, 2019

Codecov Report

Merging #693 into master will increase coverage by 0.35%.
The diff coverage is 57.14%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #693      +/-   ##
==========================================
+ Coverage   66.03%   66.38%   +0.35%     
==========================================
  Files          23       23              
  Lines        1316     1306      -10     
==========================================
- Hits          869      867       -2     
+ Misses        447      439       -8
Impacted Files Coverage Δ
src/scripts/lib/utils.js 79.16% <100%> (+1.08%) ⬆️
src/scripts/components/wrapped-element.js 93.54% <100%> (ø) ⬆️
src/scripts/components/list.js 24.32% <11.11%> (ø) ⬆️
src/scripts/choices.js 53.98% <62.96%> (+0.35%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 88f63fa...8ff8a0c. Read the comment docs.

@tinovyatkin
Copy link
Contributor

@jshjohnson Array.from accepts second parameter, so, code may be easier:

const elements = Array.from(
  document.querySelectorAll(".your-class"),
  element => new Choices(element)
);

@jshjohnson
Copy link
Collaborator Author

jshjohnson commented Oct 24, 2019

@jshjohnson Array.from accepts second parameter, so, code may be easier:

Nice - have updated

@tinovyatkin
Copy link
Contributor

Also need to update typings by removing brackets hack.
And probably document behaviour if Choices is constructed with selector that may return several elements (we use document.querySelector to pick the first one)

@tinovyatkin
Copy link
Contributor

tinovyatkin commented Oct 24, 2019

@jshjohnson consider also #691 for 8.x 🙏

@jshjohnson
Copy link
Collaborator Author

Also need to update typings by removing brackets hack.
And probably document behaviour if Choices is constructed with selector that may return several elements (we use document.querySelector to pick the first one)

See here: 4435efc - let me know if this is wrong

@jshjohnson jshjohnson added pinned and removed pinned labels Oct 24, 2019
src/scripts/choices.js Outdated Show resolved Hide resolved
src/scripts/choices.js Outdated Show resolved Hide resolved
src/scripts/choices.js Outdated Show resolved Hide resolved
@tinovyatkin tinovyatkin mentioned this pull request Oct 27, 2019
6 tasks
@jshjohnson jshjohnson merged commit 0e44a91 into master Oct 29, 2019
@jshjohnson jshjohnson deleted the fix-constructor branch October 29, 2019 21:20
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.

2 participants