[breaking change] Actionsheet: rename to ActionSheet

This commit is contained in:
陈嘉涵
2019-04-29 14:20:00 +08:00
parent fe89e18a24
commit b8424f8de9
19 changed files with 119 additions and 119 deletions

View File

@@ -0,0 +1,101 @@
<template>
<demo-section>
<demo-block :title="$t('basicUsage')">
<van-button @click="show1 = true">{{ $t('button1') }}</van-button>
<van-action-sheet
v-model="show1"
:actions="actions"
@select="onSelect"
/>
</demo-block>
<demo-block :title="$t('title2')">
<van-button @click="show2 = true">{{ $t('button2') }}</van-button>
<van-action-sheet
v-model="show2"
:actions="actions"
:cancel-text="$t('cancel')"
@cancel="onCancel"
@select="onSelect"
/>
</demo-block>
<demo-block :title="$t('title3')">
<van-button @click="show3 = true">{{ $t('button3') }}</van-button>
<van-action-sheet
v-model="show3"
:title="$t('title')"
>
<p>{{ $t('content') }}</p>
</van-action-sheet>
</demo-block>
</demo-section>
</template>
<script>
export default {
i18n: {
'zh-CN': {
button1: '弹出 ActionSheet',
button2: '弹出带取消按钮的 ActionSheet',
button3: '弹出带标题的 ActionSheet',
title2: '带取消按钮的 ActionSheet',
title3: '带标题的 ActionSheet',
description: '描述信息',
disabledOption: '禁用选项'
},
'en-US': {
button1: 'Show ActionSheet',
button2: 'Show ActionSheet with cancel button',
button3: 'Show ActionSheet with title',
title2: 'ActionSheet with cancel button',
title3: 'ActionSheet with title',
description: 'Description',
disabledOption: 'Disabled Option'
}
},
data() {
return {
show1: false,
show2: false,
show3: false
};
},
computed: {
actions() {
return [
{ name: this.$t('option') },
{ name: this.$t('option'), subname: this.$t('description') },
{ loading: true },
{ name: this.$t('disabledOption'), disabled: true }
];
}
},
methods: {
onSelect(item) {
this.show1 = false;
this.show2 = false;
this.$toast(item.name);
},
onCancel() {
this.$toast('cancel');
}
}
};
</script>
<style lang="less">
.demo-action-sheet {
.van-button {
margin-left: 15px;
}
p {
padding: 20px;
}
}
</style>

View File

@@ -0,0 +1,105 @@
## ActionSheet
### Install
``` javascript
import { ActionSheet } from 'vant';
Vue.use(ActionSheet);
```
### Usage
#### Basic Usage
Use `actions` prop to set options of action-sheet.
```html
<van-action-sheet
v-model="show"
:actions="actions"
@select="onSelect"
/>
```
```javascript
export default {
data() {
return {
show: false,
actions: [
{
name: 'Option'
},
{
name: 'Option',
description: 'Description'
},
{
loading: true
},
{
name: 'Disabled Option',
disabled: true
}
]
};
},
methods: {
onSelect(item) {
this.show = false;
Toast(item.name);
}
}
}
```
#### ActionSheet with cancel button
```html
<van-action-sheet
v-model="show"
:actions="actions"
cancel-text="Cancel"
@select="onSelect"
@cancel="onCancel"
/>
```
#### ActionSheet with title
ActionSheet will get another style if there is a `title` prop.
```html
<van-action-sheet v-model="show" title="Title">
<p>Content</p>
</van-action-sheet>
```
### API
| Attribute | Description | Type | Default |
|------|------|------|------|
| actions | Options | `Array` | `[]` |
| title | Title | `String` | - |
| cancel-text | Text of cancel button | `String` | - |
| overlay | Whether to show overlay | `Boolean` | `true` |
| close-on-click-overlay | Whether to close when click overlay | `Boolean` | `true` |
| lazy-render | Whether to lazy render util appeared | `Boolean` | `true` |
| get-container | Return the mount node for action-sheet | `String | () => HTMLElement` | - |
| safe-area-inset-bottom | Whether to enable bottom safe area adaptation, to enable those features use `viewport-fit=cover` in the `viewport` meta tag | `Boolean` | `false` |
### Event
| Event | Description | Arguments |
|------|------|------|
| select | Triggered when click option | item, index |
| cancel | Triggered when cancel click | - |
### Data struct of actions
| key | Description |
|------|------|
| name | Title |
| subname | Subtitle |
| className | className for the option |
| loading | Whether to be loading status |
| disabled | Whether to be disabled |

View File

