import { use } from '../utils'; import { emit, inherit } from '../utils/functional'; import { PopupMixin } from '../mixins/popup'; import Icon from '../icon'; import Loading from '../loading'; import Popup from '../popup'; // Types import { CreateElement, RenderContext } from 'vue/types'; import { DefaultSlots } from '../utils/types'; import { PopupMixinProps } from '../mixins/popup/type'; export type ActionSheetItem = { name: string; subname?: string; loading?: boolean; disabled?: boolean; className?: string; callback?: (item: ActionSheetItem) => void; }; export type ActionSheetProps = PopupMixinProps & { title?: string; actions: ActionSheetItem[]; cancelText?: string; closeOnClickAction?: boolean; safeAreaInsetBottom?: boolean; }; const [sfc, bem] = use('action-sheet'); function ActionSheet( h: CreateElement, props: ActionSheetProps, slots: DefaultSlots, ctx: RenderContext ) { const { title, cancelText } = props; function onCancel() { emit(ctx, 'input', false); emit(ctx, 'cancel'); } function Header() { if (title) { return (
{title}
); } } function onClickOption(item: ActionSheetItem, index: number) { return function (event: Event) { event.stopPropagation(); if (item.disabled || item.loading) { return; } if (item.callback) { item.callback(item); } emit(ctx, 'select', item, index); if (props.closeOnClickAction) { emit(ctx, 'input', false); } }; } const Option = (item: ActionSheetItem, index: number) => (
{item.loading ? ( ) : ( [ {item.name}, item.subname && {item.subname} ] )}
); return ( { emit(ctx, 'input', value); }} {...inherit(ctx)} > {Header()} {props.actions.map(Option)} {slots.default &&
{slots.default()}
} {cancelText && (
{cancelText}
)}
); } ActionSheet.props = { ...PopupMixin.props, title: String, actions: Array, cancelText: String, closeOnClickAction: Boolean, safeAreaInsetBottom: Boolean, overlay: { type: Boolean, default: true }, closeOnClickOverlay: { type: Boolean, default: true } }; export default sfc(ActionSheet);