feat(Notify): add type prop (#4237)

This commit is contained in:
neverland 2019-08-26 09:48:43 +08:00 committed by GitHub
parent ae53f9b58f
commit 10b52963b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 151 additions and 44 deletions

View File

@ -1,5 +1,5 @@
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { RED, WHITE } from '../utils/constant'; import { WHITE } from '../utils/constant';
import { inherit } from '../utils/functional'; import { inherit } from '../utils/functional';
import { PopupMixin } from '../mixins/popup'; import { PopupMixin } from '../mixins/popup';
import Popup from '../popup'; import Popup from '../popup';
@ -10,6 +10,7 @@ import { DefaultSlots } from '../utils/types';
import { PopupMixinProps } from '../mixins/popup/type'; import { PopupMixinProps } from '../mixins/popup/type';
export type NotifyProps = PopupMixinProps & { export type NotifyProps = PopupMixinProps & {
type: 'primary' | 'success' | 'danger' | 'warning';
color: string; color: string;
message: string | number; message: string | number;
duration: number; duration: number;
@ -37,7 +38,7 @@ function Notify(
position="top" position="top"
overlay={false} overlay={false}
lockScroll={false} lockScroll={false}
class={[bem(), props.className]} class={[bem([props.type]), props.className]}
{...inherit(ctx, true)} {...inherit(ctx, true)}
> >
{props.message} {props.message}
@ -47,17 +48,18 @@ function Notify(
Notify.props = { Notify.props = {
...PopupMixin.props, ...PopupMixin.props,
background: String,
className: null as any, className: null as any,
message: [Number, String], message: [Number, String],
getContainer: [String, Function], getContainer: [String, Function],
type: {
type: String,
default: 'danger'
},
color: { color: {
type: String, type: String,
default: WHITE default: WHITE
}, },
background: {
type: String,
default: RED
},
duration: { duration: {
type: Number, type: Number,
default: 3000 default: 3000

View File

@ -17,13 +17,27 @@ Vue.use(Notify);
Notify('Notify Message'); Notify('Notify Message');
``` ```
### Custom Config ### Notify Type
```js
Notify({ type: 'primary', message: 'Notify Message' });
Notify({ type: 'success', message: 'Notify Message' });
Notify({ type: 'danger', message: 'Notify Message' });
Notify({ type: 'warning', message: 'Notify Message' });
```
### Custom Notify
```js ```js
Notify({ Notify({
message: 'Notify Message', message: 'Custom Color',
duration: 1000, color: '#ad0000',
background: '#1989fa' background: '#ffe1e1'
});
Notify({
message: 'Custom Duration',
duration: 1000
}); });
``` ```
@ -54,6 +68,7 @@ export default {
| Attribute | Description | Type | Default | | Attribute | Description | Type | Default |
|------|------|------|------| |------|------|------|------|
| type | Can be set to `primary` `info` `warning` | `string` | `danger` |
| message | Message | `string` | - | | message | Message | `string` | - |
| duration | Duration(ms), won't disappear if value is 0 | `number` | `3000` | | duration | Duration(ms), won't disappear if value is 0 | `number` | `3000` |
| color | Message color | `string` | `#fff` | | | color | Message color | `string` | `#fff` | |

View File

@ -17,13 +17,38 @@ Vue.use(Notify);
Notify('通知内容'); Notify('通知内容');
``` ```
### 自定义配置 ### 通知类型
支持`primary``success``warning``danger`四种通知类型,默认为`danger`
```js
// 主要通知
Notify({ type: 'primary', message: '通知内容' });
// 成功通知
Notify({ type: 'success', message: '通知内容' });
// 危险通知
Notify({ type: 'danger', message: '通知内容' });
// 警告通知
Notify({ type: 'warning', message: '通知内容' });
```
### 自定义通知
自定义消息通知的颜色和展示时长
```js ```js
Notify({ Notify({
message: '通知内容', message: '自定义颜色',
duration: 1000, color: '#ad0000',
background: '#1989fa' background: '#ffe1e1'
});
Notify({
message: '自定义时长',
duration: 1000
}); });
``` ```
@ -54,6 +79,7 @@ export default {
| 参数 | 说明 | 类型 | 默认值 | 版本 | | 参数 | 说明 | 类型 | 默认值 | 版本 |
|------|------|------|------|------| |------|------|------|------|------|
| type | 类型,可选值为 `primary` `info` `warning` | `string` | `danger` | 2.1.6 |
| message | 展示文案,支持通过`\n`换行 | `string` | - | - | | message | 展示文案,支持通过`\n`换行 | `string` | - | - |
| duration | 展示时长(ms),值为 0 时notify 不会消失 | `number` | `3000` | - | | duration | 展示时长(ms),值为 0 时notify 不会消失 | `number` | `3000` | - |
| color | 字体颜色 | `string` | `#fff` | - | | color | 字体颜色 | `string` | `#fff` | - |

View File

@ -1,19 +1,22 @@
<template> <template>
<demo-section> <demo-section>
<demo-block :title="$t('basicUsage')"> <demo-block :title="$t('basicUsage')">
<van-button <van-button type="danger" :text="$t('basicUsage')" @click="showNotify" />
type="primary"
:text="$t('showNotify')"
@click="showNotify"
/>
</demo-block> </demo-block>
<demo-block :title="$t('customConfig')"> <demo-block :title="$t('notifyType')">
<van-button <div style="margin-bottom: 15px;">
type="primary" <van-button type="info" :text="$t('primary')" @click="showType('primary')" />
:text="$t('showCustomNotify')" <van-button type="primary" :text="$t('success')" @click="showType('success')" />
@click="showCustomNotify" </div>
/>
<van-button type="danger" :text="$t('danger')" @click="showType('danger')" />
<van-button type="warning" :text="$t('warning')" @click="showType('warning')" />
</demo-block>
<demo-block :title="$t('customNotify')">
<van-button type="primary" :text="$t('customColor')" @click="showCustomColor" />
<van-button type="primary" :text="$t('customDuration')" @click="showCustomDuration" />
</demo-block> </demo-block>
</demo-section> </demo-section>
</template> </template>
@ -22,16 +25,26 @@
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
primary: '主要通知',
success: '成功通知',
danger: '危险通知',
warning: '警告通知',
content: '通知内容', content: '通知内容',
customConfig: '自定义配置', notifyType: '通知类型',
showNotify: '显示消息通知', customColor: '自定义颜色',
showCustomNotify: '显示自定义消息通知' customNotify: '自定义配置',
customDuration: '自定义时长'
}, },
'en-US': { 'en-US': {
primary: 'Primary',
success: 'Success',
danger: 'Danger',
warning: 'Warning',
content: 'Notify Message', content: 'Notify Message',
customConfig: 'Custom Config', notifyType: 'Notify Type',
showNotify: 'Show Notify', customColor: 'Custom Color',
showCustomNotify: 'Show Custom Notify' customNotify: 'Custom Notify',
customDuration: 'Custom Duration'
} }
}, },
@ -40,11 +53,25 @@ export default {
this.$notify(this.$t('content')); this.$notify(this.$t('content'));
}, },
showCustomNotify() { showCustomColor() {
this.$notify({
message: this.$t('customColor'),
color: '#ad0000',
background: '#ffe1e1'
});
},
showCustomDuration() {
this.$notify({
message: this.$t('customDuration'),
duration: 1000
});
},
showType(type) {
this.$notify({ this.$notify({
message: this.$t('content'), message: this.$t('content'),
duration: 1000, type
background: '#1989fa'
}); });
} }
} }
@ -52,7 +79,7 @@ export default {
</script> </script>
<style lang="less"> <style lang="less">
@import "../../style/var"; @import '../../style/var';
.demo-notify { .demo-notify {
background-color: @white; background-color: @white;

View File

@ -9,4 +9,20 @@
// allow newline charactor // allow newline charactor
white-space: pre-wrap; white-space: pre-wrap;
text-align: center; text-align: center;
&--primary {
background-color: @notify-primary-background-color;
}
&--success {
background-color: @notify-success-background-color;
}
&--danger {
background-color: @notify-danger-background-color;
}
&--warning {
background-color: @notify-warning-background-color;
}
} }

View File

@ -1,6 +1,6 @@
import Vue from 'vue'; import Vue from 'vue';
import VanNotify from './Notify'; import VanNotify from './Notify';
import { RED, WHITE } from '../utils/constant'; import { WHITE } from '../utils/constant';
import { isObj, isServer } from '../utils'; import { isObj, isServer } from '../utils';
import { mount } from '../utils/functional'; import { mount } from '../utils/functional';
import { NotifyOptions } from 'types/notify'; import { NotifyOptions } from 'types/notify';
@ -57,10 +57,11 @@ function Notify(options: NotifyOptions) {
function defaultOptions(): NotifyOptions { function defaultOptions(): NotifyOptions {
return { return {
type: 'danger',
value: true, value: true,
message: '', message: '',
color: WHITE, color: WHITE,
background: RED, background: undefined,
duration: 3000, duration: 3000,
className: '', className: '',
onClose: null, onClose: null,

View File

@ -2,7 +2,10 @@
exports[`renders demo correctly 1`] = ` exports[`renders demo correctly 1`] = `
<div> <div>
<div><button class="van-button van-button--primary van-button--normal"><span class="van-button__text">显示消息通知</span></button></div> <div><button class="van-button van-button--danger van-button--normal"><span class="van-button__text">基础用法</span></button></div>
<div><button class="van-button van-button--primary van-button--normal"><span class="van-button__text">显示自定义消息通知</span></button></div> <div>
<div style="margin-bottom: 15px;"><button class="van-button van-button--info van-button--normal"><span class="van-button__text">主要通知</span></button> <button class="van-button van-button--primary van-button--normal"><span class="van-button__text">成功通知</span></button></div> <button class="van-button van-button--danger van-button--normal"><span class="van-button__text">危险通知</span></button> <button class="van-button van-button--warning van-button--normal"><span class="van-button__text">警告通知</span></button>
</div>
<div><button class="van-button van-button--primary van-button--normal"><span class="van-button__text">自定义颜色</span></button> <button class="van-button van-button--primary van-button--normal"><span class="van-button__text">自定义时长</span></button></div>
</div> </div>
`; `;

View File

@ -1,11 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`create a notify 1`] = `<div class="van-popup van-popup--top van-notify" style="color: rgb(255, 255, 255); background: rgb(255, 68, 68); z-index: 2001;" name="van-popup-slide-top">test</div>`; exports[`create a notify 1`] = `<div class="van-popup van-popup--top van-notify van-notify--danger" style="color: rgb(255, 255, 255); z-index: 2001;" name="van-popup-slide-top">test</div>`;
exports[`notify disappear 1`] = `<div class="van-popup van-popup--top van-notify" style="color: red; z-index: 2001; background: blue;" name="van-popup-slide-top">test</div>`; exports[`notify disappear 1`] = `<div class="van-popup van-popup--top van-notify van-notify--danger" style="color: red; z-index: 2001; background: blue;" name="van-popup-slide-top">test</div>`;
exports[`notify disappear 2`] = `<div class="van-popup van-popup--top van-notify" style="color: red; z-index: 2001; background: blue; display: none;" name="van-popup-slide-top">test</div>`; exports[`notify disappear 2`] = `<div class="van-popup van-popup--top van-notify van-notify--danger" style="color: red; z-index: 2001; background: blue; display: none;" name="van-popup-slide-top">test</div>`;
exports[`notify disappear 3`] = `<div class="van-popup van-popup--top van-notify" style="color: rgb(255, 255, 255); z-index: 2002; background: rgb(255, 68, 68);" name="van-popup-slide-top">text2</div>`; exports[`notify disappear 3`] = `<div class="van-popup van-popup--top van-notify van-notify--danger" style="color: rgb(255, 255, 255); z-index: 2002;" name="van-popup-slide-top">text2</div>`;
exports[`notify disappear 4`] = `<div class="van-popup van-popup--top van-notify" style="color: rgb(255, 255, 255); z-index: 2002; background: rgb(255, 68, 68); display: none;" name="van-popup-slide-top">text2</div>`; exports[`notify disappear 4`] = `<div class="van-popup van-popup--top van-notify van-notify--danger" style="color: rgb(255, 255, 255); z-index: 2002; display: none;" name="van-popup-slide-top">text2</div>`;
exports[`type prop 1`] = `<div class="van-popup van-popup--top van-notify van-notify--primary" style="color: rgb(255, 255, 255); z-index: 2001;" name="van-popup-slide-top">test</div>`;

View File

@ -11,6 +11,16 @@ test('create a notify', async () => {
expect(notify.$el.outerHTML).toMatchSnapshot(); expect(notify.$el.outerHTML).toMatchSnapshot();
}); });
test('type prop', async () => {
const notify = Notify({
message: 'test',
type: 'primary'
});
await later();
expect(notify.$el.outerHTML).toMatchSnapshot();
});
test('notify disappear', async () => { test('notify disappear', async () => {
const onClose = jest.fn(); const onClose = jest.fn();
const notify = Notify({ const notify = Notify({

View File

@ -360,6 +360,10 @@
@notify-padding: @padding-xs @padding-md; @notify-padding: @padding-xs @padding-md;
@notify-font-size: @font-size-md; @notify-font-size: @font-size-md;
@notify-line-height: 20px; @notify-line-height: 20px;
@notify-primary-background-color: @blue;
@notify-success-background-color: @green;
@notify-danger-background-color: @red;
@notify-warning-background-color: @orange;
// NumberKeyboard // NumberKeyboard
@number-keyboard-background-color: @white; @number-keyboard-background-color: @white;

1
types/notify.d.ts vendored
View File

@ -3,6 +3,7 @@ import Vue from 'vue';
export type NotifyMessage = string | number; export type NotifyMessage = string | number;
export type NotifyOptions = { export type NotifyOptions = {
type?: 'primary' | 'success' | 'danger' | 'warning';
value?: boolean; value?: boolean;
color?: string; color?: string;
message?: NotifyMessage; message?: NotifyMessage;