Skip to content

Commit

Permalink
Fix [Upload]: Vue Compat: deprecation INSTANCE_ATTRS_CLASS_STYLE (#16) (
Browse files Browse the repository at this point in the history
#219)

* feat(lib): add compat-fallthrough prop to Upload

- `Upload` introduces a new prop `compat-fallthrough` which determines
  if the `class`, `style`, and `id` attributes are applied to the root
  `<label>` element or the underlying `<input>` element. Since Vue 3
  changed the fallthrough behavior, `Upload` became incompatible with
  that of Buefy for Vue 2. The default value of this prop is given by
  the `defaultCompatFallthrough` config option that is `true` by default
  (compatible with Buefy for Vue 2).

  Replaces the `classAndStyle` computed value with `rootAttrs`, which
  did not address the `id` attribute.

* test(lib): test Upload fallthrough behavior

- Adds test cases for the `compat-fallthrough` prop of `Upload`

* chore(docs): explain compat-fallthrough prop of Upload

* docs(CHANGELOG): explain compat-fallthrough prop of Upload
  • Loading branch information
kikuomax authored Mar 29, 2024
1 parent 1ad2aaf commit b28f56f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

* `Upload`:

TBD
If `compat-fallthrough` is `true`, the attributes fall through to the root `<label>` element, otherwise to the underlying `<input>` element.

* `CarouselItem`, `StepItem`, and `TabItem` introduce a new prop `order`, which determines the order of each child item.
By default, the order of each child item is determined by the sequence in which each child item is mounted.
Expand Down
41 changes: 41 additions & 0 deletions packages/buefy-next/src/components/upload/Upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,45 @@ describe('BUpload', () => {
it('render correctly', () => {
expect(wrapper.html()).toMatchSnapshot()
})

describe('with fallthrough attributes', () => {
const attrs = {
class: 'fallthrough-class',
style: 'font-size: 2rem;',
id: 'fallthrough-id'
}

it('should apply class, style, and id to the root <label> element if compatFallthrough is true (default)', () => {
const wrapper = shallowMount(BUpload, { attrs })

const root = wrapper.find('label.upload')
expect(root.classes(attrs.class)).toBe(true)
expect(root.attributes('style')).toBe(attrs.style)
expect(root.attributes('id')).toBe(attrs.id)

const input = wrapper.find({ ref: 'input' })
expect(input.classes(attrs.class)).toBe(false)
expect(input.attributes('style')).toBeUndefined()
expect(input.attributes('id')).toBeUndefined()
})

it('should apply class, style, and id to the underlying <input> element if compatFallthrough is false', () => {
const wrapper = shallowMount(BUpload, {
attrs,
props: {
compatFallthrough: false
}
})

const root = wrapper.find('label.upload')
expect(root.classes(attrs.class)).toBe(false)
expect(root.attributes('style')).toBeUndefined()
expect(root.attributes('id')).toBeUndefined()

const input = wrapper.find({ ref: 'input' })
expect(input.classes(attrs.class)).toBe(true)
expect(input.attributes('style')).toBe(attrs.style)
expect(input.attributes('id')).toBe(attrs.id)
})
})
})
14 changes: 4 additions & 10 deletions packages/buefy-next/src/components/upload/Upload.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<label
class="upload control"
v-bind="classAndStyle"
v-bind="rootAttrs"
:class="[{'is-expanded' : expanded, 'is-rounded' : rounded}]"
>
<template v-if="!dragDrop">
Expand All @@ -28,7 +28,7 @@
<input
ref="input"
type="file"
v-bind="$attrs"
v-bind="fallthroughAttrs"
:multiple="multiple"
:accept="accept"
:disabled="disabledOrUndefined"
Expand All @@ -38,13 +38,13 @@
</template>

<script>
import CompatFallthroughMixin from '../../utils/CompatFallthroughMixin'
import FormElementMixin from '../../utils/FormElementMixin'
import { File } from '../../utils/ssr'
export default {
name: 'BUpload',
mixins: [FormElementMixin],
inheritAttrs: false,
mixins: [CompatFallthroughMixin, FormElementMixin],
props: {
modelValue: {
type: [Object, Function, File, Array]
Expand Down Expand Up @@ -79,12 +79,6 @@ export default {
}
},
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.
Expand Down
7 changes: 7 additions & 0 deletions packages/docs/src/pages/components/upload/api/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export default [
values: '—',
default: '<code>false</code>'
},
{
name: '<code>compat-fallthrough</code>',
description: 'Whether the <code>class</code>, <code>style</code>, and <code>id</code> attributes are applied to the root &lt;label&gt; element or the underlying &lt;input&gt; element. If <code>true</code>, they are applied to the root &lt;label&gt; element, which is compatible with Buefy for Vue 2.',
type: 'Boolean',
values: '-',
default: '<code>true</code>. Can be changed via the <code>defaultCompatFallthrough</code> config option.'
}
],
events: [
{
Expand Down

0 comments on commit b28f56f

Please sign in to comment.