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

refactor!: split file input change event into seperate function #129

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

Conversation

jonkoops
Copy link
Collaborator

What kind of change does this PR introduce?

  • Bug Fix
  • Feature
  • Refactoring
  • Style
  • Build
  • Chore
  • Documentation
  • CI

Did you add tests for your changes?

  • Yes, my code is well tested
  • Not relevant

If relevant, did you update the documentation?

  • Yes, I've updated the documentation
  • Not relevant

Summary
This PR is a continuation of #126 and removes the handling of the change event from the fromEvent() function, and moves it to a dedicated fromChangeEvent() function purposefully made for handling this specific event.

This reduces the need to handle polymorphic input and narrow down what the exact type should be in fromEvent(), in this case also removing the need for this operation to be async. Additionally, this allows bundlers to more effectively tree-shake the module in case the other code is never used. The JSDoc on the function itself has also been improved with additional details for the consumer.

Does this PR introduce a breaking change?
Yes, change events are now handled in a dedicated fromChangeEvent() function. Users that were previously using fromEvent() will now have to use this dedicated function instead.

Other information
Not applicable.

Signed-off-by: Jon Koops <[email protected]>

BREAKING CHANGE: File input `change` events are now handled in a dedicated `fromChangeEvent()` function.
@jonkoops jonkoops requested a review from rolandjitsu February 24, 2025 13:52
@coveralls
Copy link

Pull Request Test Coverage Report for Build 13499636153

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 13480585681: 0.0%
Covered Lines: 93
Relevant Lines: 93

💛 - Coveralls

@@ -1,2 +1,6 @@
export { fromEvent, fromFileHandles } from "./file-selector.js";
export {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe that providing a convenience function that runs though each would help users migrate smoothly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to rename and refactor fromEvent to something else as well, perhaps we can then evaluate adding it back and see if it is worth to keep it in a deprecated manner?

I have to do some digging as well and see how we're going to go about upgrading this dependency in the main project, so there will likely be a beta branch there as well.

* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file|MDN - `<input type="file">`}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event|MDN - HTMLElement: `change` event}
*/
export function fromChangeEvent(event: Event): FileWithPath[] {
if (!(event.target instanceof HTMLInputElement) || !event.target.files) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also check with isObject? Since we don't know what to expect at runtime.

Suggested change
if (!(event.target instanceof HTMLInputElement) || !event.target.files) {
if (!isObject(event) || !(event.target instanceof HTMLInputElement) || !event.target.files) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is needed, we already have a very specific type of Event, so this value should always be correct. Of course users can pass whatever they want if they are not using TypeScript, but then they are still violating the API contract, and it is their responsibility to do this check.

@jonkoops jonkoops requested a review from rolandjitsu February 25, 2025 12:38
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.

3 participants