Skip to content

Add support for file inputs #4

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

Merged
merged 5 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ import '@github/file-attachment-element'
```

```html
<file-attachment directory>
<file-attachment directory input="upload">
<input id="upload" type="file" multiple>
</file-attachment>
```

### Optional attributes

- `file-attachment[directory]` enables traversing directories.
- `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.

### Styling drag state

A boolean `[hover]` attribute is present on `<file-attachment>` while files are dragged over the element.
Expand Down
16 changes: 16 additions & 0 deletions src/file-attachment-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class FileAttachmentElement extends HTMLElement {
this.addEventListener('dragleave', onDragleave)
this.addEventListener('drop', onDrop)
this.addEventListener('paste', onPaste)
this.addEventListener('change', onChange)
}

get directory(): boolean {
Expand Down Expand Up @@ -132,3 +133,18 @@ function onPaste(event: ClipboardEvent) {
container.attach(files)
event.preventDefault()
}

function onChange(event: Event) {
const container = event.currentTarget
if (!(container instanceof FileAttachmentElement)) return
const input = event.target
if (!(input instanceof HTMLInputElement)) return
const id = container.getAttribute('input')
if (!id || input.id !== id) return

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

container.attach(files)
input.value = ''
}
22 changes: 20 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ describe('file-attachment', function() {
})

describe('element', function() {
let fileAttachment
let fileAttachment, input
beforeEach(function() {
document.body.innerHTML = `<file-attachment></file-attachment>`
document.body.innerHTML = `
<file-attachment input="upload">
<input type="file" id="upload">
</file-attachment>`

fileAttachment = document.querySelector('file-attachment')
input = document.querySelector('input')
})

afterEach(function() {
Expand Down Expand Up @@ -106,6 +110,20 @@ describe('file-attachment', function() {
const event = await listener
assert.equal('test.png', event.detail.attachments[0].file.name)
})

it('attaches files via input', async function() {
const listener = once('file-attachment-accepted')

const dataTransfer = new DataTransfer()
const file = new File(['hubot'], 'test.png', {type: 'image/png'})
dataTransfer.items.add(file)
input.files = dataTransfer.files
input.dispatchEvent(new Event('change', {bubbles: true}))

const event = await listener
assert.equal('test.png', event.detail.attachments[0].file.name)
assert.equal(0, input.files.length)
})
})
})

Expand Down