-
Notifications
You must be signed in to change notification settings - Fork 25
/
file-select.vue
53 lines (51 loc) · 1.26 KB
/
file-select.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<template>
<div class="field">
<label v-if="label" class="label">{{ label }}</label>
<label class="file-select">
<!-- We can't use a normal button element here, as it would become the target of the label. -->
<div class="button">
<!-- Display the filename if a file has been selected. -->
<span v-if="value">Selected File: {{ value.name }}</span>
<span v-else>Select File</span>
</div>
<!-- We hide this file input. -->
<input type="file" @change="handleFileChange">
</label>
<div :class="['pt-5', fileClass]">
<img v-if="image" :src="image">
</div>
</div>
</template>
<script lang="ts">
export default {
props: {
value: File,
label: {
type: String,
required: false
},
fileClass: {
type: String,
required: false,
default: 'game-cover'
}
},
data() {
return {
image: null
};
},
methods: {
handleFileChange(e) {
let file = e.target.files[0];
var reader = new FileReader();
reader.onloadend = () => {
this.image = reader.result;
};
reader.readAsDataURL(file);
// Whenever the file changes, emit the 'input' event with the file data.
this.$emit('input', file);
}
}
};
</script>