|
| 1 | +<template> |
| 2 | + <div |
| 3 | + v-bind="ouiaProps" :class="[styles.fileUpload, { |
| 4 | + [styles.modifiers.dragHover]: dragActive, |
| 5 | + [styles.modifiers.loading]: loading, |
| 6 | + }]"> |
| 7 | + <div :class="styles.fileUploadFileSelect"> |
| 8 | + <pf-input-group> |
| 9 | + <pf-input-group-item fill> |
| 10 | + <pf-text-input |
| 11 | + :id="`${validId}-filename`" |
| 12 | + ref="filenameInputRef" |
| 13 | + read-only-variant="default" |
| 14 | + :disabled="disabled" |
| 15 | + :auto-validate="false" |
| 16 | + :name="name" |
| 17 | + :aria-label="filenameAriaLabel ?? (managedFilename ? 'Read only filename' : filenamePlaceholder)" |
| 18 | + :placeholder="filenamePlaceholder" |
| 19 | + :model-value="managedFilename" |
| 20 | + :aria-describedby="`${validId}-browse-button`" |
| 21 | + /> |
| 22 | + </pf-input-group-item> |
| 23 | + <pf-input-group-item> |
| 24 | + <pf-button |
| 25 | + :id="`${validId}-browse-button`" |
| 26 | + style="cursor: pointer" |
| 27 | + component="label" |
| 28 | + variant="control" |
| 29 | + :disabled="disabled" |
| 30 | + @click="$emit('browseButtonClick', $event)" |
| 31 | + > |
| 32 | + <input type="file" :accept="props.dataTypes?.join(',')" :name="name" :required="required" style="display: none" @change="setFile(($event.target as HTMLInputElement | undefined)?.files?.[0] ?? null)"> |
| 33 | + {{ browseButtonText }} |
| 34 | + </pf-button> |
| 35 | + </pf-input-group-item> |
| 36 | + <pf-input-group-item> |
| 37 | + <pf-button |
| 38 | + variant="control" |
| 39 | + :disabled="disabled || (clearButtonDisabled ?? (!managedFilename && !value))" |
| 40 | + @click="onClearButtonClick" |
| 41 | + > |
| 42 | + {{ clearButtonText }} |
| 43 | + </pf-button> |
| 44 | + </pf-input-group-item> |
| 45 | + </pf-input-group> |
| 46 | + </div> |
| 47 | + <div :class="styles.fileUploadFileDetails"> |
| 48 | + <pf-textarea |
| 49 | + v-if="!hideDefaultPreview && type === 'text'" |
| 50 | + :id="id" |
| 51 | + :disabled="disabled" |
| 52 | + :required="required" |
| 53 | + :readonly="readonly || (!!managedFilename && !allowEditingUploadedText)" |
| 54 | + resize-orientation="vertical" |
| 55 | + :validated="validated" |
| 56 | + :aria-label="ariaLabel" |
| 57 | + :model-value="typeof value === 'string' ? value : ''" |
| 58 | + :name="textareaName" |
| 59 | + :placeholder="textareaPlaceholder" |
| 60 | + @click="$emit('textAreaClick', $event)" |
| 61 | + @blur="$emit('textAreaBlur', $event)" |
| 62 | + /> |
| 63 | + <div v-if="managedLoading" :class="styles.fileUploadFileDetailsSpinner"> |
| 64 | + <pf-spinner size="lg" :aria-valuetext="spinnerAriaValueText" /> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + <slot /> |
| 68 | + </div> |
| 69 | +</template> |
| 70 | + |
| 71 | +<script lang="ts" setup> |
| 72 | +import styles from '@patternfly/react-styles/css/components/FileUpload/file-upload'; |
| 73 | +import { computed, useTemplateRef, type HTMLAttributes } from 'vue'; |
| 74 | +import { useOUIAProps, type OUIAProps } from '../helpers/ouia'; |
| 75 | +import PfInputGroup from './InputGroup/InputGroup.vue'; |
| 76 | +import PfInputGroupItem from './InputGroup/InputGroupItem.vue'; |
| 77 | +import PfTextInput from './TextInput.vue'; |
| 78 | +import PfButton from './Button.vue'; |
| 79 | +import PfTextarea from './Textarea.vue'; |
| 80 | +import PfSpinner from './Spinner.vue'; |
| 81 | +import { getUniqueId } from '../util'; |
| 82 | +import { useManagedProp } from '../use'; |
| 83 | +import { useDropZone } from '@vueuse/core'; |
| 84 | +
|
| 85 | +defineOptions({ |
| 86 | + name: 'PfFileUpload', |
| 87 | +}); |
| 88 | +
|
| 89 | +export interface Props extends OUIAProps, /* @vue-ignore */ HTMLAttributes { |
| 90 | + /** Unique id for the text area. Also used to generate ids for accessible labels. */ |
| 91 | + id?: string; |
| 92 | + /** Flag to allow editing of a text file's contents after it is selected from disk. */ |
| 93 | + allowEditingUploadedText?: boolean; |
| 94 | + /** Aria-label for the text area. */ |
| 95 | + ariaLabel?: string; |
| 96 | + /** Text for the browse button. */ |
| 97 | + browseButtonText?: string; |
| 98 | + /** Text for the clear button. */ |
| 99 | + clearButtonText?: string; |
| 100 | + /** Value to be shown in the read-only filename field. */ |
| 101 | + filename?: string; |
| 102 | + /** Aria-label for the read-only filename field. */ |
| 103 | + filenameAriaLabel?: string; |
| 104 | + /** Placeholder string to display in the empty filename field. */ |
| 105 | + filenamePlaceholder?: string; |
| 106 | + /** Flag to hide the built-in preview of the file (where available). If true, you can use |
| 107 | + * the children property to render an alternate preview. |
| 108 | + */ |
| 109 | + hideDefaultPreview?: boolean; |
| 110 | + /** Name property for the text input. */ |
| 111 | + name?: string; |
| 112 | + /** Flag to disable the clear button. */ |
| 113 | + clearButtonDisabled?: boolean; |
| 114 | + /** Flag to show if the field is disabled. */ |
| 115 | + disabled?: boolean; |
| 116 | + /** Flag to show if a file is being loaded. */ |
| 117 | + loading?: boolean; |
| 118 | + /** Flag to show if the field is read only. */ |
| 119 | + readonly?: boolean; |
| 120 | + /** Flag to show if the field is required. */ |
| 121 | + required?: boolean; |
| 122 | + /** Aria-valuetext for the loading spinner. */ |
| 123 | + spinnerAriaValueText?: string; |
| 124 | + /** What type of file. Determines what is is expected by the value property (a string for |
| 125 | + * 'text' and 'dataURL', or a File object otherwise). |
| 126 | + */ |
| 127 | + type?: 'text' | 'dataURL' | 'arrayBuffer'; |
| 128 | + /** Value to indicate if the field is modified to show that validation state. |
| 129 | + * If set to success, field will be modified to indicate valid state. |
| 130 | + * If set to error, field will be modified to indicate error state. |
| 131 | + */ |
| 132 | + validated?: 'success' | 'error' | 'default'; |
| 133 | + /** Value of the file's contents (string if text file, ArrayBuffer object otherwise). */ |
| 134 | + modelValue?: string | ArrayBuffer; |
| 135 | + /** Flag to show if a file is being dragged over the file upload field. */ |
| 136 | + dragActive?: boolean; |
| 137 | + /** Name property for the preview textarea. */ |
| 138 | + textareaName?: string; |
| 139 | + /** Placeholder string to display in the empty text area field. */ |
| 140 | + textareaPlaceholder?: string; |
| 141 | + /** Allowed data types, if not set, all data types are allowed. */ |
| 142 | + dataTypes?: readonly string[]; |
| 143 | +} |
| 144 | +
|
| 145 | +const props = withDefaults(defineProps<Props>(), { |
| 146 | + type: 'text', |
| 147 | + validated: 'default', |
| 148 | + ariaLabel: 'File upload', |
| 149 | + filenamePlaceholder: 'Drag a file here or browse to upload', |
| 150 | + browseButtonText: 'Browse...', |
| 151 | + clearButtonText: 'Clear', |
| 152 | + clearButtonDisabled: undefined, |
| 153 | + loading: undefined, |
| 154 | +}); |
| 155 | +const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe}); |
| 156 | +
|
| 157 | +const emit = defineEmits<{ |
| 158 | + (name: 'onUpdate:modelValue', value: string | File): void; |
| 159 | + (name: 'browseButtonClick', e: PointerEvent): void; |
| 160 | + (name: 'clearButtonClick', e: PointerEvent): void; |
| 161 | + (name: 'textAreaBlur', e: Event): void; |
| 162 | + (name: 'textAreaClick', e: MouseEvent): void; |
| 163 | + (name: 'fileInputChange', file: File | null): void; |
| 164 | + (name: 'readStarted', file: File): void; |
| 165 | + (name: 'readFinished', file: File, content: string | ArrayBuffer | null): void; |
| 166 | +}>(); |
| 167 | +
|
| 168 | +defineSlots<{ |
| 169 | + default?: (props?: Record<never, never>) => any; |
| 170 | +}>(); |
| 171 | +
|
| 172 | +const value = useManagedProp<string | ArrayBuffer | null>('modelValue', null); |
| 173 | +const managedFilename = useManagedProp<string | null>('filename', null); |
| 174 | +const managedLoading = useManagedProp('loading', false); |
| 175 | +const validId = computed(() => props.id || getUniqueId()); |
| 176 | +const filenameInputRef = useTemplateRef('filenameInputRef'); |
| 177 | +
|
| 178 | +useDropZone(() => filenameInputRef.value?.$el, { |
| 179 | + dataTypes: props.dataTypes, |
| 180 | + multiple: false, |
| 181 | + async onDrop(files) { |
| 182 | + if (!files?.length) { |
| 183 | + return; |
| 184 | + } |
| 185 | + await setFile(files[0]); |
| 186 | + }, |
| 187 | +}); |
| 188 | +
|
| 189 | +function readFile(fileHandle: File) { |
| 190 | + return new Promise((resolve: (value: string | ArrayBuffer | null) => void, reject: (reason: DOMException | string | null) => void) => { |
| 191 | + const reader = new FileReader(); |
| 192 | + reader.onload = () => resolve(reader.result); |
| 193 | + reader.onerror = () => reject(reader.error); |
| 194 | + if (props.type === 'text') { |
| 195 | + reader.readAsText(fileHandle); |
| 196 | + } else if (props.type === 'dataURL') { |
| 197 | + reader.readAsDataURL(fileHandle); |
| 198 | + } else if (props.type === 'arrayBuffer') { |
| 199 | + reader.readAsArrayBuffer(fileHandle); |
| 200 | + } else { |
| 201 | + reject('unknown type'); |
| 202 | + } |
| 203 | + }); |
| 204 | +} |
| 205 | +
|
| 206 | +async function setFile(file: File | null) { |
| 207 | + emit('fileInputChange', file); |
| 208 | + managedFilename.value = file?.name ?? null; |
| 209 | +
|
| 210 | + if (!file) { |
| 211 | + value.value = null; |
| 212 | + return; |
| 213 | + } |
| 214 | +
|
| 215 | + if (props.type !== 'text' && props.type !== 'dataURL') { |
| 216 | + return; |
| 217 | + } |
| 218 | +
|
| 219 | + emit('readStarted', file); |
| 220 | + managedLoading.value = true; |
| 221 | + let content = null; |
| 222 | + try { |
| 223 | + content = await readFile(file); |
| 224 | + } finally { |
| 225 | + managedLoading.value = false; |
| 226 | + emit('readFinished', file, content); |
| 227 | + value.value = content; |
| 228 | + } |
| 229 | +} |
| 230 | +
|
| 231 | +function onClearButtonClick(e: PointerEvent) { |
| 232 | + emit('clearButtonClick', e); |
| 233 | + setFile(null); |
| 234 | +} |
| 235 | +</script> |
0 commit comments