Skip to content

Instantly share code, notes, and snippets.

@rhymes
Created March 27, 2018 08:07
Show Gist options
  • Save rhymes/1b0658e891956287de5aff833a7815a6 to your computer and use it in GitHub Desktop.
Save rhymes/1b0658e891956287de5aff833a7815a6 to your computer and use it in GitHub Desktop.
buefy app input file
<template>
<div class="container">
<b-field>
<b-tooltip
:label="tooltipLabel"
position="is-right"
type="is-dark"
animated>
<b-field>
<b-upload
:name="name"
v-model="files"
:accept="accept"
@input="onFileSelect">
<a class="button is-info">
<b-icon icon="upload" />
<span v-if="hasFile">
{{ fileName }}
</span>
<span v-else>{{ label }}</span>
</a>
</b-upload>
<p
v-if="hasFile"
class="control">
<button
title="Clear selected file"
class="button reset is-info is-outlined"
@click="onClearFileClick">
<b-icon icon="close" />
</button>
</p>
</b-field>
</b-tooltip>
</b-field>
</div>
</template>
<script>
export default {
name: 'AppInputFile',
props: {
name: {
type: String,
required: true,
},
accept: {
type: String,
default: null,
},
label: {
type: String,
default: 'Choose a file...',
},
tooltipLabel: {
type: String,
default: '',
},
},
data() {
return {
files: [],
}
},
computed: {
hasFile() {
return this.files && this.files.length > 0
},
fileName() {
if (this.hasFile) {
return this.files[0].name
}
return ''
},
},
methods: {
emitInputEvent(value) {
// send images to the parent component
this.$emit('input', value)
},
/* event handlers */
onClearFileClick() {
this.files = []
this.emitInputEvent(this.files)
},
onFileSelect() {
this.emitInputEvent(this.files)
},
},
}
</script>
<style scoped>
.container {
margin-bottom: 0.5rem;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment