class FileUploader extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
const input = this.shadowRoot.querySelector('.file-input');
const button = this.shadowRoot.querySelector('.upload-btn');
const fileList = this.shadowRoot.querySelector('.file-list');
const files = [];
button.addEventListener('click', () => input.click());
input.addEventListener('change', (e) => {
files.length = 0; // Clear previous files
fileList.innerHTML = '';
Array.from(e.target.files).forEach(file => {
files.push(file);
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
fileItem.innerHTML = `
${file.name} (${(file.size / 1024).toFixed(2)} KB)
×
`;
fileItem.querySelector('.remove-btn').addEventListener('click', () => {
const index = files.indexOf(file);
if (index > -1) {
files.splice(index, 1);
}
fileItem.remove();
});
fileList.appendChild(fileItem);
});
});
// Expose files to parent component
this.getFiles = () => files;
}
}
customElements.define('file-uploader', FileUploader);