Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listitem组件写法优化 #1124

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiled/alipay/demo/pages/List/index.axml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ant-list-item
image="SetOutline"
arrow="right"
onTap="handleTap"
catchTap="catchTap"
data-info="设置">
设置
</ant-list-item>
Expand Down
7 changes: 7 additions & 0 deletions compiled/alipay/demo/pages/List/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Page({
});
console.log(e);
},
catchTap(e) {
my.alert({
title: 'catchTap',
content: e.currentTarget.dataset.info,
});
console.log(e);
},
handleSetRadius(checked) {
this.setData({
radius: checked,
Expand Down
64 changes: 31 additions & 33 deletions compiled/alipay/src/List/ListItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import { useEvent } from 'functional-mini/component';
import { ISwitchProps } from '../../Switch/props';
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
import {
Component,
triggerEventOnly,
triggerCatchEvent,
} from '../../_util/simply';
import { IListItemProps } from './props';

const ListItem = (props: ISwitchProps) => {
const { alipayForwardCatchEvent, alipayForwardEvent } =
useComponentEvent(props);

useEvent('onTap', (e) => {
if (props.disabled) {
return;
}
alipayForwardEvent('tap', e);
});
useEvent('catchTap', (e) => {
if (props.disabled) {
return;
}
alipayForwardCatchEvent('tap', e);
});
return {};
};

mountComponent<IListItemProps>(ListItem, {
image: '',
title: '',
brief: '',
arrow: null,
extra: '',
extraBrief: '',
disabled: false,
showDivider: true,
});
Component<IListItemProps>(
{
image: '',
title: '',
brief: '',
arrow: false,
extra: '',
extraBrief: '',
disabled: false,
showDivider: true,
},
{
onTap(e) {
if (this.props.disabled) {
return;
}
triggerEventOnly(this, 'tap', e);
},
catchTap(e) {
if (this.props.disabled) {
return;
}
triggerCatchEvent(this, 'catchTap', e);
},
}
);
43 changes: 30 additions & 13 deletions compiled/alipay/src/_util/simply.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fmtEvent from "./fmtEvent";
import fmtEvent from './fmtEvent';

function removeNullProps(props) {
const newProps = {};
Expand Down Expand Up @@ -27,7 +27,7 @@ function buildProperties(props) {
}
newProperties[key] = {
type,
value: props[key]
value: props[key],
};
}
return newProperties;
Expand All @@ -39,15 +39,16 @@ function mergeDefaultProps(defaultProps: Record<string, any> = {}) {
};
}

type ComponentInstance<Props, Methods> = {
type ComponentInstance<Props, Methods> = {};

};

function ComponentImpl<Props, Methods = unknown>(defaultProps: Props, methods?: (Methods & ThisType<ComponentInstance<Props, Methods>>)) {
function ComponentImpl<Props, Methods = unknown>(
defaultProps: Props,
methods?: Methods & ThisType<ComponentInstance<Props, Methods>>
) {

Component({
props: removeNullProps(mergeDefaultProps(defaultProps)),
methods
methods,
});
}

Expand All @@ -57,10 +58,15 @@ export interface IPlatformEvent {
};
target: {
dataset: Record<string, unknown>;
}
};
}

export function triggerEvent(instance: any, eventName: string, value: unknown, e?: any) {
export function triggerEvent(
instance: any,
eventName: string,
value: unknown,
e?: any
) {
// 首字母大写,然后加上 on

const alipayCallbackName =
Expand All @@ -84,7 +90,12 @@ export function triggerEventOnly(instance: any, eventName: string, e?: any) {

}

export function triggerEventValues(instance: any, eventName: string, values: any[], e?: any) {
export function triggerEventValues(
instance: any,
eventName: string,
values: any[],
e?: any
) {
// 首字母大写,然后加上 on

const alipayCallbackName =
Expand All @@ -96,6 +107,12 @@ export function triggerEventValues(instance: any, eventName: string, values: any

}

export {
ComponentImpl as Component
}
export function triggerCatchEvent(instance: any, eventName: string, e?: any) {
const props = instance.props;
if (props[eventName]) {
props[eventName](fmtEvent(props, e));
}

}

export { ComponentImpl as Component };
3 changes: 3 additions & 0 deletions compiled/wechat/demo/pages/List/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Page({
handleTap(e) {
console.log(e);
},
catchTap(e) {
console.log(e);
},
handleSetRadius(checked) {
this.setData({
radius: checked,
Expand Down
2 changes: 1 addition & 1 deletion compiled/wechat/demo/pages/List/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ant-list-item
image="SetOutline"
arrow="right"
bind:tap="handleTap"
catch:tap="catchTap"
data-info="设置">
设置
</ant-list-item>
Expand Down
13 changes: 4 additions & 9 deletions compiled/wechat/src/List/ListItem/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
var ListItem = function (props) {
var _a = useComponentEvent(props), alipayForwardCatchEvent = _a.alipayForwardCatchEvent, alipayForwardEvent = _a.alipayForwardEvent;
return {};
};
mountComponent(ListItem, {
import { Component, } from '../../_util/simply';
Component({
image: '',
title: '',
brief: '',
arrow: null,
arrow: false,
extra: '',
extraBrief: '',
disabled: false,
showDivider: true,
});
}, {});
5 changes: 4 additions & 1 deletion compiled/wechat/src/_util/simply.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function buildProperties(props) {
}
newProperties[key] = {
type: type,
value: props[key]
value: props[key],
};
}
return newProperties;
Expand Down Expand Up @@ -67,4 +67,7 @@ export function triggerEventValues(instance, eventName, values, e) {
// 首字母大写,然后加上 on
instance.triggerEvent(eventName.toLocaleLowerCase(), values);
}
export function triggerCatchEvent(instance, eventName, e) {
instance.triggerEvent(eventName.toLocaleLowerCase());
}
export { ComponentImpl as Component };
2 changes: 1 addition & 1 deletion demo/pages/List/index.axml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default (_, { item, radius }: InternalData) => (
<AntListItem
image="SetOutline"
arrow="right"
onTap="handleTap"
catchTap="catchTap"
data-info="设置"
>
设置
Expand Down
10 changes: 10 additions & 0 deletions demo/pages/List/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ Page({

console.log(e);
},
catchTap(e) {
/// #if ALIPAY
my.alert({
title: 'catchTap',
content: e.currentTarget.dataset.info,
});
/// #endif

console.log(e);
},
handleSetRadius(checked) {
this.setData({
radius: checked,
Expand Down
68 changes: 33 additions & 35 deletions src/List/ListItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import { useEvent } from 'functional-mini/component';
import { ISwitchProps } from '../../Switch/props';
import { mountComponent } from '../../_util/component';
import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
import {
Component,
triggerEventOnly,
triggerCatchEvent,
} from '../../_util/simply';
import { IListItemProps } from './props';

const ListItem = (props: ISwitchProps) => {
const { alipayForwardCatchEvent, alipayForwardEvent } =
useComponentEvent(props);

/// #if ALIPAY
useEvent('onTap', (e) => {
if (props.disabled) {
return;
}
alipayForwardEvent('tap', e);
});
useEvent('catchTap', (e) => {
if (props.disabled) {
return;
}
alipayForwardCatchEvent('tap', e);
});
/// #endif
return {};
};

mountComponent<IListItemProps>(ListItem, {
image: '',
title: '',
brief: '',
arrow: null,
extra: '',
extraBrief: '',
disabled: false,
showDivider: true,
});
Component<IListItemProps>(
{
image: '',
title: '',
brief: '',
arrow: false,
extra: '',
extraBrief: '',
disabled: false,
showDivider: true,
},
{
/// #if ALIPAY
onTap(e) {
if (this.props.disabled) {
return;
}
triggerEventOnly(this, 'tap', e);
},
catchTap(e) {
if (this.props.disabled) {
return;
}
triggerCatchEvent(this, 'catchTap', e);
},
/// #endif
}
);
Loading
Loading