2023-03-15 19:23:52 +08:00
|
|
|
/**
|
|
|
|
* {按钮组件}
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2023/03/06 15:17:25
|
|
|
|
*/
|
|
|
|
|
2023-03-17 18:29:47 +08:00
|
|
|
import { css, html, Component, nextTick } from '@bd/core'
|
2023-03-15 19:23:52 +08:00
|
|
|
import '../icon/index.js'
|
|
|
|
|
|
|
|
class Button extends Component {
|
|
|
|
static props = {
|
2023-03-16 13:56:32 +08:00
|
|
|
type: 'primary',
|
2023-03-15 19:23:52 +08:00
|
|
|
icon: '',
|
2023-03-16 13:56:32 +08:00
|
|
|
size: 'l',
|
|
|
|
autofocus: false,
|
2023-03-16 17:26:36 +08:00
|
|
|
loading: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
observer(val) {
|
|
|
|
if (val) {
|
|
|
|
this.cacheIcon = this.icon
|
|
|
|
this.icon = 'loading'
|
|
|
|
} else {
|
|
|
|
this.icon = this.cacheIcon || ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-03-15 19:23:52 +08:00
|
|
|
disabled: false,
|
|
|
|
lazy: 0 // 并发拦截时间, 单位毫秒
|
|
|
|
}
|
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
static styles = [
|
|
|
|
// 基础样式
|
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
overflow: hidden;
|
|
|
|
display: inline-flex;
|
|
|
|
border: 0;
|
|
|
|
border-radius: 3px;
|
|
|
|
user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
color: var(--color-dark-1);
|
|
|
|
font-size: 14px;
|
|
|
|
cursor: pointer;
|
|
|
|
transition: box-shadow 0.15s linear;
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
button {
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
width: 100%;
|
|
|
|
min-width: 1px;
|
|
|
|
height: inherit;
|
|
|
|
padding: var(--button-padding, 0 4px);
|
|
|
|
line-height: 1;
|
|
|
|
border: 1px solid transparent;
|
|
|
|
border-radius: inherit;
|
|
|
|
white-space: nowrap;
|
|
|
|
background: #fff;
|
|
|
|
font-size: inherit;
|
|
|
|
font-family: inherit;
|
|
|
|
outline: none;
|
|
|
|
color: inherit;
|
|
|
|
cursor: inherit;
|
|
|
|
transition: background 0.15s linear;
|
|
|
|
|
|
|
|
&::-moz-focus-inner {
|
|
|
|
border: none;
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
.icon {
|
|
|
|
--size: var(--icon-size, 18px);
|
|
|
|
margin-right: 4px;
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
2023-03-16 13:56:32 +08:00
|
|
|
:host(:focus-within) {
|
|
|
|
box-shadow: 0 0 0 2px var(--color-plain-a);
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
2023-03-16 13:56:32 +08:00
|
|
|
`,
|
|
|
|
// 尺寸
|
|
|
|
css`
|
|
|
|
@use 'sass:map';
|
|
|
|
$sizes: (
|
|
|
|
s: (
|
|
|
|
w: 52px,
|
|
|
|
h: 20px,
|
|
|
|
f: 12px
|
|
|
|
),
|
|
|
|
m: (
|
|
|
|
w: 72px,
|
|
|
|
h: 24px,
|
|
|
|
f: 12px
|
|
|
|
),
|
|
|
|
l: (
|
|
|
|
w: 108px,
|
|
|
|
h: 32px,
|
|
|
|
f: 14px
|
|
|
|
),
|
|
|
|
xl: (
|
|
|
|
w: 132px,
|
|
|
|
h: 36px,
|
|
|
|
f: 14px
|
|
|
|
),
|
|
|
|
xxl: (
|
|
|
|
w: 160px,
|
|
|
|
h: 44px,
|
|
|
|
f: 14px
|
|
|
|
),
|
|
|
|
xxxl: (
|
|
|
|
w: 192px,
|
|
|
|
h: 52px,
|
|
|
|
f: 16px
|
|
|
|
),
|
|
|
|
xxxxl: (
|
|
|
|
w: 212px,
|
|
|
|
h: 64px,
|
|
|
|
f: 18px
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
@loop $s, $v in $sizes {
|
|
|
|
:host([size='#{$s}']) {
|
|
|
|
min-width: map.get($v, 'w');
|
|
|
|
height: map.get($v, 'h');
|
|
|
|
font-size: map.get($v, 'f');
|
|
|
|
|
|
|
|
.icon {
|
|
|
|
--size: #{map.get($v, 'f')};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:host([size='#{$s}'][circle]) {
|
|
|
|
width: map.get($v, 'h');
|
|
|
|
height: map.get($v, 'h');
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
:host([dashed]) button {
|
|
|
|
border-style: dashed;
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
:host([round]) {
|
|
|
|
border-radius: 32px;
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
2023-03-16 13:56:32 +08:00
|
|
|
:host([circle]) {
|
|
|
|
min-width: 0;
|
|
|
|
border-radius: 50%;
|
2023-03-15 19:23:52 +08:00
|
|
|
button {
|
2023-03-16 13:56:32 +08:00
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
.icon {
|
|
|
|
margin-right: 0;
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
slot {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
// 配色
|
|
|
|
css`
|
|
|
|
$colors: (
|
|
|
|
primary: 'teal',
|
|
|
|
info: 'blue',
|
|
|
|
success: 'green',
|
|
|
|
warning: 'orange',
|
|
|
|
danger: 'red',
|
|
|
|
secondary: 'dark',
|
|
|
|
help: 'grey'
|
|
|
|
);
|
|
|
|
|
|
|
|
@loop $t, $c in $colors {
|
|
|
|
:host([type='#{$t}']) {
|
|
|
|
button {
|
2023-03-15 19:23:52 +08:00
|
|
|
color: var(--color-#{$c}-2);
|
2023-03-16 13:56:32 +08:00
|
|
|
border-color: var(--color-#{$c}-2);
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
color: var(--color-#{$c}-1);
|
|
|
|
border-color: var(--color-#{$c}-1);
|
|
|
|
}
|
|
|
|
&:active {
|
|
|
|
color: var(--color-#{$c}-3);
|
|
|
|
}
|
|
|
|
&:disabled {
|
|
|
|
color: var(--color-#{$c}-2);
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
&:host([solid]) button {
|
|
|
|
border: 0;
|
|
|
|
color: #fff;
|
|
|
|
background: var(--color-#{$c}-2);
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
&:hover {
|
|
|
|
background: var(--color-#{$c}-1);
|
|
|
|
}
|
|
|
|
&:active {
|
|
|
|
background: var(--color-#{$c}-3);
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
&:disabled {
|
|
|
|
background: var(--color-#{$c}-2);
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
|
2023-03-16 13:56:32 +08:00
|
|
|
&:host(:focus-within) {
|
|
|
|
box-shadow: 0 0 0 2px var(--color-#{$c}-a);
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 13:56:32 +08:00
|
|
|
`,
|
|
|
|
// 状态
|
|
|
|
css`
|
|
|
|
:host([loading]),
|
|
|
|
:host([disabled]) {
|
|
|
|
cursor: not-allowed;
|
|
|
|
opacity: 0.6;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
]
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.stamp = 0
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 18:03:07 +08:00
|
|
|
this._clickFn = this.$on(
|
2023-03-16 13:56:32 +08:00
|
|
|
'click',
|
|
|
|
ev => {
|
|
|
|
let { loading, disabled, lazy } = this
|
|
|
|
let now = Date.now()
|
|
|
|
|
|
|
|
if (loading || disabled) {
|
|
|
|
return ev.stopPropagation()
|
|
|
|
}
|
|
|
|
// 并发拦截
|
|
|
|
if (lazy > 0 && now - this.stamp < lazy) {
|
|
|
|
return ev.stopPropagation()
|
|
|
|
}
|
|
|
|
this.stamp = now
|
|
|
|
},
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}
|
2023-03-15 19:23:52 +08:00
|
|
|
|
2023-03-16 17:26:36 +08:00
|
|
|
mounted() {
|
|
|
|
if (this.autofocus) {
|
2023-03-17 18:29:47 +08:00
|
|
|
this.$refs.btn.setAttribute('autofocus', '')
|
2023-03-16 17:26:36 +08:00
|
|
|
// 需要focus()才能聚焦成功
|
2023-03-17 18:29:47 +08:00
|
|
|
nextTick(_ => this.$refs.btn.focus())
|
2023-03-16 17:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 18:03:07 +08:00
|
|
|
unmounted() {
|
|
|
|
this.$off('click', this._clickFn)
|
|
|
|
}
|
|
|
|
|
2023-03-15 19:23:52 +08:00
|
|
|
render() {
|
2023-03-16 13:56:32 +08:00
|
|
|
return html`
|
2023-03-17 18:29:47 +08:00
|
|
|
<button ref="btn" disabled=${this.disabled}>
|
2023-03-21 16:06:07 +08:00
|
|
|
<wc-icon class="icon" name=${this.icon}></wc-icon>
|
2023-03-17 18:29:47 +08:00
|
|
|
<slot></slot>
|
2023-03-16 13:56:32 +08:00
|
|
|
</button>
|
|
|
|
`
|
2023-03-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:50:02 +08:00
|
|
|
Button.reg('button')
|