mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-25 15:59:16 +08:00
feat: toast update
This commit is contained in:
parent
7fb9a274de
commit
6728837bb2
@ -25,6 +25,12 @@ export default {
|
||||
showFailToast() {
|
||||
Toast.fail('失败文案');
|
||||
},
|
||||
showForbidClickToast() {
|
||||
Toast({
|
||||
message: '背景不能点击',
|
||||
forbidClick: true
|
||||
})
|
||||
},
|
||||
showCustomizedToast(duration) {
|
||||
let leftSec = duration / 1000;
|
||||
let toast = Toast({
|
||||
@ -40,6 +46,12 @@ export default {
|
||||
}
|
||||
toast.message = (--leftSec).toString();
|
||||
}, 1000);
|
||||
},
|
||||
showToast() {
|
||||
this.toast = Toast('我是提示文案,建议不超过十五字~');
|
||||
},
|
||||
closeToast() {
|
||||
this.toast.clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -55,6 +67,7 @@ export default {
|
||||
<zan-button @click="showLoadingToast">加载Toast</zan-button>
|
||||
<zan-button @click="showSuccessToast">成功</zan-button>
|
||||
<zan-button @click="showFailToast">失败</zan-button>
|
||||
<zan-button @click="showForbidClickToast">背景不能点击</zan-button>
|
||||
<zan-button @click="showCustomizedToast(5000)">倒数5秒</zan-button>
|
||||
|
||||
<script>
|
||||
@ -74,6 +87,12 @@ export default {
|
||||
showFailToast() {
|
||||
Toast.fail('失败文案');
|
||||
},
|
||||
showForbidClickToast() {
|
||||
Toast({
|
||||
message: '背景不能点击',
|
||||
forbidClick: true
|
||||
})
|
||||
},
|
||||
showCustomizedToast(duration) {
|
||||
let leftSec = duration / 1000;
|
||||
let toast = Toast({
|
||||
@ -96,9 +115,67 @@ export default {
|
||||
```
|
||||
:::
|
||||
|
||||
### API
|
||||
### 手动关闭
|
||||
|
||||
:::demo 手动关闭
|
||||
```html
|
||||
<zan-button @click="showToast">打开</zan-button>
|
||||
<zan-button @click="closeToast">关闭</zan-button>
|
||||
|
||||
<script>
|
||||
import { Toast } from 'src/index';
|
||||
|
||||
export default {
|
||||
methods: {
|
||||
showToast() {
|
||||
this.toast = Toast('我是提示文案,建议不超过十五字~');
|
||||
},
|
||||
closeToast() {
|
||||
this.toast.clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
```
|
||||
:::
|
||||
|
||||
|
||||
|
||||
### 基础用法
|
||||
### Toast(options)
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||
|-----------|-----------|-----------|-------------|-------------|
|
||||
| type | 类型 | String | 'text' | 'text', 'loading', 'success', 'failure' |
|
||||
| message | 内容 | String | '' | - |\| message | 内容 | String | '' | -
|
||||
| forbidClick | 不允许背景点击 | Boolean | false | true, false|
|
||||
|
||||
### 快速用法
|
||||
### Toast(message)
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||
|-----------|-----------|-----------|-------------|-------------|
|
||||
| message | 内容 | String | '' | - |
|
||||
|
||||
### Toast.loading() || Toast.loading(message, options)
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||
|-----------|-----------|-----------|-------------|-------------|
|
||||
| forbidClick | 不允许背景点击 | Boolean | false | true, false|
|
||||
|
||||
### Toast.success(message) || Toast.success(message, options)
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||
|-----------|-----------|-----------|-------------|-------------|
|
||||
| type | 类型 | String | 'text' | 'text', 'loading', 'success', 'failure' |
|
||||
| forbidClick | 不允许背景点击 | Boolean | false | true, false|
|
||||
|
||||
### Toast.fail(message) || Toast.fail(message, options)
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||
|-----------|-----------|-----------|-------------|-------------|
|
||||
| type | 类型 | String | 'text' | 'text', 'loading', 'success', 'failure' |
|
||||
| forbidClick | 不允许背景点击 | Boolean | false | true, false|
|
||||
|
||||
### instanceOfToast.clear()
|
||||
关闭toast。
|
||||
|
@ -36,16 +36,20 @@ var Toast = (options = {}) => {
|
||||
clearTimeout(instance.timer);
|
||||
instance.type = options.type ? options.type : 'text';
|
||||
instance.message = typeof options === 'string' ? options : options.message;
|
||||
instance.forbidClick = options.forbidClick ? options.forbidClick : false;
|
||||
instance.clear = () => {
|
||||
if (instance.closed) return;
|
||||
instance.visible = false;
|
||||
instance.$el.addEventListener('transitionend', removeDom);
|
||||
instance.closed = true;
|
||||
};
|
||||
|
||||
document.body.appendChild(instance.$el);
|
||||
Vue.nextTick(function() {
|
||||
instance.visible = true;
|
||||
instance.$el.removeEventListener('transitionend', removeDom);
|
||||
instance.timer = setTimeout(function() {
|
||||
if (instance.closed) return;
|
||||
instance.visible = false;
|
||||
instance.$el.addEventListener('transitionend', removeDom);
|
||||
instance.closed = true;
|
||||
instance.clear();
|
||||
}, duration);
|
||||
});
|
||||
return instance;
|
||||
|
@ -1,19 +1,22 @@
|
||||
<template>
|
||||
<transition name="zan-toast">
|
||||
<div class="zan-toast" :class="['zan-toast--' + displayStyle]" v-show="visible">
|
||||
<!-- 只显示文字 -->
|
||||
<template v-if="displayStyle === 'text'" >
|
||||
<div class="zan-toast__text">{{message}}</div>
|
||||
</template>
|
||||
<!-- 加载中 -->
|
||||
<template v-if="displayStyle === 'loading'">
|
||||
<zan-loading v-if="type === 'loading'" type="gradient-circle" color="white"></zan-loading>
|
||||
</template>
|
||||
<!-- 图案加文字 -->
|
||||
<template v-if="displayStyle === 'default'">
|
||||
<zan-icon class="zan-toast__icon" name="check"></zan-icon>
|
||||
<div class="zan-toast__text">{{message}}</div>
|
||||
</template>
|
||||
<div class="zan-toast-wrapper" v-show="visible">
|
||||
<div class="zan-toast" :class="['zan-toast--' + displayStyle]">
|
||||
<!-- 只显示文字 -->
|
||||
<template v-if="displayStyle === 'text'" >
|
||||
<div class="zan-toast__text">{{message}}</div>
|
||||
</template>
|
||||
<!-- 加载中 -->
|
||||
<template v-if="displayStyle === 'loading'">
|
||||
<zan-loading v-if="type === 'loading'" type="gradient-circle" color="white"></zan-loading>
|
||||
</template>
|
||||
<!-- 图案加文字 -->
|
||||
<template v-if="displayStyle === 'default'">
|
||||
<zan-icon class="zan-toast__icon" :name="type"></zan-icon>
|
||||
<div class="zan-toast__text">{{message}}</div>
|
||||
</template>
|
||||
</div>
|
||||
<div class="zan-toast__overlay" v-if="forbidClick"></div>
|
||||
</div>
|
||||
</transition>
|
||||
</template>
|
||||
@ -54,6 +57,10 @@ export default {
|
||||
return value.length <= 16;
|
||||
}
|
||||
}
|
||||
},
|
||||
forbidClick: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
data() {
|
||||
@ -71,10 +78,6 @@ export default {
|
||||
default:
|
||||
return 'default';
|
||||
}
|
||||
},
|
||||
iconName() {
|
||||
// TODO: 更新icon
|
||||
return 'check';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
@component-namespace zan {
|
||||
@b toast {
|
||||
position: fixed;
|
||||
z-index: 3000;
|
||||
z-index: 3001;
|
||||
border-radius: 5px;
|
||||
background-color: #272727;
|
||||
opacity: .7;
|
||||
@ -13,6 +13,17 @@
|
||||
font-size: 12px;
|
||||
color: $c-white;
|
||||
text-align: center;
|
||||
line-height: 12px;
|
||||
|
||||
@e overlay {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
top: 0;
|
||||
background: transparent;
|
||||
height: 100vh;
|
||||
width: 100vh;
|
||||
z-index: 3000;
|
||||
}
|
||||
|
||||
@m loading {
|
||||
padding: 45px;
|
||||
@ -26,12 +37,15 @@
|
||||
@m default {
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
|
||||
.zan-toast__icon {
|
||||
padding: 20px;
|
||||
font-size: 36px;
|
||||
padding-top: 20px;
|
||||
font-size: 50px;
|
||||
}
|
||||
|
||||
.zan-toast__text {
|
||||
padding-bottom: 20px;
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
test/unit/specs/toast.spec.js
Normal file
36
test/unit/specs/toast.spec.js
Normal file
@ -0,0 +1,36 @@
|
||||
import Toast from 'packages/toast';
|
||||
import { mount } from 'avoriaz';
|
||||
|
||||
describe('Toast', () => {
|
||||
it('create simple toast', () => {
|
||||
Toast('a message');
|
||||
var toast = document.querySelector('.zan-toast');
|
||||
|
||||
expect(toast).not.to.be.underfined;
|
||||
|
||||
setTimeout(() => {
|
||||
expect(toast.hidden).to.be.true;
|
||||
}, 301);
|
||||
});
|
||||
|
||||
it('create loading toast', () => {
|
||||
Toast.loading('');
|
||||
var toast = document.querySelector('.zan-toast');
|
||||
|
||||
expect(toast).not.to.be.underfined;
|
||||
|
||||
setTimeout(() => {
|
||||
expect(toast.hidden).to.be.true;
|
||||
}, 301);
|
||||
});
|
||||
it('create loading toast', () => {
|
||||
Toast.success('');
|
||||
var toast = document.querySelector('.zan-toast');
|
||||
|
||||
expect(toast).not.to.be.underfined;
|
||||
|
||||
setTimeout(() => {
|
||||
expect(toast.hidden).to.be.true;
|
||||
}, 301);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user