[improvement] Toast: add context option (#800)

This commit is contained in:
neverland 2018-10-23 11:50:49 +08:00 committed by GitHub
parent f1e5a51c68
commit d4873849d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 12 deletions

View File

@ -1,13 +1,13 @@
<van-button <van-button
square square
id="{{ id }}"
size="large" size="large"
lang="{{ lang }}"
type="{{ type }}" type="{{ type }}"
loading="{{ loading }}" loading="{{ loading }}"
disabled="{{ disabled }}" disabled="{{ disabled }}"
custom-class="custom-class"
id="{{ id }}"
lang="{{ lang }}"
open-type="{{ openType }}" open-type="{{ openType }}"
custom-class="custom-class"
app-parameter="{{ appParameter }}" app-parameter="{{ appParameter }}"
hover-stay-time="{{ hoverStayTime }}" hover-stay-time="{{ hoverStayTime }}"
hover-start-time="{{ hoverStartTime }}" hover-start-time="{{ hoverStartTime }}"
@ -17,7 +17,7 @@
send-message-path="{{ sendMessagePath }}" send-message-path="{{ sendMessagePath }}"
send-message-img="{{ sendMessageImg }}" send-message-img="{{ sendMessageImg }}"
show-message-card="{{ showMessageCard }}" show-message-card="{{ showMessageCard }}"
bind:tap="onClick" bind:click="onClick"
binderror="bindError" binderror="bindError"
bindcontact="bindContact" bindcontact="bindContact"
bindopensetting="bindOpenSetting" bindopensetting="bindOpenSetting"

View File

@ -1,12 +1,12 @@
<van-button <van-button
custom-class="van-goods-action-icon"
size="large"
disabled="{{ disabled }}"
loading="{{ loading }}"
square square
id="{{ id }}" id="{{ id }}"
size="large"
lang="{{ lang }}" lang="{{ lang }}"
loading="{{ loading }}"
disabled="{{ disabled }}"
open-type="{{ openType }}" open-type="{{ openType }}"
custom-class="van-goods-action-icon"
app-parameter="{{ appParameter }}" app-parameter="{{ appParameter }}"
hover-stay-time="{{ hoverStayTime }}" hover-stay-time="{{ hoverStayTime }}"
hover-start-time="{{ hoverStartTime }}" hover-start-time="{{ hoverStartTime }}"
@ -16,7 +16,7 @@
send-message-path="{{ sendMessagePath }}" send-message-path="{{ sendMessagePath }}"
send-message-img="{{ sendMessageImg }}" send-message-img="{{ sendMessageImg }}"
show-message-card="{{ showMessageCard }}" show-message-card="{{ showMessageCard }}"
bind:tap="onClick" bind:click="onClick"
binderror="bindError" binderror="bindError"
bindcontact="bindContact" bindcontact="bindContact"
bindopensetting="bindOpenSetting" bindopensetting="bindOpenSetting"

View File

@ -95,6 +95,7 @@ const timer = setInterval(() => {
| zIndex | z-index 层级 | `Number` | `1000` | | zIndex | z-index 层级 | `Number` | `1000` |
| duration | 展示时长(ms),值为 0 时toast 不会消失 | `Number` | `3000` | | duration | 展示时长(ms),值为 0 时toast 不会消失 | `Number` | `3000` |
| selector | 自定义选择器 | `String` | `van-toast` | | selector | 自定义选择器 | `String` | `van-toast` |
| context | 选择器的选择范围,可以传入自定义组件的 this 作为上下文 | `Object` | 当前页面 |
### 更新日志 ### 更新日志

View File

@ -7,6 +7,7 @@ export type ToastOptions = {
type?: string; type?: string;
mask?: boolean; mask?: boolean;
zIndex?: number; zIndex?: number;
context?: any;
position?: string; position?: string;
duration?: number; duration?: number;
selector?: string; selector?: string;
@ -45,16 +46,26 @@ function parseOptions(message): ToastOptions {
return isObj(message) ? message : { message }; return isObj(message) ? message : { message };
} }
function getContext() {
const pages = getCurrentPages();
return pages[pages.length - 1];
}
const Toast: Toast = (options = {}) => { const Toast: Toast = (options = {}) => {
options = { options = {
...currentOptions, ...currentOptions,
...parseOptions(options) ...parseOptions(options)
} as ToastOptions; } as ToastOptions;
const pages = getCurrentPages(); const context = options.context || getContext();
const ctx = pages[pages.length - 1]; const toast = context.selectComponent(options.selector);
const toast = ctx.selectComponent(options.selector); if (!toast) {
console.warn('未找到 van-toast 节点,请确认 selector 及 context 是否正确');
return;
}
delete options.context;
delete options.selector; delete options.selector;
queue.push(toast); queue.push(toast);