feat(ActionSheet): add description prop (#4691)

This commit is contained in:
neverland 2019-10-11 15:28:52 +08:00 committed by GitHub
parent 79322ea892
commit 9f523682e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 133 additions and 36 deletions

View File

@ -16,11 +16,7 @@ Vue.use(ActionSheet);
Use `actions` prop to set options of action-sheet. Use `actions` prop to set options of action-sheet.
```html ```html
<van-action-sheet <van-action-sheet v-model="show" :actions="actions" @select="onSelect" />
v-model="show"
:actions="actions"
@select="onSelect"
/>
``` ```
```javascript ```javascript
@ -48,10 +44,7 @@ export default {
### Status ### Status
```html ```html
<van-action-sheet <van-action-sheet v-model="show" :actions="actions" />
v-model="show"
:actions="actions"
/>
``` ```
```javascript ```javascript
@ -76,11 +69,37 @@ export default {
v-model="show" v-model="show"
:actions="actions" :actions="actions"
cancel-text="Cancel" cancel-text="Cancel"
@select="onSelect"
@cancel="onCancel" @cancel="onCancel"
/> />
``` ```
```js
export default {
data() {
return {
show: false
};
},
methods: {
onCancel() {
this.show = false;
Toast('cancel');
}
}
}
```
### Show Description
```html
<van-action-sheet
v-model="show"
:actions="actions"
description="Description"
/>
```
### ActionSheet with title ### ActionSheet with title
```html ```html
@ -98,6 +117,7 @@ export default {
| actions | Options | *Action[]* | `[]` | - | | actions | Options | *Action[]* | `[]` | - |
| title | Title | *string* | - | - | | title | Title | *string* | - | - |
| cancel-text | Text of cancel button | *string* | - | - | | cancel-text | Text of cancel button | *string* | - | - |
| description | Description above the options | *string* | - | 2.2.8 |
| overlay | Whether to show overlay | *boolean* | `true` | - | | overlay | Whether to show overlay | *boolean* | `true` | - |
| round | Whether to show round corner | *boolean* | `true` | 2.0.9 | | round | Whether to show round corner | *boolean* | `true` | 2.0.9 |
| close-on-click-action | Whether to close when click action | *boolean* | `false` | - | | close-on-click-action | Whether to close when click action | *boolean* | `false` | - |

View File

@ -16,11 +16,7 @@ Vue.use(ActionSheet);
`ActionSheet`通过`actions`数组来定义展示的选项,数组的每一项是一个对象,对象属性见文档下方表格。 `ActionSheet`通过`actions`数组来定义展示的选项,数组的每一项是一个对象,对象属性见文档下方表格。
```html ```html
<van-action-sheet <van-action-sheet v-model="show" :actions="actions" @select="onSelect" />
v-model="show"
:actions="actions"
@select="onSelect"
/>
``` ```
```javascript ```javascript
@ -38,7 +34,8 @@ export default {
methods: { methods: {
onSelect(item) { onSelect(item) {
// 点击选项时默认不会关闭菜单,可以手动关闭 // 默认情况下,点击选项时不会自动关闭菜单
// 可以通过 close-on-click-action 属性开启自动关闭
this.show = false; this.show = false;
Toast(item.name); Toast(item.name);
} }
@ -48,13 +45,10 @@ export default {
### 选项状态 ### 选项状态
选项可以设置为加载状态或禁用状态 选项可以设置为加载状态或禁用状态,也可以通过`color`设置选项颜色
```html ```html
<van-action-sheet <van-action-sheet v-model="show" :actions="actions" />
v-model="show"
:actions="actions"
/>
``` ```
```javascript ```javascript
@ -81,11 +75,39 @@ export default {
v-model="show" v-model="show"
:actions="actions" :actions="actions"
cancel-text="取消" cancel-text="取消"
@select="onSelect"
@cancel="onCancel" @cancel="onCancel"
/> />
``` ```
```js
export default {
data() {
return {
show: false
};
},
methods: {
onCancel() {
this.show = false;
Toast('cancel');
}
}
}
```
### 展示描述信息
设置`description`属性后,会在选项上方显示描述信息
```html
<van-action-sheet
v-model="show"
:actions="actions"
description="这是一段描述信息"
/>
```
### 展示标题栏 ### 展示标题栏
通过设置`title`属性展示标题栏,同时可以使用插槽自定义菜单内容 通过设置`title`属性展示标题栏,同时可以使用插槽自定义菜单内容
@ -110,8 +132,9 @@ export default {
| 参数 | 说明 | 类型 | 默认值 | 版本 | | 参数 | 说明 | 类型 | 默认值 | 版本 |
|------|------|------|------|------| |------|------|------|------|------|
| actions | 菜单选项 | *Action[]* | `[]` | - | | actions | 菜单选项 | *Action[]* | `[]` | - |
| title | 标题 | *string* | - | - | | title | 顶部标题 | *string* | - | - |
| cancel-text | 取消按钮文字 | *string* | - | - | | cancel-text | 取消按钮文字 | *string* | - | - |
| description | 选项上方的描述信息 | *string* | - | 2.2.8 |
| overlay | 是否显示遮罩层 | *boolean* | `true` | - | | overlay | 是否显示遮罩层 | *boolean* | `true` | - |
| round | 是否显示圆角 | *boolean* | `true` | 2.0.9 | | round | 是否显示圆角 | *boolean* | `true` | 2.0.9 |
| close-on-click-action | 是否在点击选项后关闭 | *boolean* | `false` | - | | close-on-click-action | 是否在点击选项后关闭 | *boolean* | `false` | - |

View File

@ -10,7 +10,7 @@
<van-action-sheet v-model="show2" close-on-click-action :actions="statusActions" /> <van-action-sheet v-model="show2" close-on-click-action :actions="statusActions" />
</demo-block> </demo-block>
<demo-block :title="$t('title2')"> <demo-block :title="$t('showCancel')">
<van-button type="primary" @click="show3 = true">{{ $t('buttonText') }}</van-button> <van-button type="primary" @click="show3 = true">{{ $t('buttonText') }}</van-button>
<van-action-sheet <van-action-sheet
v-model="show3" v-model="show3"
@ -21,9 +21,19 @@
/> />
</demo-block> </demo-block>
<demo-block :title="$t('title3')"> <demo-block :title="$t('showDescription')">
<van-button type="primary" @click="show4 = true">{{ $t('buttonText') }}</van-button> <van-button type="primary" @click="show4 = true">{{ $t('buttonText') }}</van-button>
<van-action-sheet v-model="show4" :title="$t('title')"> <van-action-sheet
v-model="show4"
:actions="simpleActions"
close-on-click-action
:description="$t('description')"
/>
</demo-block>
<demo-block :title="$t('showTitle')">
<van-button type="primary" @click="show5 = true">{{ $t('buttonText') }}</van-button>
<van-action-sheet v-model="show5" :title="$t('title')">
<p>{{ $t('content') }}</p> <p>{{ $t('content') }}</p>
</van-action-sheet> </van-action-sheet>
</demo-block> </demo-block>
@ -37,19 +47,23 @@ export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
buttonText: '弹出菜单', buttonText: '弹出菜单',
title2: '展示取消按钮',
title3: '展示标题栏',
status: '选项状态', status: '选项状态',
description: '描述信息', subname: '副文本',
disabledOption: '禁用选项' disabledOption: '禁用选项',
showTitle: '展示标题栏',
showCancel: '展示取消按钮',
showDescription: '展示描述信息',
description: '这是一段描述信息'
}, },
'en-US': { 'en-US': {
buttonText: 'Show ActionSheet', buttonText: 'Show ActionSheet',
title2: 'ActionSheet with cancel button',
title3: 'ActionSheet with title',
status: 'Status', status: 'Status',
description: 'Description', subname: 'Subname',
disabledOption: 'Disabled Option' disabledOption: 'Disabled Option',
showTitle: 'ActionSheet with title',
showCancel: 'ActionSheet with cancel button',
showDescription: 'ActionSheet with description',
description: 'Description'
} }
}, },
@ -58,7 +72,8 @@ export default {
show1: false, show1: false,
show2: false, show2: false,
show3: false, show3: false,
show4: false show4: false,
show5: false
}; };
}, },
@ -67,7 +82,7 @@ export default {
return [ return [
{ name: this.$t('option') }, { name: this.$t('option') },
{ name: this.$t('option') }, { name: this.$t('option') },
{ name: this.$t('option'), subname: this.$t('description') } { name: this.$t('option'), subname: this.$t('subname') }
]; ];
}, },

View File

@ -51,6 +51,14 @@
text-align: center; text-align: center;
} }
&__description {
padding: @padding-md;
color: @action-sheet-description-color;
font-size: @action-sheet-description-font-size;
line-height: @action-sheet-description-line-height;
text-align: center;
}
&__close { &__close {
position: absolute; position: absolute;
top: 0; top: 0;

View File

@ -27,6 +27,7 @@ export type ActionSheetProps = PopupMixinProps & {
actions?: ActionSheetItem[]; actions?: ActionSheetItem[];
duration: number; duration: number;
cancelText?: string; cancelText?: string;
description?: string;
closeOnClickAction?: boolean; closeOnClickAction?: boolean;
safeAreaInsetBottom?: boolean; safeAreaInsetBottom?: boolean;
}; };
@ -116,6 +117,10 @@ function ActionSheet(
} }
} }
const Description = props.description && (
<div class={bem('description')}>{props.description}</div>
);
return ( return (
<Popup <Popup
class={bem()} class={bem()}
@ -132,6 +137,7 @@ function ActionSheet(
{...inherit(ctx, true)} {...inherit(ctx, true)}
> >
{Header()} {Header()}
{Description}
{props.actions && props.actions.map(Option)} {props.actions && props.actions.map(Option)}
{Content()} {Content()}
{CancelText()} {CancelText()}
@ -145,6 +151,7 @@ ActionSheet.props = {
actions: Array, actions: Array,
duration: Number, duration: Number,
cancelText: String, cancelText: String,
description: String,
getContainer: [String, Function], getContainer: [String, Function],
closeOnClickAction: Boolean, closeOnClickAction: Boolean,
round: { round: {

View File

@ -14,5 +14,8 @@ exports[`renders demo correctly 1`] = `
<div><button class="van-button van-button--primary van-button--normal"><span class="van-button__text">弹出菜单</span></button> <div><button class="van-button van-button--primary van-button--normal"><span class="van-button__text">弹出菜单</span></button>
<!----> <!---->
</div> </div>
<div><button class="van-button van-button--primary van-button--normal"><span class="van-button__text">弹出菜单</span></button>
<!---->
</div>
</div> </div>
`; `;

View File

@ -8,6 +8,12 @@ exports[`callback events 1`] = `
exports[`color option 1`] = `<div class="van-popup van-popup--round van-popup--bottom van-popup--safe-area-inset-bottom van-action-sheet" name="van-popup-slide-bottom"><button class="van-action-sheet__item van-hairline--top" style="color: red;"><span class="van-action-sheet__name">Option</span></button></div>`; exports[`color option 1`] = `<div class="van-popup van-popup--round van-popup--bottom van-popup--safe-area-inset-bottom van-action-sheet" name="van-popup-slide-bottom"><button class="van-action-sheet__item van-hairline--top" style="color: red;"><span class="van-action-sheet__name">Option</span></button></div>`;
exports[`description prop 1`] = `
<div class="van-popup van-popup--round van-popup--bottom van-popup--safe-area-inset-bottom van-action-sheet" name="van-popup-slide-bottom">
<div class="van-action-sheet__description">This is a description</div><button class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></button>
</div>
`;
exports[`disable lazy-render 1`] = `<div class="van-popup van-popup--round van-popup--bottom van-popup--safe-area-inset-bottom van-action-sheet" style="display: none;" name="van-popup-slide-bottom"><button class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></button><button class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></button><button class="van-action-sheet__cancel">Cancel</button></div>`; exports[`disable lazy-render 1`] = `<div class="van-popup van-popup--round van-popup--bottom van-popup--safe-area-inset-bottom van-action-sheet" style="display: none;" name="van-popup-slide-bottom"><button class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></button><button class="van-action-sheet__item van-hairline--top"><span class="van-action-sheet__name">Option</span></button><button class="van-action-sheet__cancel">Cancel</button></div>`;
exports[`render title and default slot 1`] = ``; exports[`render title and default slot 1`] = ``;

View File

@ -162,3 +162,15 @@ test('color option', () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
test('description prop', () => {
const wrapper = mount(ActionSheet, {
propsData: {
value: true,
description: 'This is a description',
actions: [{ name: 'Option' }]
}
});
expect(wrapper).toMatchSnapshot();
});

View File

@ -49,6 +49,9 @@
@action-sheet-max-height: 90%; @action-sheet-max-height: 90%;
@action-sheet-header-height: 44px; @action-sheet-header-height: 44px;
@action-sheet-header-font-size: @font-size-lg; @action-sheet-header-font-size: @font-size-lg;
@action-sheet-description-color: @gray-darker;
@action-sheet-description-font-size: @font-size-md;
@action-sheet-description-line-height: 20px;
@action-sheet-item-height: 50px; @action-sheet-item-height: 50px;
@action-sheet-item-background: @white; @action-sheet-item-background: @white;
@action-sheet-item-font-size: @font-size-lg; @action-sheet-item-font-size: @font-size-lg;