Skip to content

Commit

Permalink
chore(lib): update Upload
Browse files Browse the repository at this point in the history
- `Upload` migrates to Vue 3. Changes sufficient to render the
  documentation page of `Upload` are made.
- In `src/components/upload/Upload.vue`,
    - `v-model` binding conforms to Vue 3,
        - `value` prop --> `model-value`
        - `input` event --> `update:modelValue`
    - `class` and `style` specified to an `Upload` component are
      copied to the root `label` of `Upload`, because they are not
      copied to the root element if `inheritAttrs` is `false` on
      Vue 3. A new computed value `classAndStyle` extracts `class`
      and `style` from `$attrs`.
    - Events to be emitted are listed in `emits`.
    - `disabled` is replaced with a new computed value
      `disabledOrUndefined` that takes `true` or `undefined`. This is
      introduced because setting a boolean attribute to `false` does
      not remove it on Vue 3, `null` or `undefined` has to be given
      to remove it.
    - Automatic ESLint fix is applied.
  • Loading branch information
kikuomax committed Jul 7, 2023
1 parent f5026d6 commit 6f8952e
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/components/upload/Upload.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<template>
<label class="upload control" :class="{'is-expanded' : expanded, 'is-rounded' : rounded}">
<label
class="upload control"
v-bind="classAndStyle"
:class="[{'is-expanded' : expanded, 'is-rounded' : rounded}]"
>
<template v-if="!dragDrop">
<slot/>
<slot />
</template>

<div
Expand All @@ -16,8 +20,9 @@
@dragover.prevent="updateDragDropFocus(true)"
@dragleave.prevent="updateDragDropFocus(false)"
@dragenter.prevent="updateDragDropFocus(true)"
@drop.prevent="onFileChange">
<slot/>
@drop.prevent="onFileChange"
>
<slot />
</div>

<input
Expand All @@ -26,8 +31,9 @@
v-bind="$attrs"
:multiple="multiple"
:accept="accept"
:disabled="disabled"
@change="onFileChange">
:disabled="disabledOrUndefined"
@change="onFileChange"
>
</label>
</template>

Expand All @@ -40,7 +46,7 @@ export default {
mixins: [FormElementMixin],
inheritAttrs: false,
props: {
value: {
modelValue: {
type: [Object, Function, File, Array]
},
multiple: Boolean,
Expand All @@ -64,21 +70,35 @@ export default {
default: false
}
},
emits: ['invalid', 'update:modelValue'],
data() {
return {
newValue: this.value,
newValue: this.modelValue,
dragDropFocus: false,
_elementRef: 'input'
}
},
computed: {
classAndStyle() {
return {
class: this.$attrs.class,
style: this.$attrs.style
}
},
disabledOrUndefined() {
// On Vue 3, setting a boolean attribute `false` does not remove it,
// `true` or `undefined` has to be given to remove it.
return this.disabled || undefined
}
},
watch: {
/**
* When v-model is changed:
* 1. Set internal value.
* 2. Reset internal input file value
* 3. If it's invalid, validate again.
*/
value(value) {
modelValue(value) {
this.newValue = value
if (!value || (Array.isArray(value) && value.length === 0)) {
this.$refs.input.value = null
Expand Down Expand Up @@ -130,7 +150,7 @@ export default {
}
if (!newValues) return
}
this.$emit('input', this.newValue)
this.$emit('update:modelValue', this.newValue)
!this.dragDrop && this.checkHtml5Validity()
},
Expand Down

0 comments on commit 6f8952e

Please sign in to comment.