Skip to content

Commit

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

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

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

* test(lib): test Datepicker fallthrough behavior

- Adds new test cases that test the new `compat-fallthrough` prop of
  `Datepicker`

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

* docs(CHANGELOG): explain compat-fallthrough prop of Datepicker
  • Loading branch information
kikuomax authored Mar 26, 2024
1 parent 919f57e commit bb8d7f8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

* `Datepicker`:

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

* `Input`:

Expand Down
55 changes: 55 additions & 0 deletions packages/buefy-next/src/components/datepicker/Datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,59 @@ describe('BDatepicker', () => {
expect(subject.exists()).toBeTruthy()
})
})

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 div element, if compatFallthrough is true (default)', () => {
const wrapper = shallowMount(BDatepicker, {
attrs,
global: {
stubs: {
// b-dropdown must be rendered to access ref=input
'b-dropdown': false
}
}
})

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

const input = wrapper.findComponent({ 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, if compatFallthrough is false', () => {
const wrapper = shallowMount(BDatepicker, {
attrs,
props: {
compatFallthrough: false
},
global: {
stubs: {
// b-dropdown must be rendered to access ref=input
'b-dropdown': false
}
}
})

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

const input = wrapper.findComponent({ ref: 'input' })
expect(input.classes(attrs.class)).toBe(true)
expect(input.attributes('style')).toBe(attrs.style)
expect(input.attributes('id')).toBe(attrs.id)
})
})
})
9 changes: 5 additions & 4 deletions packages/buefy-next/src/components/datepicker/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div
class="datepicker control"
:class="[size, {'is-expanded': expanded}]"
v-bind="rootAttrs"
>
<b-dropdown
v-if="!isMobile || inline"
Expand Down Expand Up @@ -33,7 +34,7 @@
:loading="loading"
:disabled="disabledOrUndefined"
:readonly="!editable"
v-bind="$attrs"
v-bind="fallthroughAttrs"
:use-html5-validation="false"
@click="onInputClick"
@icon-right-click="$emit('icon-right-click', $event)"
Expand Down Expand Up @@ -217,7 +218,7 @@
:min="formatNative(minDate)"
:disabled="disabledOrUndefined"
:readonly="false"
v-bind="$attrs"
v-bind="fallthroughAttrs"
:use-html5-validation="false"
@change="onChangeNativePicker"
@focus="onFocus"
Expand All @@ -227,6 +228,7 @@
</template>

<script>
import CompatFallthroughMixin from '../../utils/CompatFallthroughMixin'
import FormElementMixin from '../../utils/FormElementMixin'
import { isMobile, getMonthNames, getWeekdayNames, matchWithGroups } from '../../utils/helpers'
import config from '../../utils/config'
Expand Down Expand Up @@ -300,8 +302,7 @@ export default {
[Dropdown.name]: Dropdown,
[DropdownItem.name]: DropdownItem
},
mixins: [FormElementMixin],
inheritAttrs: false,
mixins: [CompatFallthroughMixin, FormElementMixin],
provide() {
return {
$datepicker: this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ export default [
values: '—',
default: '—'
},
{
name: '<code>compat-fallthrough</code>',
description: 'Whether <code>class</code>, <code>style</code>, and <code>id</code> attributes are applied to the root &lt;div&gt; element or the underlying &lt;b-input&gt; component. If <code>true</code>, they are applied to the root &lt;div&gt; element, which is compatible with Buefy for Vue 2.',
type: 'Boolean',
values: '-',
default: '<code>true</code>. Can be changed via <code>defaultCompatFallthrough</code> config option.'
},
{
name: 'Any native attribute',
description: '—',
Expand Down

0 comments on commit bb8d7f8

Please sign in to comment.