Skip to content

Commit

Permalink
chore(lib): update Collapse
Browse files Browse the repository at this point in the history
- `Collapse` migrates to Vue 3. Changes sufficient to render the
  documentation page of `Collapse` are made. A commit that updates
  the documentation page will follow this commit.
- In `src/components/collapse/Collapse.vue`,
    - The `createElement` argument of the `render` function is
      replaced with the global `h` function as `createElement`.
        - `directives` is replaced with `withDirectives`. The
          `vShow` built-in directive is directly specified instead
          of resolving by the name "show".
        - The `Transition` built-in component is directly specified
          instead of resolving by the name "transition".
        - `$scopedSlots` is replaced with `$slots`, because they are
          integrated on Vue 3.
        - `staticClass` --> `class`
        - `on.click` --> `onClick`
    - The default `v-model` binding conforms to Vue 3,
        - `open` prop --> `modelValue`
        - `update:open` event --> `update:modelValue`
    - Events to be sent are listed in `emits`.
  • Loading branch information
kikuomax committed Jul 7, 2023
1 parent b2a8d88 commit 690d289
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions src/components/collapse/Collapse.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<script>
import { h as createElement, Transition, vShow, withDirectives } from 'vue'
export default {
name: 'BCollapse',
// deprecated, to replace with default 'value' in the next breaking change
model: {
prop: 'open',
event: 'update:open'
},
props: {
open: {
modelValue: {
type: Boolean,
default: true
},
Expand All @@ -30,13 +27,14 @@ export default {
}
}
},
emits: ['close', 'open', 'update:modelValue'],
data() {
return {
isOpen: this.open
isOpen: this.modelValue
}
},
watch: {
open(value) {
modelValue(value) {
this.isOpen = value
}
},
Expand All @@ -46,29 +44,53 @@ export default {
*/
toggle() {
this.isOpen = !this.isOpen
this.$emit('update:open', this.isOpen)
this.$emit('update:modelValue', this.isOpen)
this.$emit(this.isOpen ? 'open' : 'close')
}
},
render(createElement) {
const trigger = createElement('div', {
staticClass: 'collapse-trigger', on: { click: this.toggle }
}, this.$scopedSlots.trigger
? [this.$scopedSlots.trigger({ open: this.isOpen })]
: [this.$slots.trigger]
render() {
const trigger = createElement(
'div',
{
class: 'collapse-trigger',
onClick: this.toggle
},
{
default: () => {
return this.$slots.trigger
? this.$slots.trigger({ open: this.isOpen })
: undefined
}
}
)
const content = withDirectives(
createElement(
Transition,
{ name: this.animation },
[
createElement(
'div',
{
class: 'collapse-content',
id: this.ariaId
},
this.$slots
)
]
),
[[vShow, this.isOpen]]
)
return createElement(
'div',
{ class: 'collapse' },
{
default: () => {
return this.position === 'is-top'
? [trigger, content]
: [content, trigger]
}
}
)
const content = createElement('transition', { props: { name: this.animation } }, [
createElement('div', {
staticClass: 'collapse-content',
attrs: { 'id': this.ariaId },
directives: [{
name: 'show',
value: this.isOpen
}]
}, this.$slots.default)
])
return createElement('div', { staticClass: 'collapse' },
this.position === 'is-top' ? [trigger, content] : [content, trigger])
}
}
</script>

0 comments on commit 690d289

Please sign in to comment.