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: dynamically added filefields #846

Merged
merged 1 commit into from
Nov 11, 2024

Conversation

lukasvinclav
Copy link
Contributor

No description provided.

@lukasvinclav lukasvinclav merged commit b80a5f4 into main Nov 11, 2024
7 checks passed
@yardensachs
Copy link

This introduced a bug. I am debugging now.

@yardensachs
Copy link

@lukasvinclav There's an issue with the fileInputUpdatePath implementation that prevents it from working with file inputs that exist when the page initially loads.

Currently, the MutationObserver only watches for newly added elements (childList mutations) and attaches the change event handler to file inputs after they're dynamically added to the DOM. However, this means any file inputs that are part of the initial page HTML are missed completely.

Suggested Fix

We should modify the function to:

  1. Immediately attach handlers to existing file inputs when the function is called
  2. Keep the MutationObserver for handling dynamically added inputs

Here's how the code could be restructured:

const fileInputUpdatePath = () => {
  const handleFileInput = (input) => {
    input.addEventListener("change", (e) => {
      const parts = e.target.value.split("\\");
      const placeholder = input.parentNode.parentNode.parentNode.querySelector(
        "input[type=text]"
      );
      placeholder.setAttribute("value", parts[parts.length - 1]);
    });
  };

  // Handle existing inputs
  document.querySelectorAll("input[type=file]").forEach(handleFileInput);

  // Watch for new inputs
  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      if (mutation.type === "childList") {
        document.querySelectorAll("input[type=file]").forEach(handleFileInput);
      }
    }
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true,
  });
};

@lukasvinclav
Copy link
Contributor Author

Please create a new issue with the description.

@yardensachs
Copy link

Cool:
#855 and #856

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