From de7c83240d62c3ce4c9d8bda79ab8ca174bc902b Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 24 Aug 2020 10:28:12 +0800 Subject: [PATCH] chore(ActionSheet): improve code --- src/action-sheet/index.js | 168 ++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 90 deletions(-) diff --git a/src/action-sheet/index.js b/src/action-sheet/index.js index dd707ad1d..2e15c72f1 100644 --- a/src/action-sheet/index.js +++ b/src/action-sheet/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace } from '../utils'; +import { createNamespace, pick } from '../utils'; // Components import Icon from '../icon'; @@ -8,58 +8,11 @@ import Loading from '../loading'; const [createComponent, bem] = createNamespace('action-sheet'); -function Header({ title, closeIcon, onCancel }) { - if (title) { - return ( -
- {title} - -
- ); - } -} - -function Option({ item }) { - const { name, color, subname, loading, disabled, className } = item; - - const Content = loading ? ( - - ) : ( - [ - {name}, - subname &&
{subname}
, - ] - ); - - return ( - - ); -} - -function CancelText({ cancelText, onCancel }) { - if (cancelText) { - return [ -
, - , - ]; - } -} - export default createComponent({ props: { ...popupSharedProps, title: String, actions: Array, - duration: [Number, String], - teleport: [String, Object], cancelText: String, description: String, closeOnPopstate: Boolean, @@ -76,38 +29,87 @@ export default createComponent({ type: Boolean, default: true, }, - overlay: { - type: Boolean, - default: true, - }, - closeOnClickOverlay: { - type: Boolean, - default: true, - }, }, emits: ['select', 'cancel', 'update:show'], setup(props, { slots, emit }) { - function onUpdateShow(show) { - emit('update:show', show); - } + const popupPropKeys = Object.keys(popupSharedProps); - function onCancel() { + const onUpdateShow = (show) => { + emit('update:show', show); + }; + + const onCancel = () => { onUpdateShow(false); emit('cancel'); - } + }; - return function () { - const { - title, - actions, - closeIcon, - cancelText, - description, - closeOnClickAction, - ...restProps - } = props; + const renderHeader = () => { + if (props.title) { + return ( +
+ {props.title} + +
+ ); + } + }; + + const renderCancel = () => { + if (props.cancelText) { + return [ +
, + , + ]; + } + }; + + const renderOption = (item, index) => { + const { name, color, subname, loading, disabled, className } = item; + + const Content = loading ? ( + + ) : ( + [ + {name}, + subname &&
{subname}
, + ] + ); + + const onClick = () => { + emit('select', item, index); + if (props.closeOnClickAction) { + emit('update:show', false); + } + }; + + return ( + + ); + }; + + const renderOptions = () => { + if (props.actions) { + return props.actions.map(renderOption); + } + }; + + return () => { + const { round, description } = props; const Content = slots.default && (
{slots.default()}
@@ -117,35 +119,21 @@ export default createComponent({
{description}
); - const Options = - actions && - actions.map((item, index) => ( -