Skip to content

Commit b943d04

Browse files
committed
refactor: CModal
- rename visible prop to show, - add header, body, footer wrapper slots enabling full customization, - replace 'accepted' event with 'hide', - stop sharing hide method in slots, component should be controlled only by show prop
1 parent 6c49868 commit b943d04

File tree

4 files changed

+61
-53
lines changed

4 files changed

+61
-53
lines changed

src/components/Modal/CModal.vue

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,54 @@
88
>
99
<div :class="dialogClasses" role="document">
1010
<div :class="contentClasses">
11-
<div v-if="!noHeader" class="modal-header" >
12-
<slot name="header" :hide="hide">
11+
<slot v-if="!noHeader" name="header-wrapper"
12+
>
13+
<header class="modal-header">
14+
<slot name="header">
1315
<h5 class="modal-title">
1416
{{title}}
1517
</h5>
1618
<button
1719
type="button"
1820
class="close"
1921
aria-label="Close"
20-
@click="hide()"
22+
@click="hide($event)"
2123
>
2224
<span>&times;</span>
2325
</button>
24-
</slot>
25-
</div>
26-
<div v-if="!noBody" class="modal-body">
27-
<slot :hide="hide"></slot>
28-
</div>
29-
<div v-if="!noFooter" class="modal-footer">
30-
<slot name="footer" :hide="hide">
31-
<button
32-
type="button"
33-
class="btn btn-secondary" @click="hide()"
34-
>
35-
Cancel
36-
</button>
37-
<button
38-
type="button"
39-
:class="btnClasses"
40-
@click="hide(true)"
41-
>
42-
OK
43-
</button>
44-
</slot>
45-
</div>
26+
</slot>
27+
</header>
28+
</slot>
29+
<slot v-if="!noBody" name="body-wrapper">
30+
<div class="modal-body">
31+
<slot></slot>
32+
</div>
33+
</slot>
34+
<slot v-if="!noFooter" name="footer-wrapper">
35+
<footer class="modal-footer">
36+
<slot name="footer">
37+
<button
38+
type="button"
39+
class="btn btn-secondary"
40+
@click="hide($event)"
41+
>
42+
Cancel
43+
</button>
44+
<button
45+
type="button"
46+
:class="btnClasses"
47+
@click="hide($event)"
48+
>
49+
OK
50+
</button>
51+
</slot>
52+
</footer>
53+
</slot>
4654
</div>
4755
</div>
4856
</div>
4957
<div
50-
v-if="!noBackdrop && (isVisible || isTransitioning)"
58+
v-if="!noBackdrop && (visible || isTransitioning)"
5159
:class="backdropClasses"
5260
>
5361
</div>
@@ -58,7 +66,7 @@
5866
export default {
5967
name: 'CModal',
6068
props: {
61-
visible: Boolean,
69+
show: Boolean,
6270
centered: Boolean,
6371
title: String,
6472
size: {
@@ -79,7 +87,7 @@ export default {
7987
},
8088
data () {
8189
return {
82-
isVisible: this.visible,
90+
visible: this.show,
8391
isTransitioning: false,
8492
timeout: null,
8593
}
@@ -89,7 +97,7 @@ export default {
8997
return {
9098
'modal-backdrop': true,
9199
'fade': !this.noFade,
92-
'show': this.isVisible || this.noFade
100+
'show': this.visible || this.noFade
93101
}
94102
},
95103
modalClasses () {
@@ -99,8 +107,8 @@ export default {
99107
{
100108
// 'close-modal': !this.noCloseOnBackdrop,
101109
'fade': !this.noFade,
102-
'show': this.isVisible,
103-
'd-block': this.isVisible || this.isTransitioning,
110+
'show': this.visible,
111+
'd-block': this.visible || this.isTransitioning,
104112
[`modal-${this.variant}`]: Boolean(this.variant)
105113
}
106114
]
@@ -129,22 +137,22 @@ export default {
129137
}
130138
},
131139
watch: {
132-
visible (val) {
140+
show (val) {
133141
this.toggle(val)
134142
}
135143
},
136144
methods: {
137145
modalClick (e) {
138146
if (e.target === this.$el.firstElementChild && !this.noCloseOnBackdrop) {
139-
this.hide()
147+
this.hide(e)
140148
}
141149
},
142-
hide (accepted = false) {
143-
this.$emit('update:visible', false)
144-
this.$emit('accepted', accepted)
150+
hide (e) {
151+
this.$emit('update:show', false)
152+
this.$emit('hide', e)
145153
},
146154
toggle (newVal) {
147-
setTimeout(() => { this.isVisible = newVal }, 0)
155+
setTimeout(() => { this.visible = newVal }, 0)
148156
if (!this.noFade) {
149157
this.isTransitioning = true
150158
clearTimeout(this.timeout)

src/components/Modal/tests/CModal.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const ComponentName = 'CModal'
55
const defaultWrapper = mount(Component)
66
const customWrapper = mount(Component, {
77
propsData: {
8-
visible: true,
8+
show: true,
99
centered: true,
1010
title: 'modal title',
1111
size: 'lg',
@@ -37,11 +37,11 @@ describe(ComponentName, () => {
3737
it('hides on backdrop click', () => {
3838
const click = () => customWrapper.find('.modal').trigger('click')
3939
click()
40-
expect(customWrapper.emitted().accepted).not.toBeTruthy()
40+
expect(customWrapper.emitted().hide).not.toBeTruthy()
4141

4242
customWrapper.setProps({ noCloseOnBackdrop: false })
4343
click()
44-
expect(customWrapper.emitted().accepted).toBeTruthy()
44+
expect(customWrapper.emitted().hide).toBeTruthy()
4545
})
4646
it('doesnt animate when noFade prop is set', () => {
4747
defaultWrapper.setProps({ noFade: true })
@@ -51,7 +51,7 @@ describe(ComponentName, () => {
5151
it('toggles visibility correctly', () => {
5252
jest.useFakeTimers()
5353

54-
customWrapper.setProps({ visible: false })
54+
customWrapper.setProps({ show: false })
5555
expect(customWrapper.vm.isTransitioning).toBe(true)
5656
jest.runAllTimers()
5757

src/components/Modal/tests/__snapshots__/CModal.spec.js.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`CModal renders correctly 1`] = `
1414
<div
1515
class="modal-content"
1616
>
17-
<div
17+
<header
1818
class="modal-header"
1919
>
2020
<h5
@@ -34,33 +34,33 @@ exports[`CModal renders correctly 1`] = `
3434
×
3535
</span>
3636
</button>
37-
</div>
37+
</header>
3838
3939
<div
4040
class="modal-body"
4141
/>
4242
43-
<div
43+
<footer
4444
class="modal-footer"
4545
>
4646
<button
4747
class="btn btn-secondary"
4848
type="button"
4949
>
5050
51-
Cancel
52-
51+
Cancel
52+
5353
</button>
5454
5555
<button
5656
class="btn btn-primary"
5757
type="button"
5858
>
5959
60-
OK
61-
60+
OK
61+
6262
</button>
63-
</div>
63+
</footer>
6464
</div>
6565
</div>
6666
</div>
@@ -83,7 +83,7 @@ exports[`CModal renders correctly 2`] = `
8383
<div
8484
class="additional-content-class modal-content border-success"
8585
>
86-
<div
86+
<header
8787
class="modal-header"
8888
>
8989
<h5
@@ -103,7 +103,7 @@ exports[`CModal renders correctly 2`] = `
103103
×
104104
</span>
105105
</button>
106-
</div>
106+
</header>
107107
108108
<div
109109
class="modal-body"

src/components/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export declare class CMedia extends Vue {
369369
}
370370

371371
export declare class CModal extends Vue {
372-
visible: boolean
372+
show: boolean
373373
centered: boolean
374374
title: string
375375
size: string
@@ -571,7 +571,7 @@ export declare class CTabs extends Vue {
571571
}
572572

573573
export declare class CTab extends Vue {
574-
titleHtml: string,
574+
titleHtml: string
575575
active: boolean
576576
disabled: boolean
577577
}

0 commit comments

Comments
 (0)