mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-23 09:52:57 +08:00
[Improvement] Actionsheet: add select event (#1594)
This commit is contained in:
parent
58eab6f2df
commit
1f67c28998
@ -2,12 +2,18 @@
|
|||||||
<demo-section>
|
<demo-section>
|
||||||
<demo-block :title="$t('basicUsage')">
|
<demo-block :title="$t('basicUsage')">
|
||||||
<van-button @click="show1 = true">{{ $t('button1') }}</van-button>
|
<van-button @click="show1 = true">{{ $t('button1') }}</van-button>
|
||||||
<van-actionsheet v-model="show1" :actions="actions" />
|
<van-actionsheet v-model="show1" :actions="actions" @select="onSelect" />
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block :title="$t('title2')">
|
<demo-block :title="$t('title2')">
|
||||||
<van-button @click="show2 = true">{{ $t('button2') }}</van-button>
|
<van-button @click="show2 = true">{{ $t('button2') }}</van-button>
|
||||||
<van-actionsheet v-model="show2" :actions="actions" :cancel-text="$t('cancel')" @cancel="handleCancel" />
|
<van-actionsheet
|
||||||
|
v-model="show2"
|
||||||
|
:actions="actions"
|
||||||
|
:cancel-text="$t('cancel')"
|
||||||
|
@cancel="onCancel"
|
||||||
|
@select="onSelect"
|
||||||
|
/>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block :title="$t('title3')">
|
<demo-block :title="$t('title3')">
|
||||||
@ -53,20 +59,22 @@ export default {
|
|||||||
computed: {
|
computed: {
|
||||||
actions() {
|
actions() {
|
||||||
return [
|
return [
|
||||||
{ name: this.$t('option'), callback: this.onClick },
|
{ name: this.$t('option') },
|
||||||
{ name: this.$t('option'), subname: this.$t('description') },
|
{ name: this.$t('option'), subname: this.$t('description') },
|
||||||
{ name: this.$t('option'), loading: true },
|
{ loading: true },
|
||||||
{ name: this.$t('disabledOption'), disabled: true }
|
{ name: this.$t('disabledOption'), disabled: true }
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onClick(item) {
|
onSelect(item) {
|
||||||
|
this.show1 = false;
|
||||||
|
this.show2 = false;
|
||||||
this.$toast(item.name);
|
this.$toast(item.name);
|
||||||
},
|
},
|
||||||
|
|
||||||
handleCancel() {
|
onCancel() {
|
||||||
this.$toast('cancel');
|
this.$toast('cancel');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,10 +83,6 @@ export default {
|
|||||||
|
|
||||||
<style lang="postcss">
|
<style lang="postcss">
|
||||||
.demo-actionsheet {
|
.demo-actionsheet {
|
||||||
.actionsheet-wx {
|
|
||||||
color: #06bf04;
|
|
||||||
}
|
|
||||||
|
|
||||||
.van-button {
|
.van-button {
|
||||||
margin-left: 15px;
|
margin-left: 15px;
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,11 @@ Vue.use(Actionsheet);
|
|||||||
Use `actions` prop to set options of actionsheet.
|
Use `actions` prop to set options of actionsheet.
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-actionsheet v-model="show" :actions="actions" />
|
<van-actionsheet
|
||||||
|
v-model="show"
|
||||||
|
:actions="actions"
|
||||||
|
@select="onSelect"
|
||||||
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -23,15 +27,13 @@ export default {
|
|||||||
show: false,
|
show: false,
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
name: 'Option',
|
name: 'Option'
|
||||||
callback: this.onClick
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Option',
|
name: 'Option',
|
||||||
description: 'Description'
|
description: 'Description'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Option',
|
|
||||||
loading: true
|
loading: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -43,7 +45,8 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onClick(item) {
|
onSelect(item) {
|
||||||
|
this.show = false;
|
||||||
Toast(item.name);
|
Toast(item.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,7 +56,13 @@ export default {
|
|||||||
#### Actionsheet with cancel button
|
#### Actionsheet with cancel button
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-actionsheet v-model="show" :actions="actions" cancel-text="Cancel" />
|
<van-actionsheet
|
||||||
|
v-model="show"
|
||||||
|
:actions="actions"
|
||||||
|
cancel-text="Cancel"
|
||||||
|
@select="onSelect"
|
||||||
|
@calcel="onCancel"
|
||||||
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Actionsheet with title
|
#### Actionsheet with title
|
||||||
@ -72,8 +81,8 @@ Actionsheet will get another style if there is a `title` prop.
|
|||||||
| actions | Options | `Array` | `[]` |
|
| actions | Options | `Array` | `[]` |
|
||||||
| title | Title | `String` | - |
|
| title | Title | `String` | - |
|
||||||
| cancel-text | Text of cancel button | `String` | - |
|
| cancel-text | Text of cancel button | `String` | - |
|
||||||
| overlay | Whether to show overlay | `Boolean` | - |
|
| overlay | Whether to show overlay | `Boolean` | `true` |
|
||||||
| close-on-click-overlay | Whether to close when click overlay | `Boolean` | - |
|
| close-on-click-overlay | Whether to close when click overlay | `Boolean` | `true` |
|
||||||
| lazy-render | Whether to lazy render util appeared | `Boolean` | `true` |
|
| lazy-render | Whether to lazy render util appeared | `Boolean` | `true` |
|
||||||
| get-container | Return the mount node for actionsheet | `() => HTMLElement` | - |
|
| get-container | Return the mount node for actionsheet | `() => HTMLElement` | - |
|
||||||
|
|
||||||
@ -81,6 +90,7 @@ Actionsheet will get another style if there is a `title` prop.
|
|||||||
|
|
||||||
| Event | Description | Arguments |
|
| Event | Description | Arguments |
|
||||||
|-----------|-----------|-----------|
|
|-----------|-----------|-----------|
|
||||||
|
| select | Triggered when click option | item |
|
||||||
| cancel | Triggered when cancel click | - |
|
| cancel | Triggered when cancel click | - |
|
||||||
|
|
||||||
### Data struct of actions
|
### Data struct of actions
|
||||||
@ -92,4 +102,3 @@ Actionsheet will get another style if there is a `title` prop.
|
|||||||
| className | className for the option |
|
| className | className for the option |
|
||||||
| loading | Whether to be loading status |
|
| loading | Whether to be loading status |
|
||||||
| disabled | Whether to be disabled |
|
| disabled | Whether to be disabled |
|
||||||
| callback | Triggered when click option |
|
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<li
|
<li
|
||||||
v-for="item in actions"
|
v-for="item in actions"
|
||||||
:class="[b('item', { disabled: item.disabled || item.loading }), item.className, 'van-hairline--top']"
|
:class="[b('item', { disabled: item.disabled || item.loading }), item.className, 'van-hairline--top']"
|
||||||
@click.stop="onClickItem(item)"
|
@click.stop="onSelect(item)"
|
||||||
>
|
>
|
||||||
<template v-if="!item.loading">
|
<template v-if="!item.loading">
|
||||||
<span :class="b('name')">{{ item.name }}</span>
|
<span :class="b('name')">{{ item.name }}</span>
|
||||||
@ -59,9 +59,10 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onClickItem(item) {
|
onSelect(item) {
|
||||||
if (item.callback && !item.disabled && !item.loading) {
|
if (!item.disabled && !item.loading) {
|
||||||
item.callback(item);
|
item.callback && item.callback(item);
|
||||||
|
this.$emit('select', item);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ exports[`callback events 1`] = `
|
|||||||
<li class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span>
|
<li class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span>
|
||||||
<!---->
|
<!---->
|
||||||
</li>
|
</li>
|
||||||
<li class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span>
|
<li class="van-actionsheet__item van-actionsheet__item--disabled van-hairline--top"><span class="van-actionsheet__name">Option</span>
|
||||||
<!---->
|
<!---->
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -8,17 +8,21 @@ test('callback events', () => {
|
|||||||
value: true,
|
value: true,
|
||||||
actions: [
|
actions: [
|
||||||
{ name: 'Option', callback },
|
{ name: 'Option', callback },
|
||||||
{ name: 'Option' }
|
{ name: 'Option', disabled: true }
|
||||||
],
|
],
|
||||||
cancelText: 'Cancel'
|
cancelText: 'Cancel'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
wrapper.findAll('li').trigger('click');
|
const options = wrapper.findAll('li');
|
||||||
|
options.at(0).trigger('click');
|
||||||
|
options.at(1).trigger('click');
|
||||||
wrapper.find('.van-actionsheet__cancel').trigger('click');
|
wrapper.find('.van-actionsheet__cancel').trigger('click');
|
||||||
|
|
||||||
expect(callback.mock.calls.length).toBe(1);
|
expect(callback.mock.calls.length).toBe(1);
|
||||||
expect(wrapper.emitted('cancel')).toBeTruthy();
|
expect(wrapper.emitted('cancel')).toBeTruthy();
|
||||||
expect(wrapper.emitted('input')[0][0]).toBeFalsy();
|
expect(wrapper.emitted('input')[0][0]).toBeFalsy();
|
||||||
|
expect(wrapper.emitted('select')[0][0]).toBeTruthy();
|
||||||
|
expect(wrapper.emitted('select')[0][1]).toBeFalsy();
|
||||||
expect(wrapper).toMatchSnapshot();
|
expect(wrapper).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
@ -13,7 +13,11 @@ Vue.use(Actionsheet);
|
|||||||
需要传入一个`actions`的数组,数组的每一项是一个对象,对象属性见文档下方表格。
|
需要传入一个`actions`的数组,数组的每一项是一个对象,对象属性见文档下方表格。
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-actionsheet v-model="show" :actions="actions" />
|
<van-actionsheet
|
||||||
|
v-model="show"
|
||||||
|
:actions="actions"
|
||||||
|
@select="onSelect"
|
||||||
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -23,15 +27,13 @@ export default {
|
|||||||
show: false,
|
show: false,
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
name: '选项',
|
name: '选项'
|
||||||
callback: this.onClick
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '选项',
|
name: '选项',
|
||||||
subname: '描述信息'
|
subname: '描述信息'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '选项',
|
|
||||||
loading: true
|
loading: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -43,7 +45,9 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onClick(item) {
|
onSelect(item) {
|
||||||
|
// 点击选项时默认不会关闭菜单,可以手动关闭
|
||||||
|
this.show = false;
|
||||||
Toast(item.name);
|
Toast(item.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +59,13 @@ export default {
|
|||||||
如果传入了`cancelText`属性,且不为空,则会在下方显示一个取消按钮,点击会将当前`Actionsheet`关闭。
|
如果传入了`cancelText`属性,且不为空,则会在下方显示一个取消按钮,点击会将当前`Actionsheet`关闭。
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-actionsheet v-model="show" :actions="actions" cancel-text="取消" />
|
<van-actionsheet
|
||||||
|
v-model="show"
|
||||||
|
:actions="actions"
|
||||||
|
cancel-text="取消"
|
||||||
|
@select="onSelect"
|
||||||
|
@calcel="onCancel"
|
||||||
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 带标题的 Actionsheet
|
#### 带标题的 Actionsheet
|
||||||
@ -74,9 +84,9 @@ export default {
|
|||||||
|-----------|-----------|-----------|-------------|
|
|-----------|-----------|-----------|-------------|
|
||||||
| actions | 菜单选项 | `Array` | `[]` |
|
| actions | 菜单选项 | `Array` | `[]` |
|
||||||
| title | 标题 | `String` | - |
|
| title | 标题 | `String` | - |
|
||||||
| cancel-text | 取消按钮文字 | `String` | - |
|
| cancel-text | 取消按钮文字,为空时不展示取消按钮 | `String` | - |
|
||||||
| overlay | 是否显示遮罩层 | `Boolean` | - |
|
| overlay | 是否显示遮罩层 | `Boolean` | `true` |
|
||||||
| close-on-click-overlay | 点击遮罩是否关闭菜单 | `Boolean` | - |
|
| close-on-click-overlay | 点击遮罩是否关闭菜单 | `Boolean` | `true` |
|
||||||
| lazy-render | 是否在首次显示弹层时才渲染 DOM 节点 | `Boolean` | `true` |
|
| lazy-render | 是否在首次显示弹层时才渲染 DOM 节点 | `Boolean` | `true` |
|
||||||
| get-container | 指定挂载的 HTML 节点 | `() => HTMLElement` | - |
|
| get-container | 指定挂载的 HTML 节点 | `() => HTMLElement` | - |
|
||||||
|
|
||||||
@ -84,6 +94,7 @@ export default {
|
|||||||
|
|
||||||
| 事件名 | 说明 | 参数 |
|
| 事件名 | 说明 | 参数 |
|
||||||
|-----------|-----------|-----------|
|
|-----------|-----------|-----------|
|
||||||
|
| select | 选中选项时触发,禁用或加载状态下不会触发 | item: 选项对应的对象 |
|
||||||
| cancel | 取消按钮点击时触发 | - |
|
| cancel | 取消按钮点击时触发 | - |
|
||||||
|
|
||||||
### actions
|
### actions
|
||||||
@ -97,4 +108,3 @@ export default {
|
|||||||
| className | 为对应列添加额外的 class |
|
| className | 为对应列添加额外的 class |
|
||||||
| loading | 是否为加载状态 |
|
| loading | 是否为加载状态 |
|
||||||
| disabled | 是否为禁用状态 |
|
| disabled | 是否为禁用状态 |
|
||||||
| callback | 点击时的回调。该回调接受一个参数,参数为当前点击 action 的对象信息 |
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user