|
| 1 | +# Form File |
| 2 | + |
| 3 | +<ClientOnly> |
| 4 | + <Teleport to=".bd-toc"> |
| 5 | + |
| 6 | +[[toc]] |
| 7 | + |
| 8 | + </Teleport> |
| 9 | +</ClientOnly> |
| 10 | + |
| 11 | +<div class="lead mb-5"> |
| 12 | + |
| 13 | +File input control that supports single and multiple file modes |
| 14 | + |
| 15 | +</div> |
| 16 | + |
| 17 | +<BAlert :model-value="true" variant="danger"> |
| 18 | +The current variation is subject to change pre v1.0. The implementation may change to become closer to the Bootstrap-vue implementation based on feedback <BLink target="_blank" href="https://github.com/bootstrap-vue-next/bootstrap-vue-next/discussions/1213" rel="noopener">vote here</BLink> |
| 19 | +</BAlert> |
| 20 | + |
| 21 | +## Single File Mode |
| 22 | + |
| 23 | +The default behavior is single file mode. While using single file mode the `modelValue` will be a single `File` object |
| 24 | + |
| 25 | +<HighlightCard> |
| 26 | + <BFormFile v-model="first" label="Hello!" /> |
| 27 | + <div class="mt-3"> |
| 28 | + File: <strong>{{ first }}</strong> |
| 29 | + </div> |
| 30 | + <template #html> |
| 31 | + |
| 32 | +```vue |
| 33 | +<template> |
| 34 | + <BFormFile v-model="file" label="Hello!" /> |
| 35 | + <div class="mt-3"> |
| 36 | + Files: <strong>{{ file }}</strong> |
| 37 | + </div> |
| 38 | +</template> |
| 39 | +
|
| 40 | +<script setup lang="ts"> |
| 41 | +const file = ref<null | File>(null) |
| 42 | +</script> |
| 43 | +``` |
| 44 | + |
| 45 | + </template> |
| 46 | +</HighlightCard> |
| 47 | + |
| 48 | +## Multiple File Mode |
| 49 | + |
| 50 | +To toggle multiple file mode, simply set the `multiple` prop to `true`. While in multiple file mode, the `modelValue` will be a `File[]`, even if only one file is selected |
| 51 | + |
| 52 | +<HighlightCard> |
| 53 | + <BFormFile v-model="second" multiple /> |
| 54 | + <div class="mt-3"> |
| 55 | + Files: <strong>{{ second }}</strong> |
| 56 | + </div> |
| 57 | + <template #html> |
| 58 | + |
| 59 | +```vue |
| 60 | +<template> |
| 61 | + <BFormFile v-model="files" multiple /> |
| 62 | + <div class="mt-3"> |
| 63 | + Files: <strong>{{ files }}</strong> |
| 64 | + </div> |
| 65 | +</template> |
| 66 | +
|
| 67 | +<script setup lang="ts"> |
| 68 | +const files = ref<null | File[]>(null) |
| 69 | +</script> |
| 70 | +``` |
| 71 | + |
| 72 | + </template> |
| 73 | +</HighlightCard> |
| 74 | + |
| 75 | +## Limiting to certain file types |
| 76 | + |
| 77 | +You can limit the file types by setting the `accept` prop. The `accept` attribute is a csv list of acceptable types. This can be a `string` or `string[]`. If a `string[]` is inputted, it simply gets joined as a csv list |
| 78 | + |
| 79 | +<HighlightCard> |
| 80 | + <BFormFile v-model="third" accept="image/*" /> |
| 81 | + <div class="mt-3"> |
| 82 | + File: <strong>{{ third }}</strong> |
| 83 | + </div> |
| 84 | + <template #html> |
| 85 | + |
| 86 | +```vue |
| 87 | +<template> |
| 88 | + <BFormFile v-model="file" accept="image/*" /> |
| 89 | + <div class="mt-3"> |
| 90 | + Files: <strong>{{ file }}</strong> |
| 91 | + </div> |
| 92 | +</template> |
| 93 | +
|
| 94 | +<script setup lang="ts"> |
| 95 | +const file = ref<null | File>(null) |
| 96 | +</script> |
| 97 | +``` |
| 98 | + |
| 99 | + </template> |
| 100 | +</HighlightCard> |
| 101 | + |
| 102 | +## Drag and Drop Support |
| 103 | + |
| 104 | +Drag and drop support uses the browsers default behavior. You can explicitly disable drag and drop by using the `noDrop` prop |
| 105 | + |
| 106 | +<HighlightCard> |
| 107 | + <BFormFile v-model="fourth" no-drop /> |
| 108 | + <div class="mt-3"> |
| 109 | + File: <strong>{{ fourth }}</strong> |
| 110 | + </div> |
| 111 | + <template #html> |
| 112 | + |
| 113 | +```vue |
| 114 | +<template> |
| 115 | + <BFormFile v-model="file" no-drop /> |
| 116 | + <div class="mt-3"> |
| 117 | + Files: <strong>{{ file }}</strong> |
| 118 | + </div> |
| 119 | +</template> |
| 120 | +
|
| 121 | +<script setup lang="ts"> |
| 122 | +const file = ref<null | File>(null) |
| 123 | +</script> |
| 124 | +``` |
| 125 | + |
| 126 | + </template> |
| 127 | +</HighlightCard> |
| 128 | + |
| 129 | +## Sizing |
| 130 | + |
| 131 | +You can modify the size of the form control by using the `size` prop |
| 132 | + |
| 133 | +<HighlightCard> |
| 134 | + <BFormFile class="mt-3" size="sm" /> |
| 135 | + <BFormFile class="mt-3" /> |
| 136 | + <BFormFile class="mt-3" size="lg" /> |
| 137 | + |
| 138 | +<template #html> |
| 139 | + |
| 140 | +```vue-html |
| 141 | +<BFormFile class="mt-3" size="sm" /> |
| 142 | +<BFormFile class="mt-3" /> |
| 143 | +<BFormFile class="mt-3" size="lg" /> |
| 144 | +``` |
| 145 | + |
| 146 | + </template> |
| 147 | +</HighlightCard> |
| 148 | + |
| 149 | +## Label |
| 150 | + |
| 151 | +You can add a label above the input by using the `label` prop or the `label` slot |
| 152 | + |
| 153 | +<HighlightCard> |
| 154 | + <BFormFile label="I'm first!" /> |
| 155 | + <BFormFile> |
| 156 | + <template #label> |
| 157 | + I'm second! |
| 158 | + </template> |
| 159 | + </BFormFile> |
| 160 | + |
| 161 | +<template #html> |
| 162 | + |
| 163 | +```vue-html |
| 164 | +<BFormFile class="mt-3" label="I'm first!" /> |
| 165 | +<BFormFile class="mt-3"> |
| 166 | + <template #label> |
| 167 | + I'm second! |
| 168 | + </template> |
| 169 | +</BFormFile> |
| 170 | +``` |
| 171 | + |
| 172 | + </template> |
| 173 | +</HighlightCard> |
| 174 | + |
| 175 | +## Directory Mode |
| 176 | + |
| 177 | +By adding the `directory` prop, a user can select directories instead of files |
| 178 | + |
| 179 | +<BAlert variant="danger" :model-value="true"> |
| 180 | + Directory mode is a non-standard attribute in the HTML spec. All major browsers have chosen too support it, but it may not function correctly for browsers that have chosen not to implement it. Use with caution |
| 181 | +</BAlert> |
| 182 | + |
| 183 | +### Example to be Written |
| 184 | + |
| 185 | +## Autofocus |
| 186 | + |
| 187 | +If you set the `autofocus` prop to true, the input will be focused when the component is inserted |
| 188 | + |
| 189 | +<HighlightCard> |
| 190 | + <BFormFile class="mt-3" autofocus /> |
| 191 | + |
| 192 | +<template #html> |
| 193 | + |
| 194 | +```vue-html |
| 195 | +<BFormFile class="mt-3" autofocus /> |
| 196 | +``` |
| 197 | + |
| 198 | + </template> |
| 199 | +</HighlightCard> |
| 200 | + |
| 201 | +## Contextual State |
| 202 | + |
| 203 | +You can use the `state` prop to provide visual feedback on the state of the input |
| 204 | + |
| 205 | +<HighlightCard> |
| 206 | + <BFormFile class="mt-3" :state="false" /> |
| 207 | + <BFormFile class="mt-3" :state="true" /> |
| 208 | + |
| 209 | +<template #html> |
| 210 | + |
| 211 | +```vue-html |
| 212 | +<BFormFile class="mt-3" :state="false" /> |
| 213 | +<BFormFile class="mt-3" :state="true" /> |
| 214 | +``` |
| 215 | + |
| 216 | + </template> |
| 217 | +</HighlightCard> |
| 218 | + |
| 219 | +## Modifying the file selection |
| 220 | + |
| 221 | +With inputs that are of type `file`, the value is strictly `uni-directional`. Meaning that you cannot change the value of the input via JavaScript. You can change the value of the `v-model`, and this will work for an "outside view", however, the actual `input` element will not have its [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) changed. This is for security reasons as a malicious script could attempt to read and steal documents |
| 222 | + |
| 223 | +<ComponentReference :data="data" /> |
| 224 | + |
| 225 | +<script setup lang="ts"> |
| 226 | +import {data} from '../../data/components/formFile.data' |
| 227 | +import ComponentReference from '../../components/ComponentReference.vue' |
| 228 | +import HighlightCard from '../../components/HighlightCard.vue' |
| 229 | +import {BFormFile, BAlert, BLink} from 'bootstrap-vue-next' |
| 230 | +import {ref} from 'vue' |
| 231 | + |
| 232 | +const first = ref(null) |
| 233 | +const second = ref(null) |
| 234 | +const third = ref(null) |
| 235 | +const fourth = ref(null) |
| 236 | +</script> |
0 commit comments