mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 18:36:51 +08:00
chore: optimize some code (#9669)
This commit is contained in:
parent
d5b3350ee5
commit
477017a4ba
@ -1,10 +1,11 @@
|
|||||||
import { nextTick, PropType, defineComponent } from 'vue';
|
import { nextTick, defineComponent } from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import {
|
import {
|
||||||
pick,
|
pick,
|
||||||
extend,
|
extend,
|
||||||
truthProp,
|
truthProp,
|
||||||
|
makeArrayProp,
|
||||||
makeStringProp,
|
makeStringProp,
|
||||||
createNamespace,
|
createNamespace,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
@ -40,7 +41,7 @@ export default defineComponent({
|
|||||||
props: extend({}, popupSharedProps, {
|
props: extend({}, popupSharedProps, {
|
||||||
title: String,
|
title: String,
|
||||||
round: truthProp,
|
round: truthProp,
|
||||||
actions: Array as PropType<ActionSheetAction[]>,
|
actions: makeArrayProp<ActionSheetAction>(),
|
||||||
closeIcon: makeStringProp('cross'),
|
closeIcon: makeStringProp('cross'),
|
||||||
closeable: truthProp,
|
closeable: truthProp,
|
||||||
cancelText: String,
|
cancelText: String,
|
||||||
@ -138,12 +139,6 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderOptions = () => {
|
|
||||||
if (props.actions) {
|
|
||||||
return props.actions.map(renderOption);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<Popup
|
<Popup
|
||||||
class={bem()}
|
class={bem()}
|
||||||
@ -154,7 +149,7 @@ export default defineComponent({
|
|||||||
{renderHeader()}
|
{renderHeader()}
|
||||||
{renderDescription()}
|
{renderDescription()}
|
||||||
<div class={bem('content')}>
|
<div class={bem('content')}>
|
||||||
{renderOptions()}
|
{props.actions.map(renderOption)}
|
||||||
{slots.default?.()}
|
{slots.default?.()}
|
||||||
</div>
|
</div>
|
||||||
{renderCancel()}
|
{renderCancel()}
|
||||||
|
@ -80,9 +80,7 @@ export default defineComponent({
|
|||||||
options: CascaderOption[],
|
options: CascaderOption[],
|
||||||
value: string | number
|
value: string | number
|
||||||
): CascaderOption[] | undefined => {
|
): CascaderOption[] | undefined => {
|
||||||
for (let i = 0; i < options.length; i++) {
|
for (const option of options) {
|
||||||
const option = options[i];
|
|
||||||
|
|
||||||
if (option[valueKey] === value) {
|
if (option[valueKey] === value) {
|
||||||
return [option];
|
return [option];
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,13 @@ import { ImagePreviewScaleEventParams } from './types';
|
|||||||
|
|
||||||
const [name, bem] = createNamespace('image-preview');
|
const [name, bem] = createNamespace('image-preview');
|
||||||
|
|
||||||
|
const popupProps = [
|
||||||
|
'show',
|
||||||
|
'transition',
|
||||||
|
'overlayStyle',
|
||||||
|
'closeOnPopstate',
|
||||||
|
] as const;
|
||||||
|
|
||||||
const props = {
|
const props = {
|
||||||
show: Boolean,
|
show: Boolean,
|
||||||
loop: truthProp,
|
loop: truthProp,
|
||||||
@ -207,12 +214,7 @@ export default defineComponent({
|
|||||||
overlayClass={bem('overlay')}
|
overlayClass={bem('overlay')}
|
||||||
onClosed={onClosed}
|
onClosed={onClosed}
|
||||||
onUpdate:show={updateShow}
|
onUpdate:show={updateShow}
|
||||||
{...pick(props, [
|
{...pick(props, popupProps)}
|
||||||
'show',
|
|
||||||
'transition',
|
|
||||||
'overlayStyle',
|
|
||||||
'closeOnPopstate',
|
|
||||||
])}
|
|
||||||
>
|
>
|
||||||
{renderClose()}
|
{renderClose()}
|
||||||
{renderImages()}
|
{renderImages()}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ref,
|
ref,
|
||||||
|
Slot,
|
||||||
watch,
|
watch,
|
||||||
computed,
|
computed,
|
||||||
PropType,
|
PropType,
|
||||||
@ -94,31 +95,15 @@ export default defineComponent({
|
|||||||
emit('error', event);
|
emit('error', event);
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderLoadingIcon = () => {
|
const renderIcon = (name: string, className: unknown, slot?: Slot) => {
|
||||||
if (slots.loading) {
|
if (slot) {
|
||||||
return slots.loading();
|
return slot();
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Icon
|
<Icon
|
||||||
|
name={name}
|
||||||
size={props.iconSize}
|
size={props.iconSize}
|
||||||
name={props.loadingIcon}
|
class={className}
|
||||||
class={bem('loading-icon')}
|
|
||||||
classPrefix={props.iconPrefix}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderErrorIcon = () => {
|
|
||||||
if (slots.error) {
|
|
||||||
return slots.error();
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Icon
|
|
||||||
size={props.iconSize}
|
|
||||||
name={props.errorIcon}
|
|
||||||
class={bem('error-icon')}
|
|
||||||
classPrefix={props.iconPrefix}
|
classPrefix={props.iconPrefix}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@ -126,10 +111,18 @@ export default defineComponent({
|
|||||||
|
|
||||||
const renderPlaceholder = () => {
|
const renderPlaceholder = () => {
|
||||||
if (loading.value && props.showLoading) {
|
if (loading.value && props.showLoading) {
|
||||||
return <div class={bem('loading')}>{renderLoadingIcon()}</div>;
|
return (
|
||||||
|
<div class={bem('loading')}>
|
||||||
|
{renderIcon(props.loadingIcon, bem('loading-icon'), slots.loading)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (error.value && props.showError) {
|
if (error.value && props.showError) {
|
||||||
return <div class={bem('error')}>{renderErrorIcon()}</div>;
|
return (
|
||||||
|
<div class={bem('error')}>
|
||||||
|
{renderIcon(props.errorIcon, bem('error-icon'), slots.error)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,16 +100,6 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const getScrollerRect = () => {
|
|
||||||
if ('getBoundingClientRect' in scrollParent.value!) {
|
|
||||||
return useRect(scrollParent);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
top: 0,
|
|
||||||
left: 0,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const getActiveAnchor = (
|
const getActiveAnchor = (
|
||||||
scrollTop: number,
|
scrollTop: number,
|
||||||
rects: Array<{ top: number; height: number }>
|
rects: Array<{ top: number; height: number }>
|
||||||
@ -133,7 +123,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
const { sticky, indexList } = props;
|
const { sticky, indexList } = props;
|
||||||
const scrollTop = getScrollTop(scrollParent.value!);
|
const scrollTop = getScrollTop(scrollParent.value!);
|
||||||
const scrollParentRect = getScrollerRect();
|
const scrollParentRect = useRect(scrollParent);
|
||||||
|
|
||||||
const rects = children.map((item) =>
|
const rects = children.map((item) =>
|
||||||
item.getRect(scrollParent.value, scrollParentRect)
|
item.getRect(scrollParent.value, scrollParentRect)
|
||||||
|
@ -9,6 +9,7 @@ import {
|
|||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import {
|
import {
|
||||||
|
pick,
|
||||||
isDef,
|
isDef,
|
||||||
unknownProp,
|
unknownProp,
|
||||||
numericProp,
|
numericProp,
|
||||||
@ -28,6 +29,15 @@ import type { ToastType, ToastPosition } from './types';
|
|||||||
|
|
||||||
const [name, bem] = createNamespace('toast');
|
const [name, bem] = createNamespace('toast');
|
||||||
|
|
||||||
|
const popupProps = [
|
||||||
|
'show',
|
||||||
|
'overlay',
|
||||||
|
'transition',
|
||||||
|
'overlayClass',
|
||||||
|
'overlayStyle',
|
||||||
|
'closeOnClickOverlay',
|
||||||
|
] as const;
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name,
|
name,
|
||||||
|
|
||||||
@ -128,20 +138,15 @@ export default defineComponent({
|
|||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<Popup
|
<Popup
|
||||||
show={props.show}
|
|
||||||
class={[
|
class={[
|
||||||
bem([props.position, { [props.type]: !props.icon }]),
|
bem([props.position, { [props.type]: !props.icon }]),
|
||||||
props.className,
|
props.className,
|
||||||
]}
|
]}
|
||||||
overlay={props.overlay}
|
|
||||||
lockScroll={false}
|
lockScroll={false}
|
||||||
transition={props.transition}
|
|
||||||
overlayClass={props.overlayClass}
|
|
||||||
overlayStyle={props.overlayStyle}
|
|
||||||
closeOnClickOverlay={props.closeOnClickOverlay}
|
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
onClosed={clearTimer}
|
onClosed={clearTimer}
|
||||||
onUpdate:show={updateShow}
|
onUpdate:show={updateShow}
|
||||||
|
{...pick(props, popupProps)}
|
||||||
>
|
>
|
||||||
{renderIcon()}
|
{renderIcon()}
|
||||||
{renderMessage()}
|
{renderMessage()}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user