Skip to content

Commit b3d2413

Browse files
authored
Merge pull request #11 from github/dont-required-child-input-to-have-id
Don't required child input to have ID
2 parents dddc7ce + 0e1cca8 commit b3d2413

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import '@github/file-attachment-element'
1515
```
1616

1717
```html
18-
<file-attachment directory input="upload">
19-
<input id="upload" type="file" multiple>
18+
<file-attachment directory>
19+
<input type="file" multiple />
2020
</file-attachment>
2121
```
2222

2323
### Optional attributes
2424

2525
- `file-attachment[directory]` enables traversing directories.
26-
- `file-attachment[input]` points to the ID of a file input inside of `<file-attachment>`. Files selected from the `<input>` will be attached to `<file-attachment>`. Supplying an input is strongly recommended in order to ensure users can upload files without a mouse or knowing where to paste files.
26+
- `file-attachment[input]` points to the ID of a file input inside of `<file-attachment>`. If supplied, only files selected from the corresponding `<input>` will be attached to `<file-attachment>`.
2727

2828
### Styling drag state
2929

src/file-attachment-element.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Attachment from './attachment'
22

33
export default class FileAttachmentElement extends HTMLElement {
4-
constructor() {
5-
super()
4+
connectedCallback(): void {
65
this.addEventListener('dragenter', onDragenter)
76
this.addEventListener('dragover', onDragenter)
87
this.addEventListener('dragleave', onDragleave)
@@ -11,6 +10,15 @@ export default class FileAttachmentElement extends HTMLElement {
1110
this.addEventListener('change', onChange)
1211
}
1312

13+
disconnectedCallback(): void {
14+
this.removeEventListener('dragenter', onDragenter)
15+
this.removeEventListener('dragover', onDragenter)
16+
this.removeEventListener('dragleave', onDragleave)
17+
this.removeEventListener('drop', onDrop)
18+
this.removeEventListener('paste', onPaste)
19+
this.removeEventListener('change', onChange)
20+
}
21+
1422
get directory(): boolean {
1523
return this.hasAttribute('directory')
1624
}
@@ -132,8 +140,9 @@ function onChange(event: Event) {
132140
if (!(container instanceof FileAttachmentElement)) return
133141
const input = event.target
134142
if (!(input instanceof HTMLInputElement)) return
143+
135144
const id = container.getAttribute('input')
136-
if (!id || input.id !== id) return
145+
if (id && input.id !== id) return
137146

138147
const files = input.files
139148
if (!files || files.length === 0) return

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ describe('file-attachment', function () {
6161
let fileAttachment, input
6262
beforeEach(function () {
6363
document.body.innerHTML = `
64-
<file-attachment input="upload">
65-
<input type="file" id="upload">
64+
<file-attachment>
65+
<input type="file">
6666
</file-attachment>`
6767

6868
fileAttachment = document.querySelector('file-attachment')

0 commit comments

Comments
 (0)