import { PropType } from 'vue'; // Utils import { createNamespace, pick } from '../utils'; // Components import Icon from '../icon'; import Popup, { popupSharedProps } from '../popup'; import Loading from '../loading'; const [createComponent, bem] = createNamespace('action-sheet'); export type ActionSheetAction = { name?: string; color?: string; subname?: string; loading?: boolean; disabled?: boolean; callback?: (action: ActionSheetAction) => void; className?: any; }; export default createComponent({ props: { ...popupSharedProps, title: String, actions: Array as PropType, cancelText: String, description: String, closeOnPopstate: Boolean, closeOnClickAction: Boolean, round: { type: Boolean, default: true, }, closeable: { type: Boolean, default: true, }, closeIcon: { type: String, default: 'cross', }, safeAreaInsetBottom: { type: Boolean, default: true, }, }, emits: ['select', 'cancel', 'update:show'], setup(props, { slots, emit }) { const popupPropKeys = Object.keys(popupSharedProps); const onUpdateShow = (show: boolean) => { emit('update:show', show); }; const onCancel = () => { onUpdateShow(false); emit('cancel'); }; const renderHeader = () => { if (props.title) { return (
{props.title} {props.closeable && ( )}
); } }; const renderCancel = () => { if (props.cancelText) { return [
, , ]; } }; const renderOption = (item: ActionSheetAction, index: number) => { const { name, color, subname, loading, callback, disabled, className, } = item; const Content = loading ? ( ) : ( [ {name}, subname &&
{subname}
, ] ); const onClick = () => { if (disabled || loading) { return; } if (callback) { callback(item); } emit('select', item, index); if (props.closeOnClickAction) { onUpdateShow(false); } }; return ( ); }; const renderDescription = () => { if (props.description || slots.description) { const content = slots.description ? slots.description() : props.description; return
{content}
; } }; const renderOptions = () => { if (props.actions) { return props.actions.map(renderOption); } }; return () => ( {renderHeader()} {renderDescription()}
{renderOptions()} {slots.default?.()}
{renderCancel()}
); }, });