From 0119e4cc7b4dd889d3d8fac22b6d4e509d76d578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Mon, 13 May 2019 19:39:49 +0800 Subject: [PATCH] [improvement] ActionSheet: optimize jsx --- packages/action-sheet/index.tsx | 82 ++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 33 deletions(-) diff --git a/packages/action-sheet/index.tsx b/packages/action-sheet/index.tsx index 03daf8144..7504da7f5 100644 --- a/packages/action-sheet/index.tsx +++ b/packages/action-sheet/index.tsx @@ -2,8 +2,8 @@ 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'; +import Loading from '../loading'; // Types import { CreateElement, RenderContext } from 'vue/types'; @@ -37,6 +37,10 @@ function ActionSheet( ) { const { title, cancelText } = props; + function onInput(value: boolean) { + emit(ctx, 'input', value); + } + function onCancel() { emit(ctx, 'input', false); emit(ctx, 'cancel'); @@ -53,8 +57,16 @@ function ActionSheet( } } - function onClickOption(item: ActionSheetItem, index: number) { - return function (event: Event) { + function Content() { + if (slots.default) { + return
{slots.default()}
; + } + } + + function Option(item: ActionSheetItem, index: number) { + const disabled = item.disabled || item.loading; + + function onClickOption(event: MouseEvent) { event.stopPropagation(); if (item.disabled || item.loading) { @@ -70,51 +82,55 @@ function ActionSheet( if (props.closeOnClickAction) { emit(ctx, 'input', false); } - }; + } + + function OptionContent() { + if (item.loading) { + return ; + } + + return [ + {item.name}, + item.subname && {item.subname} + ]; + } + + return ( +
+ {OptionContent()} +
+ ); } - const Option = (item: ActionSheetItem, index: number) => ( -
- {item.loading ? ( - - ) : ( - [ - {item.name}, - item.subname && {item.subname} - ] - )} -
- ); + function CancelText() { + if (cancelText) { + return ( +
+ {cancelText} +
+ ); + } + } return ( { - emit(ctx, 'input', value); - }} + onInput={onInput} {...inherit(ctx)} > {Header()} {props.actions.map(Option)} - {slots.default &&
{slots.default()}
} - {cancelText && ( -
- {cancelText} -
- )} + {Content()} + {CancelText()}
); }