@@ -0,0 +1,68 @@
@import '../style/var';
.van-action-sheet {
color: @action-sheet-item-text-color;
max-height: @action-sheet-max-height;
&__item,
&__cancel {
text-align: center;
font-size: @action-sheet-item-font-size;
line-height: @action-sheet-item-height;
background-color: @action-sheet-item-background;
&:active {
background-color: @active-color;
}
}
&__item {
height: @action-sheet-item-height;
}
&__item--disabled {
color: @gray;
&:active {
background-color: @white;
}
}
&__subname {
color: @action-sheet-subname-color;
font-size: @action-sheet-subname-font-size;
margin-left: 5px;
}
&__loading {
display: inline-block;
}
&__cancel::before {
content: ' ';
display: block;
height: 10px;
background-color: @background-color;
}
&__header {
font-size: @action-sheet-header-font-size;
line-height: @action-sheet-header-height;
text-align: center;
}
&__close {
top: 0;
right: 0;
padding: 0 15px;
position: absolute;
line-height: inherit;
color: @action-sheet-close-icon-color;
font-size: @action-sheet-close-icon-size;
}
&--safe-area-inset-bottom {
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
}

View File

@@ -0,0 +1,122 @@
import { use } from '../utils';
import { emit, inherit } from '../utils/functional';
import { PopupMixin } from '../mixins/popup';
import Icon from '../icon';
import Loading from '../loading';
import Popup from '../popup';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { PopupMixinProps } from '../mixins/popup/type';
export type ActionSheetItem = {
name: string;
subname?: string;
loading?: boolean;
disabled?: boolean;
className?: string;
callback?: (item: ActionSheetItem) => void;
};
export type ActionSheetProps = PopupMixinProps & {
title?: string;
actions: ActionSheetItem[];
cancelText?: string;
safeAreaInsetBottom?: boolean;
};
const [sfc, bem] = use('action-sheet');
function ActionSheet(
h: CreateElement,
props: ActionSheetProps,
slots: DefaultSlots,
ctx: RenderContext<ActionSheetProps>
) {
const { title, cancelText } = props;
const onCancel = () => {
emit(ctx, 'input', false);
emit(ctx, 'cancel');
};
const Header = () => (
<div class={[bem('header'), 'van-hairline--top-bottom']}>
{title}
<Icon name="close" class={bem('close')} onClick={onCancel} />
</div>
);
const Option = (item: ActionSheetItem, index: number) => (
<div
class={[
bem('item', { disabled: item.disabled || item.loading }),
item.className,
'van-hairline--top'
]}
onClick={(event: Event) => {
event.stopPropagation();
if (!item.disabled && !item.loading) {
if (item.callback) {
item.callback(item);
}
emit(ctx, 'select', item, index);
}
}}
>
{item.loading ? (
<Loading class={bem('loading')} size="20px" />
) : (
[
<span class={bem('name')}>{item.name}</span>,
item.subname && <span class={bem('subname')}>{item.subname}</span>
]
)}
</div>
);
return (
<Popup
class={bem({ 'safe-area-inset-bottom': props.safeAreaInsetBottom })}
value={props.value}
position="bottom"
overlay={props.overlay}
lazyRender={props.lazyRender}
getContainer={props.getContainer}
closeOnClickOverlay={props.closeOnClickOverlay}
onInput={(value: boolean) => {
emit(ctx, 'input', value);
}}
{...inherit(ctx)}
>
{title ? Header() : props.actions.map(Option)}
{slots.default && <div class={bem('content')}>{slots.default()}</div>}
{cancelText && (
<div class={bem('cancel')} onClick={onCancel}>
{cancelText}
</div>
)}
</Popup>
);
}
ActionSheet.props = {
...PopupMixin.props,
title: String,
actions: Array,
cancelText: String,
safeAreaInsetBottom: Boolean,
overlay: {
type: Boolean,
default: true
},
closeOnClickOverlay: {
type: Boolean,
default: true
}
};
export default sfc<ActionSheetProps>(ActionSheet);

View File

@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders demo correctly 1`] = `
<div>
<div><button class="van-button van-button--default van-button--normal"><span class="van-button__text">弹出 ActionSheet</span></button>
<!---->
</div>
<div><button class="van-button van-button--default van-button--normal"><span class="van-button__text">弹出带取消按钮的 ActionSheet</span></button>
<!---->
</div>
<div><button class="van-button van-button--default van-button--normal"><span class="van-button__text">弹出带标题的 ActionSheet</span></button>
<!---->
</div>
</div>
`;

View File

@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`callback events 1`] = `
<div class="van-popup van-popup--bottom van-action-sheet" name="van-popup-slide-bottom">
<div class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></div>
<div class="van-action-sheet__item van-action-sheet__item--disabled van-hairline--top"><span class="van-action-sheet__name">Option</span></div>
<div class="van-action-sheet__cancel">Cancel</div>
</div>
`;
exports[`disable lazy-render 1`] = `
<div class="van-popup van-popup--bottom van-action-sheet" style="display: none;" name="van-popup-slide-bottom">
<div class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></div>
<div class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></div>
<div class="van-action-sheet__cancel">Cancel</div>
</div>
`;

View File

@@ -0,0 +1,4 @@
import Demo from '../demo';
import demoTest from '../../../test/demo-test';
demoTest(Demo);

View File

@@ -0,0 +1,66 @@
import { mount } from '../../../test/utils';
import ActionSheet from '..';
test('callback events', () => {
const callback = jest.fn();
const onInput = jest.fn();
const onCancel = jest.fn();
const onSelect = jest.fn();
const actions = [
{ name: 'Option', callback },
{ name: 'Option', disabled: true }
];
const wrapper = mount(ActionSheet, {
propsData: {
value: true,
actions,
cancelText: 'Cancel'
},
context: {
on: {
input: onInput,
cancel: onCancel,
select: onSelect
}
}
});
const options = wrapper.findAll('.van-action-sheet__item');
options.at(0).trigger('click');
options.at(1).trigger('click');
wrapper.find('.van-action-sheet__cancel').trigger('click');
expect(callback).toHaveBeenCalled();
expect(onCancel).toHaveBeenCalled();
expect(onInput).toHaveBeenCalledWith(false);
expect(onSelect).toHaveBeenCalledWith(actions[0], 0);
expect(wrapper).toMatchSnapshot();
});
test('disable lazy-render', () => {
const wrapper = mount(ActionSheet, {
propsData: {
lazyRender: false,
actions: [
{ name: 'Option' },
{ name: 'Option' }
],
cancelText: 'Cancel'
}
});
expect(wrapper).toMatchSnapshot();
});
test('get container', () => {
const wrapper = mount(ActionSheet, {
propsData: {
value: true,
getContainer: 'body'
}
});
expect(wrapper.vm.$el.parentNode).toEqual(document.body);
});

View File

@@ -0,0 +1,112 @@
## ActionSheet 上拉菜单
### 使用指南
``` javascript
import { ActionSheet } from 'vant';
Vue.use(ActionSheet);
```
### 代码演示
#### 基础用法
需要传入一个`actions`的数组,数组的每一项是一个对象,对象属性见文档下方表格。
```html
<van-action-sheet
v-model="show"
:actions="actions"
@select="onSelect"
/>
```
```javascript
export default {
data() {
return {
show: false,
actions: [
{
name: '选项'
},
{
name: '选项',
subname: '描述信息'
},
{
loading: true
},
{
name: '禁用选项',
disabled: true
}
]
};
},
methods: {
onSelect(item) {
// 点击选项时默认不会关闭菜单,可以手动关闭
this.show = false;
Toast(item.name);
}
}
}
```
#### 带取消按钮的 ActionSheet
如果传入了`cancelText`属性,且不为空,则会在下方显示一个取消按钮,点击会将当前`ActionSheet`关闭。
```html
<van-action-sheet
v-model="show"
:actions="actions"
cancel-text="取消"
@select="onSelect"
@cancel="onCancel"
/>
```
#### 带标题的 ActionSheet
如果传入了`title`属性,且不为空,则另外一种样式的`ActionSheet`,里面内容需要自定义。
```html
<van-action-sheet v-model="show" title="支持以下配送方式">
<p>一些内容</p>
</van-action-sheet>
```
### API
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|------|------|------|------|------|
| actions | 菜单选项 | `Array` | `[]` | - |
| title | 标题 | `String` | - | - |
| cancel-text | 取消按钮文字,为空时不展示取消按钮 | `String` | - | - |
| overlay | 是否显示遮罩层 | `Boolean` | `true` | - |
| close-on-click-overlay | 是否在点击蒙层后关闭 | `Boolean` | `true` | - |
| lazy-render | 是否在显示弹层时才渲染节点 | `Boolean` | `true` | 1.1.11 |
| get-container | 指定挂载的节点,可以传入选择器,<br>或一个返回节点的函数 | `String | () => HTMLElement` | - | - |
| safe-area-inset-bottom | 是否开启 iPhone X 底部安全区适配,需要在 `viewport` meta 标签中设置 `viewport-fit=cover` | `Boolean` | `false` | 1.6.15 |
### Event
| 事件名 | 说明 | 参数 |
|------|------|------|
| select | 选中选项时触发,禁用或加载状态下不会触发 | item: 选项对应的对象, index: 选择对应的索引 |
| cancel | 取消按钮点击时触发 | - |
### actions
`API`中的`actions`为一个对象数组,数组中的每一个对象配置每一列,每一列有以下`key`
| key | 说明 |
|------|------|
| name | 标题 |
| subname | 二级标题 |
| className | 为对应列添加额外的 class |
| loading | 是否为加载状态 |
| disabled | 是否为禁用状态 |