Skip to content

Commit 8144909

Browse files
committed
button as router link
1 parent 47109da commit 8144909

File tree

6 files changed

+121
-18
lines changed

6 files changed

+121
-18
lines changed

dist/vue-typed-ui.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,10 @@ __decorate([vueTyped.Prop({
17071707
type: String,
17081708
default: 'button'
17091709
})], _ButtonBase.prototype, "type", void 0);
1710+
__decorate([vueTyped.Prop()], _ButtonBase.prototype, "to", void 0);
1711+
__decorate([vueTyped.Prop({
1712+
type: Boolean
1713+
})], _ButtonBase.prototype, "preventRouter", void 0);
17101714
var _ButtonEvents;
17111715
(function (_ButtonEvents) {
17121716
/**
@@ -1765,15 +1769,41 @@ var Button = function (_ButtonBase2) {
17651769
if (this.color) {
17661770
css += ' ' + this.color;
17671771
}
1768-
var el = ch('button', {
1769-
attrs: {
1770-
type: this.type
1771-
},
1772-
on: {
1773-
'click': this.click
1774-
},
1775-
'class': css
1776-
}, children);
1772+
var el;
1773+
if (!this.to) {
1774+
el = ch('button', {
1775+
attrs: {
1776+
type: this.type
1777+
},
1778+
on: {
1779+
'click': this.click
1780+
},
1781+
'class': css
1782+
}, children);
1783+
} else {
1784+
if (this.preventRouter) {
1785+
el = ch('a', {
1786+
attrs: {
1787+
href: this.to
1788+
},
1789+
on: {
1790+
'click': this.click
1791+
},
1792+
'class': css
1793+
}, children);
1794+
} else {
1795+
el = ch('router-link', {
1796+
props: {
1797+
to: this.to,
1798+
exact: true
1799+
},
1800+
on: {
1801+
'click': this.click
1802+
},
1803+
'class': css
1804+
}, children);
1805+
}
1806+
}
17771807
return el;
17781808
}
17791809
}, {

doc/api.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,18 @@
125125
"type": "'left' | 'right'",
126126
"description": "Button icon position"
127127
},
128+
"preventRouter": {
129+
"type": "boolean",
130+
"description": "If set to `true` then `to` attribute will always represents `href` attribute of `a` tag."
131+
},
128132
"size": {
129133
"type": "'mini' | 'tiny' | 'small' | 'medium' | 'large' | 'big' | 'huge' | 'massive'",
130134
"description": "Button size"
131135
},
136+
"to": {
137+
"type": "string | Object",
138+
"description": "Target URL when button clicked. If you are using vue-router then this attribute represents `to` attribute of `router-link` tag."
139+
},
132140
"type": {
133141
"type": "string",
134142
"description": "Button type",

lib/interface.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,21 @@ export interface IButton {
9292
*/
9393
iconPos: 'left' | 'right'
9494

95+
/**
96+
* If set to `true` then `to` attribute will always represents `href` attribute of `a` tag.
97+
*/
98+
preventRouter: boolean
99+
95100
/**
96101
* Button size
97102
*/
98103
size: 'mini' | 'tiny' | 'small' | 'medium' | 'large' | 'big' | 'huge' | 'massive'
99104

105+
/**
106+
* Target URL when button clicked. If you are using vue-router then this attribute represents `to` attribute of `router-link` tag.
107+
*/
108+
to: string | Object
109+
100110
/**
101111
* Button type
102112
*/

src/components/form-inputs/button/_base.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ export abstract class _ButtonBase extends Virtual < Vue > () {
8383
})
8484
type: string
8585

86+
/**
87+
* Target URL when button clicked. If you are using vue-router then this attribute represents `to` attribute of `router-link` tag.
88+
*
89+
* @type {string | Object}
90+
*/
91+
@Prop()
92+
to: string | Object
93+
94+
/**
95+
* If set to `true` then `to` attribute will always represents `href` attribute of `a` tag.
96+
*
97+
* @type {boolean}
98+
*/
99+
@Prop({
100+
type: Boolean
101+
})
102+
preventRouter: boolean
103+
86104
}
87105

88106

src/components/form-inputs/button/index.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,44 @@ export class Button extends _ButtonBase implements IButton {
5454
css += ' ' + this.color
5555
}
5656

57-
var el = ch('button', {
58-
attrs: {
59-
type: this.type
60-
},
61-
on: {
62-
'click': this.click
63-
},
64-
'class': css
65-
}, children)
57+
var el
58+
59+
if (!this.to) {
60+
el = ch('button', {
61+
attrs: {
62+
type: this.type
63+
},
64+
on: {
65+
'click': this.click
66+
},
67+
'class': css
68+
}, children)
69+
} else {
70+
if (this.preventRouter) {
71+
el = ch('a', {
72+
attrs: {
73+
href: this.to
74+
},
75+
on: {
76+
'click': this.click
77+
},
78+
'class': css
79+
}, children)
80+
} else {
81+
el = ch('router-link', {
82+
props: {
83+
to: this.to,
84+
exact: true
85+
},
86+
on: {
87+
'click': this.click
88+
},
89+
'class': css
90+
}, children)
91+
}
92+
}
93+
94+
6695

6796
return el
6897
}

src/components/form-inputs/button/schema/props.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
"type": "string",
5252
"description": "Button type",
5353
"default": "'button'"
54+
},
55+
"to": {
56+
"type": "string | Object",
57+
"description": "Target URL when button clicked. If you are using vue-router then this attribute represents `to` attribute of `router-link` tag."
58+
},
59+
"preventRouter": {
60+
"type": "boolean",
61+
"description": "If set to `true` then `to` attribute will always represents `href` attribute of `a` tag."
5462
}
5563
},
5664
"events": {

0 commit comments

Comments
 (0)