Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复 onShareAppMessage 拿不到参数的问题
Browse files Browse the repository at this point in the history
Close #169
  • Loading branch information
yesmeck committed Aug 27, 2019
1 parent 249fd07 commit 9189f01
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
6 changes: 5 additions & 1 deletion packages/remax/src/__tests__/alipay/helpers/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ class Page {
}

shareAppMessage() {
this.config.onShareAppMessage();
this.config.onShareAppMessage({
from: 'menu',
target: undefined,
webViewUrl: 'https://www.alipay.com',
});
}

titleClick() {
Expand Down
8 changes: 6 additions & 2 deletions packages/remax/src/__tests__/alipay/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ describe('page', () => {
log.push('usePageScroll');
});

useShareAppMessage(() => {
useShareAppMessage(object => {
log.push(object.from);
log.push('useShareAppMessage');
});

Expand Down Expand Up @@ -90,6 +91,7 @@ describe('page', () => {
'usePullIntercept',
'useReachBottom',
'usePageScroll',
'menu',
'useShareAppMessage',
'useTitleClick',
'useOptionMenuClick',
Expand Down Expand Up @@ -170,7 +172,8 @@ describe('page', () => {
log.push('onPageScroll');
}

onShareAppMessage() {
onShareAppMessage(object: any) {
log.push(object.from);
log.push('onShareAppMessage');
}

Expand Down Expand Up @@ -216,6 +219,7 @@ describe('page', () => {
'onPullIntercept',
'onReachBottom',
'onPageScroll',
'menu',
'onShareAppMessage',
'onTitleClick',
'onOptionMenuClick',
Expand Down
10 changes: 5 additions & 5 deletions packages/remax/src/createPageConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ export default function createPageConfig(Page: React.ComponentType<any>) {
};
},

callLifecycle(lifecycle: Lifecycle) {
callLifecycle(lifecycle: Lifecycle, ...args: any[]) {
const callbacks = this.lifecycleCallback[lifecycle] || [];
let result;
callbacks.forEach((callback: any) => {
result = callback();
result = callback.call(null, ...args);
});
if (result) {
return result;
}

const callback = callbackName(lifecycle);
if (this.wrapper[callback]) {
return this.wrapper[callback]();
return this.wrapper[callback](...args);
}
},

Expand All @@ -86,8 +86,8 @@ export default function createPageConfig(Page: React.ComponentType<any>) {
return this.callLifecycle(Lifecycle.pageScroll);
},

onShareAppMessage() {
return this.callLifecycle(Lifecycle.shareAppMessage);
onShareAppMessage(options: any) {
return this.callLifecycle(Lifecycle.shareAppMessage, options);
},

onTitleClick() {
Expand Down
8 changes: 4 additions & 4 deletions packages/remax/src/createPageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ export default function createPageWrapper(

Object.keys(Lifecycle).forEach(phase => {
const callback = callbackName(phase);
(this as any)[callback] = () => {
return this.callLifecycle(phase);
(this as any)[callback] = (...args: any[]) => {
return this.callLifecycle(phase, ...args);
};
});
}

callLifecycle(phase: string) {
callLifecycle(phase: string, ...args: any[]) {
const callback = callbackName(phase);
if (this.instance && typeof this.instance[callback] === 'function') {
return this.instance[callback]();
return this.instance[callback](...args);
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/remax/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export function usePageScroll(callback: Callback) {
}, []);
}

export function useShareAppMessage(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.shareAppMessage, callback);
}, []);
export function useShareAppMessage(
callback: (o: {
from: 'button' | 'menu';
target: object | undefined;
webViewUrl: string;
}) => any
) {
registerLifecycle(Lifecycle.shareAppMessage, callback);
}

export function useTitleClick(callback: Callback) {
Expand Down
4 changes: 2 additions & 2 deletions packages/remax/src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import capitalize from './utils/capitalize';

declare const getCurrentPages: any;

export type Callback = () => any;
export type Callback = (...args: any[]) => any;

export enum Lifecycle {
show = 'show',
Expand All @@ -25,7 +25,7 @@ export function callbackName(name: string) {
return 'on' + capitalize(name);
}

export function registerLifecycle(method: Lifecycle, callback: () => void) {
export function registerLifecycle(method: Lifecycle, callback: Callback) {
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
return currentPage.registerLifecycle(method, callback);
Expand Down

0 comments on commit 9189f01

Please sign in to comment.