Created
February 13, 2024 07:31
-
-
Save hand-dot/f2aa2c4bfdc9acd8472fcb8b1f9b38d7 to your computer and use it in GitHub Desktop.
custom-element-fileuploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FileUploader extends HTMLElement { | |
| constructor() { | |
| super(); | |
| this.attachShadow({ mode: 'open' }); | |
| const input = document.createElement('input'); | |
| input.type = 'file'; | |
| this.shadowRoot.appendChild(input); | |
| // inputのchangeイベントリスナーを設定 | |
| input.addEventListener('change', (event) => { | |
| // 選択されたファイルを取得 | |
| const file = event.target.files[0]; | |
| this.selectedFile = file; | |
| // カスタムイベントを作成して発火 | |
| const customEvent = new CustomEvent('file-changed', { | |
| detail: { file }, // イベントに関連付けられたデータ | |
| bubbles: true, // イベントがバブリングするかどうか | |
| composed: true // イベントがShadow DOMの境界を越えるかどうか | |
| }); | |
| this.dispatchEvent(customEvent); | |
| }); | |
| } | |
| } | |
| customElements.define('file-uploader', FileUploader); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $w.onReady(function () { | |
| $w("#customElement1").on('file-changed', (event) => { | |
| console.log('file: ', event.detail.file) | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment