feat: better file input chooser
This commit is contained in:
@@ -54,13 +54,12 @@
|
|||||||
/>
|
/>
|
||||||
<div v-if="param.isFile" class="file-chips-container hide-scrollbar">
|
<div v-if="param.isFile" class="file-chips-container hide-scrollbar">
|
||||||
<div class="space-x-2 file-chips-wrapper">
|
<div class="space-x-2 file-chips-wrapper">
|
||||||
<SmartDeletableChip
|
<SmartFileChip
|
||||||
v-for="(file, fileIndex) in param.value"
|
v-for="(file, fileIndex) in param.value"
|
||||||
:key="`param-${index}-file-${fileIndex}`"
|
:key="`param-${index}-file-${fileIndex}`"
|
||||||
@chip-delete="chipDelete(index, fileIndex)"
|
|
||||||
>
|
>
|
||||||
{{ file.name }}
|
{{ file.name }}
|
||||||
</SmartDeletableChip>
|
</SmartFileChip>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<span v-else class="flex flex-1">
|
<span v-else class="flex flex-1">
|
||||||
@@ -85,21 +84,17 @@
|
|||||||
/>
|
/>
|
||||||
</span>
|
</span>
|
||||||
<span>
|
<span>
|
||||||
<label for="attachment" class="p-0">
|
<label :for="`attachment${index}`" class="p-0">
|
||||||
<ButtonSecondary
|
<input
|
||||||
class="w-full"
|
:id="`attachment${index}`"
|
||||||
svg="paperclip"
|
:ref="`attachment${index}`"
|
||||||
@click.native="$refs.attachment[index].click()"
|
:name="`attachment${index}`"
|
||||||
|
type="file"
|
||||||
|
multiple
|
||||||
|
class="p-1 transition cursor-pointer file:transition file:cursor-pointer text-secondaryLight hover:text-secondaryDark file:mr-4 file:py-1 file:px-4 file:rounded file:border-0 file:text-tiny text-tiny file:text-secondary hover:file:text-secondaryDark file:bg-primaryLight hover:file:bg-primaryDark"
|
||||||
|
@change="setRequestAttachment(index, param, $event)"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<input
|
|
||||||
ref="attachment"
|
|
||||||
class="input"
|
|
||||||
name="attachment"
|
|
||||||
type="file"
|
|
||||||
multiple
|
|
||||||
@change="setRequestAttachment(index, param, $event)"
|
|
||||||
/>
|
|
||||||
</span>
|
</span>
|
||||||
<span>
|
<span>
|
||||||
<ButtonSecondary
|
<ButtonSecondary
|
||||||
@@ -163,8 +158,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script setup lang="ts">
|
||||||
import { defineComponent, onMounted, Ref, watch } from "@nuxtjs/composition-api"
|
import { onMounted, Ref, watch } from "@nuxtjs/composition-api"
|
||||||
import { FormDataKeyValue } from "@hoppscotch/data"
|
import { FormDataKeyValue } from "@hoppscotch/data"
|
||||||
import { pluckRef } from "~/helpers/utils/composables"
|
import { pluckRef } from "~/helpers/utils/composables"
|
||||||
import {
|
import {
|
||||||
@@ -175,87 +170,66 @@ import {
|
|||||||
useRESTRequestBody,
|
useRESTRequestBody,
|
||||||
} from "~/newstore/RESTSession"
|
} from "~/newstore/RESTSession"
|
||||||
|
|
||||||
export default defineComponent({
|
const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref<
|
||||||
setup() {
|
FormDataKeyValue[]
|
||||||
const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref<
|
>
|
||||||
FormDataKeyValue[]
|
|
||||||
>
|
|
||||||
|
|
||||||
const addBodyParam = () => {
|
const addBodyParam = () => {
|
||||||
addFormDataEntry({ key: "", value: "", active: true, isFile: false })
|
addFormDataEntry({ key: "", value: "", active: true, isFile: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateBodyParam = (index: number, entry: FormDataKeyValue) => {
|
const updateBodyParam = (index: number, entry: FormDataKeyValue) => {
|
||||||
updateFormDataEntry(index, entry)
|
updateFormDataEntry(index, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteBodyParam = (index: number) => {
|
const deleteBodyParam = (index: number) => {
|
||||||
deleteFormDataEntry(index)
|
deleteFormDataEntry(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
const clearContent = () => {
|
const clearContent = () => {
|
||||||
deleteAllFormDataEntries()
|
deleteAllFormDataEntries()
|
||||||
}
|
}
|
||||||
|
|
||||||
const chipDelete = (paramIndex: number, fileIndex: number) => {
|
const setRequestAttachment = (
|
||||||
const entry = bodyParams.value[paramIndex]
|
index: number,
|
||||||
if (entry.isFile) {
|
entry: FormDataKeyValue,
|
||||||
entry.value.splice(fileIndex, 1)
|
event: InputEvent
|
||||||
if (entry.value.length === 0) {
|
) => {
|
||||||
updateFormDataEntry(paramIndex, {
|
// check if file exists or not
|
||||||
...entry,
|
if ((event.target as HTMLInputElement).files?.length === 0) {
|
||||||
isFile: false,
|
updateFormDataEntry(index, {
|
||||||
value: "",
|
...entry,
|
||||||
})
|
isFile: false,
|
||||||
return
|
value: "",
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
updateFormDataEntry(paramIndex, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
const setRequestAttachment = (
|
|
||||||
index: number,
|
|
||||||
entry: FormDataKeyValue,
|
|
||||||
event: InputEvent
|
|
||||||
) => {
|
|
||||||
const fileEntry: FormDataKeyValue = {
|
|
||||||
...entry,
|
|
||||||
isFile: true,
|
|
||||||
value: Array.from((event.target as HTMLInputElement).files!),
|
|
||||||
}
|
|
||||||
updateFormDataEntry(index, fileEntry)
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(
|
|
||||||
bodyParams,
|
|
||||||
() => {
|
|
||||||
if (
|
|
||||||
bodyParams.value.length > 0 &&
|
|
||||||
(bodyParams.value[bodyParams.value.length - 1].key !== "" ||
|
|
||||||
bodyParams.value[bodyParams.value.length - 1].value !== "")
|
|
||||||
)
|
|
||||||
addBodyParam()
|
|
||||||
},
|
|
||||||
{ deep: true }
|
|
||||||
)
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
if (!bodyParams.value?.length) {
|
|
||||||
addBodyParam()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
const fileEntry: FormDataKeyValue = {
|
||||||
bodyParams,
|
...entry,
|
||||||
addBodyParam,
|
isFile: true,
|
||||||
updateBodyParam,
|
value: Array.from((event.target as HTMLInputElement).files!),
|
||||||
deleteBodyParam,
|
}
|
||||||
clearContent,
|
updateFormDataEntry(index, fileEntry)
|
||||||
setRequestAttachment,
|
}
|
||||||
chipDelete,
|
|
||||||
}
|
watch(
|
||||||
|
bodyParams,
|
||||||
|
() => {
|
||||||
|
if (
|
||||||
|
bodyParams.value.length > 0 &&
|
||||||
|
(bodyParams.value[bodyParams.value.length - 1].key !== "" ||
|
||||||
|
bodyParams.value[bodyParams.value.length - 1].value !== "")
|
||||||
|
)
|
||||||
|
addBodyParam()
|
||||||
},
|
},
|
||||||
|
{ deep: true }
|
||||||
|
)
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (!bodyParams.value?.length) {
|
||||||
|
addBodyParam()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -268,8 +242,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
.file-chips-wrapper {
|
.file-chips-wrapper {
|
||||||
@apply flex;
|
@apply flex;
|
||||||
@apply px-4;
|
@apply p-1;
|
||||||
@apply py-1;
|
|
||||||
@apply w-0;
|
@apply w-0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<span class="chip">
|
<span class="chip">
|
||||||
<i class="opacity-75 material-icons">attachment</i>
|
<i class="opacity-75 material-icons">attachment</i>
|
||||||
<span class="px-2 truncate max-w-64"><slot></slot></span>
|
<span class="px-2 truncate max-w-32"><slot></slot></span>
|
||||||
<ButtonSecondary
|
|
||||||
class="rounded close-button"
|
|
||||||
svg="x"
|
|
||||||
@click.native="$emit('chip-delete')"
|
|
||||||
/>
|
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -21,8 +16,4 @@
|
|||||||
@apply bg-transparent;
|
@apply bg-transparent;
|
||||||
@apply border border-divider;
|
@apply border border-divider;
|
||||||
}
|
}
|
||||||
|
|
||||||
.close-button {
|
|
||||||
@apply p-0.5;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
Reference in New Issue
Block a user