Skip to content

Commit da91631

Browse files
authored
[improvement] Notify: tsx & functional (youzan#2810)
1 parent 52e6fd2 commit da91631

File tree

10 files changed

+176
-131
lines changed

10 files changed

+176
-131
lines changed

packages/actionsheet/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ function Actionsheet(
9090
class={bem()}
9191
value={props.value}
9292
position="bottom"
93+
overlay={props.overlay}
94+
closeOnClickOverlay={props.closeOnClickOverlay}
9395
onInput={(value: boolean) => {
9496
emit(ctx, 'input', value);
9597
}}

packages/notify/Notify.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/notify/Notify.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { use } from '../utils';
2+
import { RED, WHITE } from '../utils/color';
3+
import { emit, inherit } from '../utils/functional';
4+
import { PopupMixin } from '../mixins/popup';
5+
import Popup from '../popup';
6+
7+
// Types
8+
import { CreateElement, RenderContext } from 'vue/types';
9+
import { DefaultSlots } from '../utils/use/sfc';
10+
import { PopupMixinProps } from '../mixins/popup/type';
11+
12+
export type NotifyProps = PopupMixinProps & {
13+
color: string;
14+
message: string | number;
15+
duration: number;
16+
className?: any;
17+
background: string;
18+
};
19+
20+
const [sfc, bem] = use('notify');
21+
22+
function Notify(
23+
h: CreateElement,
24+
props: NotifyProps,
25+
slots: DefaultSlots,
26+
ctx: RenderContext<NotifyProps>
27+
) {
28+
const style = {
29+
color: props.color,
30+
background: props.background
31+
};
32+
33+
return (
34+
<Popup
35+
value={props.value}
36+
style={style}
37+
position="top"
38+
overlay={false}
39+
lockScroll={false}
40+
class={[bem(), props.className]}
41+
onInput={(value: boolean) => {
42+
emit(ctx, 'input', value);
43+
}}
44+
{...inherit(ctx)}
45+
>
46+
{props.message}
47+
</Popup>
48+
);
49+
}
50+
51+
Notify.props = {
52+
...PopupMixin.props,
53+
className: null as any,
54+
message: [String, Number],
55+
color: {
56+
type: String,
57+
default: WHITE
58+
},
59+
background: {
60+
type: String,
61+
default: RED
62+
},
63+
duration: {
64+
type: Number,
65+
default: 3000
66+
}
67+
};
68+
69+
export default sfc<NotifyProps>(Notify);

packages/notify/index.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

packages/notify/index.less

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
@import '../style/var';
22

33
.van-notify {
4-
position: fixed;
5-
top: 0;
6-
width: 100%;
74
text-align: center;
85
box-sizing: border-box;
96
padding: @notify-padding;

packages/notify/index.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import Vue from 'vue';
2+
import VanNotify from './Notify';
3+
import { RED, WHITE } from '../utils/color';
4+
import { isObj, isServer } from '../utils';
5+
import { mount } from '../utils/functional';
6+
import { NotifyOptions } from 'types/notify';
7+
8+
let timer: number | NodeJS.Timeout;
9+
let instance: any;
10+
11+
function parseOptions(message: NotifyOptions): NotifyOptions {
12+
return isObj(message) ? message : ({ message } as NotifyOptions);
13+
}
14+
15+
function Notify(options: NotifyOptions) {
16+
/* istanbul ignore if */
17+
if (isServer) {
18+
return;
19+
}
20+
21+
if (!instance) {
22+
instance = mount(VanNotify);
23+
}
24+
25+
options = {
26+
...Notify.currentOptions,
27+
...parseOptions(options)
28+
};
29+
30+
Object.assign(instance, options);
31+
clearTimeout(timer as NodeJS.Timeout);
32+
33+
if (options.duration && options.duration > 0) {
34+
timer = setTimeout(Notify.clear, options.duration);
35+
}
36+
37+
return instance;
38+
}
39+
40+
function defaultOptions(): NotifyOptions {
41+
return {
42+
value: true,
43+
message: '',
44+
color: WHITE,
45+
background: RED,
46+
duration: 3000,
47+
className: ''
48+
};
49+
}
50+
51+
Notify.clear = () => {
52+
if (instance) {
53+
instance.value = false;
54+
}
55+
};
56+
57+
Notify.currentOptions = defaultOptions();
58+
59+
Notify.setDefaultOptions = (options: NotifyOptions) => {
60+
Object.assign(Notify.currentOptions, options);
61+
};
62+
63+
Notify.resetDefaultOptions = () => {
64+
Notify.currentOptions = defaultOptions();
65+
};
66+
67+
Notify.install = () => {
68+
Vue.use(VanNotify as any);
69+
};
70+
71+
Vue.prototype.$notify = Notify;
72+
73+
export default Notify;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`create a notify 1`] = `<div class="van-notify" style="color: rgb(255, 255, 255); background: rgb(255, 68, 68); z-index: 2000;" name="van-slide-down">test</div>`;
3+
exports[`create a notify 1`] = `<div class="van-popup van-popup--top van-notify" name="van-popup-slide-top" style="color: rgb(255, 255, 255); background: rgb(255, 68, 68); z-index: 2000;">test</div>`;
44

5-
exports[`notify disappear 1`] = `<div class="van-notify" style="color: red; z-index: 2000; background: blue;" name="van-slide-down">test</div>`;
5+
exports[`notify disappear 1`] = `<div class="van-popup van-popup--top van-notify" name="van-popup-slide-top" style="color: red; z-index: 2000; background: blue;">test</div>`;
66

7-
exports[`notify disappear 2`] = `<div class="van-notify" style="color: red; z-index: 2000; background: blue; display: none;" name="van-slide-down">test</div>`;
7+
exports[`notify disappear 2`] = `<div class="van-popup van-popup--top van-notify" name="van-popup-slide-top" style="color: red; z-index: 2000; background: blue; display: none;">test</div>`;
88

9-
exports[`notify disappear 3`] = `<div class="van-notify" style="color: rgb(255, 255, 255); z-index: 2001; background: rgb(255, 68, 68);" name="van-slide-down">text2</div>`;
9+
exports[`notify disappear 3`] = `<div class="van-popup van-popup--top van-notify" name="van-popup-slide-top" style="color: rgb(255, 255, 255); z-index: 2001; background: rgb(255, 68, 68);">text2</div>`;
1010

11-
exports[`notify disappear 4`] = `<div class="van-notify" style="color: rgb(255, 255, 255); z-index: 2001; background: rgb(255, 68, 68); display: none;" name="van-slide-down">text2</div>`;
11+
exports[`notify disappear 4`] = `<div class="van-popup van-popup--top van-notify" name="van-popup-slide-top" style="color: rgb(255, 255, 255); z-index: 2001; background: rgb(255, 68, 68); display: none;">text2</div>`;

packages/sku/type.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable camelcase */
2+
13
export type SkuData = {
24
price: string;
35
none_sku: boolean;

packages/utils/functional.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RenderContext, VNodeData } from 'vue/types';
1+
import Vue, { RenderContext, VNodeData } from 'vue';
22

33
type ObjectIndex = {
44
[key: string]: any;
@@ -21,7 +21,10 @@ const inheritKey = [
2121
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
2222

2323
// inherit partial context, map nativeOn to on
24-
export function inherit(context: Context, inheritListeners?: boolean): InheritContext {
24+
export function inherit(
25+
context: Context,
26+
inheritListeners?: boolean
27+
): InheritContext {
2528
const result = inheritKey.reduce(
2629
(obj, key) => {
2730
if (context.data[key]) {
@@ -53,3 +56,18 @@ export function emit(context: Context, eventName: string, ...args: any[]) {
5356
}
5457
}
5558
}
59+
60+
// mount functional component
61+
export function mount(Component: any) {
62+
const instance = new Vue({
63+
el: document.createElement('div'),
64+
props: Component.props,
65+
render(h) {
66+
return h(Component, { props: this.$props });
67+
}
68+
});
69+
70+
document.body.appendChild(instance.$el);
71+
72+
return instance;
73+
}

types/notify.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Vue from 'vue';
22

3-
type NotifyMessage = string | number;
3+
export type NotifyMessage = string | number;
44

55
export type NotifyOptions = {
6-
message?: NotifyMessage;
6+
value?: boolean;
77
color?: string;
8+
message?: NotifyMessage;
89
duration?: number;
910
className?: any;
1011
background?: string;
@@ -21,6 +22,8 @@ export interface Notify {
2122
(message: NotifyOptions | NotifyMessage): VanNotify;
2223
clear(): void;
2324
install(): void;
25+
currentOptions: NotifyOptions;
26+
defaultOptions: NotifyOptions;
2427
setDefaultOptions(options: NotifyOptions): void;
2528
resetDefaultOptions(): void;
2629
}

0 commit comments

Comments
 (0)