|
17 | 17 | </label> |
18 | 18 | </template> |
19 | 19 |
|
20 | | -<script> |
21 | | -import { fileIsValid } from '~/helpers/fileUtils.js' |
| 20 | +<script setup lang="ts"> |
| 21 | +import { useFormatBytes } from '@modrinth/ui' |
| 22 | +import { fileIsValid } from '@modrinth/utils' |
| 23 | +import { ref } from 'vue' |
22 | 24 |
|
23 | | -export default { |
24 | | - components: {}, |
25 | | - props: { |
26 | | - prompt: { |
27 | | - type: String, |
28 | | - default: 'Select file', |
29 | | - }, |
30 | | - multiple: { |
31 | | - type: Boolean, |
32 | | - default: false, |
33 | | - }, |
34 | | - accept: { |
35 | | - type: String, |
36 | | - default: null, |
37 | | - }, |
| 25 | +const props = withDefaults( |
| 26 | + defineProps<{ |
| 27 | + prompt?: string |
| 28 | + multiple?: boolean |
| 29 | + accept?: string |
38 | 30 | /** |
39 | 31 | * The max file size in bytes |
40 | 32 | */ |
41 | | - maxSize: { |
42 | | - type: Number, |
43 | | - default: null, |
44 | | - }, |
45 | | - showIcon: { |
46 | | - type: Boolean, |
47 | | - default: true, |
48 | | - }, |
49 | | - shouldAlwaysReset: { |
50 | | - type: Boolean, |
51 | | - default: false, |
52 | | - }, |
53 | | - longStyle: { |
54 | | - type: Boolean, |
55 | | - default: false, |
56 | | - }, |
57 | | - disabled: { |
58 | | - type: Boolean, |
59 | | - default: false, |
60 | | - }, |
| 33 | + maxSize?: number | null |
| 34 | + showIcon?: boolean |
| 35 | + shouldAlwaysReset?: boolean |
| 36 | + longStyle?: boolean |
| 37 | + disabled?: boolean |
| 38 | + }>(), |
| 39 | + { |
| 40 | + prompt: 'Select file', |
| 41 | + multiple: false, |
| 42 | + showIcon: true, |
| 43 | + shouldAlwaysReset: false, |
| 44 | + longStyle: false, |
| 45 | + disabled: false, |
61 | 46 | }, |
62 | | - emits: ['change'], |
63 | | - data() { |
64 | | - return { |
65 | | - files: [], |
66 | | - } |
67 | | - }, |
68 | | - methods: { |
69 | | - addFiles(files, shouldNotReset) { |
70 | | - if (!shouldNotReset || this.shouldAlwaysReset) { |
71 | | - this.files = files |
72 | | - } |
| 47 | +) |
73 | 48 |
|
74 | | - const validationOptions = { maxSize: this.maxSize, alertOnInvalid: true } |
75 | | - this.files = [...this.files].filter((file) => fileIsValid(file, validationOptions)) |
| 49 | +const emit = defineEmits<{ change: [files: File[]] }>() |
76 | 50 |
|
77 | | - if (this.files.length > 0) { |
78 | | - this.$emit('change', this.files) |
79 | | - } |
80 | | - }, |
81 | | - handleDrop(e) { |
82 | | - this.addFiles(e.dataTransfer.files) |
83 | | - }, |
84 | | - handleChange(e) { |
85 | | - this.addFiles(e.target.files) |
86 | | - }, |
87 | | - }, |
| 51 | +const formatBytes = useFormatBytes() |
| 52 | +
|
| 53 | +const files = ref<File[]>([]) |
| 54 | +
|
| 55 | +function addFiles(incoming: FileList, shouldNotReset = false) { |
| 56 | + if (!shouldNotReset || props.shouldAlwaysReset) { |
| 57 | + files.value = Array.from(incoming) |
| 58 | + } |
| 59 | + const validationOptions = { maxSize: props.maxSize, alertOnInvalid: true } |
| 60 | + files.value = files.value.filter((file) => fileIsValid(file, validationOptions, formatBytes)) |
| 61 | + if (files.value.length > 0) { |
| 62 | + emit('change', files.value) |
| 63 | + } |
| 64 | +} |
| 65 | +
|
| 66 | +function handleDrop(e: DragEvent) { |
| 67 | + addFiles(e.dataTransfer!.files) |
| 68 | +} |
| 69 | +
|
| 70 | +function handleChange(e: Event) { |
| 71 | + const input = e.target as HTMLInputElement |
| 72 | + if (!input.files) return |
| 73 | + addFiles(input.files) |
88 | 74 | } |
89 | 75 | </script> |
90 | 76 |
|
|
0 commit comments