mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-27 03:46:35 +08:00
chore(ActionSheet): improve code
This commit is contained in:
parent
00395f0146
commit
de7c83240d
@ -1,5 +1,5 @@
|
|||||||
// Utils
|
// Utils
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace, pick } from '../utils';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
@ -8,58 +8,11 @@ import Loading from '../loading';
|
|||||||
|
|
||||||
const [createComponent, bem] = createNamespace('action-sheet');
|
const [createComponent, bem] = createNamespace('action-sheet');
|
||||||
|
|
||||||
function Header({ title, closeIcon, onCancel }) {
|
|
||||||
if (title) {
|
|
||||||
return (
|
|
||||||
<div class={bem('header')}>
|
|
||||||
{title}
|
|
||||||
<Icon name={closeIcon} class={bem('close')} onClick={onCancel} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Option({ item }) {
|
|
||||||
const { name, color, subname, loading, disabled, className } = item;
|
|
||||||
|
|
||||||
const Content = loading ? (
|
|
||||||
<Loading class={bem('loading-icon')} />
|
|
||||||
) : (
|
|
||||||
[
|
|
||||||
<span class={bem('name')}>{name}</span>,
|
|
||||||
subname && <div class={bem('subname')}>{subname}</div>,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
style={{ color }}
|
|
||||||
class={[bem('item', { loading, disabled }), className]}
|
|
||||||
>
|
|
||||||
{Content}
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function CancelText({ cancelText, onCancel }) {
|
|
||||||
if (cancelText) {
|
|
||||||
return [
|
|
||||||
<div class={bem('gap')} />,
|
|
||||||
<button type="button" class={bem('cancel')} onClick={onCancel}>
|
|
||||||
{cancelText}
|
|
||||||
</button>,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
props: {
|
props: {
|
||||||
...popupSharedProps,
|
...popupSharedProps,
|
||||||
title: String,
|
title: String,
|
||||||
actions: Array,
|
actions: Array,
|
||||||
duration: [Number, String],
|
|
||||||
teleport: [String, Object],
|
|
||||||
cancelText: String,
|
cancelText: String,
|
||||||
description: String,
|
description: String,
|
||||||
closeOnPopstate: Boolean,
|
closeOnPopstate: Boolean,
|
||||||
@ -76,38 +29,87 @@ export default createComponent({
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
overlay: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
closeOnClickOverlay: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
emits: ['select', 'cancel', 'update:show'],
|
emits: ['select', 'cancel', 'update:show'],
|
||||||
|
|
||||||
setup(props, { slots, emit }) {
|
setup(props, { slots, emit }) {
|
||||||
function onUpdateShow(show) {
|
const popupPropKeys = Object.keys(popupSharedProps);
|
||||||
emit('update:show', show);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onCancel() {
|
const onUpdateShow = (show) => {
|
||||||
|
emit('update:show', show);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onCancel = () => {
|
||||||
onUpdateShow(false);
|
onUpdateShow(false);
|
||||||
emit('cancel');
|
emit('cancel');
|
||||||
}
|
};
|
||||||
|
|
||||||
return function () {
|
const renderHeader = () => {
|
||||||
const {
|
if (props.title) {
|
||||||
title,
|
return (
|
||||||
actions,
|
<div class={bem('header')}>
|
||||||
closeIcon,
|
{props.title}
|
||||||
cancelText,
|
<Icon
|
||||||
description,
|
name={props.closeIcon}
|
||||||
closeOnClickAction,
|
class={bem('close')}
|
||||||
...restProps
|
onClick={onCancel}
|
||||||
} = props;
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderCancel = () => {
|
||||||
|
if (props.cancelText) {
|
||||||
|
return [
|
||||||
|
<div class={bem('gap')} />,
|
||||||
|
<button type="button" class={bem('cancel')} onClick={onCancel}>
|
||||||
|
{props.cancelText}
|
||||||
|
</button>,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderOption = (item, index) => {
|
||||||
|
const { name, color, subname, loading, disabled, className } = item;
|
||||||
|
|
||||||
|
const Content = loading ? (
|
||||||
|
<Loading class={bem('loading-icon')} />
|
||||||
|
) : (
|
||||||
|
[
|
||||||
|
<span class={bem('name')}>{name}</span>,
|
||||||
|
subname && <div class={bem('subname')}>{subname}</div>,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onClick = () => {
|
||||||
|
emit('select', item, index);
|
||||||
|
if (props.closeOnClickAction) {
|
||||||
|
emit('update:show', false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
style={{ color }}
|
||||||
|
class={[bem('item', { loading, disabled }), className]}
|
||||||
|
onClick={onClick}
|
||||||
|
>
|
||||||
|
{Content}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderOptions = () => {
|
||||||
|
if (props.actions) {
|
||||||
|
return props.actions.map(renderOption);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const { round, description } = props;
|
||||||
|
|
||||||
const Content = slots.default && (
|
const Content = slots.default && (
|
||||||
<div class={bem('content')}>{slots.default()}</div>
|
<div class={bem('content')}>{slots.default()}</div>
|
||||||
@ -117,35 +119,21 @@ export default createComponent({
|
|||||||
<div class={bem('description')}>{description}</div>
|
<div class={bem('description')}>{description}</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
const Options =
|
|
||||||
actions &&
|
|
||||||
actions.map((item, index) => (
|
|
||||||
<Option
|
|
||||||
item={item}
|
|
||||||
onClick={() => {
|
|
||||||
emit('select', item, index);
|
|
||||||
|
|
||||||
if (closeOnClickAction) {
|
|
||||||
emit('update:show', false);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
));
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Popup
|
<Popup
|
||||||
class={bem()}
|
class={bem()}
|
||||||
|
round={round}
|
||||||
position="bottom"
|
position="bottom"
|
||||||
{...{
|
{...{
|
||||||
...restProps,
|
...pick(props, popupPropKeys),
|
||||||
'onUpdate:show': onUpdateShow,
|
'onUpdate:show': onUpdateShow,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Header title={title} closeIcon={closeIcon} onCancel={onCancel} />
|
{renderHeader()}
|
||||||
{Description}
|
{Description}
|
||||||
{Options}
|
{renderOptions()}
|
||||||
{Content}
|
{Content}
|
||||||
<CancelText cancelText={cancelText} onCancel={onCancel} />
|
{renderCancel()}
|
||||||
</Popup>
|
</Popup>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user