Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(popup): sfc to tsx #1401

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/popup/__test__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config, mount } from '@vue/test-utils';
import { describe, it, expect, vi } from 'vitest';
import Popup from '../popup.vue';
import Popup from '../popup';
import { ref } from 'vue';
import { CloseIcon } from 'tdesign-icons-vue-next';

Expand Down
2 changes: 1 addition & 1 deletion src/popup/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Popup from './popup.vue';
import Popup from './popup';
import { withInstall, WithInstallType } from '../shared';
import { TdPopupProps } from './type';

Expand Down
88 changes: 48 additions & 40 deletions src/popup/popup.vue → src/popup/popup.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
<template>
<teleport v-if="!destroyOnClose || wrapperVisible" :to="to" :disabled="!to">
<t-overlay v-bind="overlayProps" :visible="innerVisible && showOverlay" @click="handleOverlayClick" />
<transition :name="contentTransitionName" @after-enter="afterEnter" @after-leave="afterLeave">
<div v-show="innerVisible" :class="[name, $attrs.class, contentClasses]" :style="rootStyles" v-bind="$attrs">
<div v-if="closeBtnNode" :class="`${name}__close`" @click="handleCloseClick">
<t-node :content="closeBtnNode" />
</div>
<slot />
</div>
</transition>
</teleport>
</template>

<script lang="ts">
import { computed, watch, defineComponent, h, getCurrentInstance, ref, nextTick } from 'vue';
import { computed, watch, defineComponent, h, ref, nextTick, Teleport, Transition } from 'vue';
import { CloseIcon } from 'tdesign-icons-vue-next';

import popupProps from './props';
import TOverlay from '../overlay';
import config from '../config';
import { TdPopupProps } from './type';
import { useDefault, TNode, renderTNode, isBrowser } from '../shared';
import { useContent, useTNodeJSX } from '../hooks/tnode';
import { useDefault, isBrowser } from '../shared';
import { getAttach } from '../shared/dom';

const { prefix } = config;
Expand All @@ -31,21 +17,25 @@ let lockTimes = 0;

export default defineComponent({
name,
components: { TNode, TOverlay },
inheritAttrs: false,
props: popupProps,
emits: ['open', 'close', 'opened', 'closed', 'visible-change', 'update:visible', 'update:modelValue'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import props from './props';

这里简写~

setup(props, context) {
const currentInstance = getCurrentInstance();
const [currentVisible, setVisible] = useDefault<TdPopupProps['visible'], TdPopupProps>(
props,
context.emit,
'visible',
'visible-change',
);

const wrapperVisible = ref(currentVisible.value);

const innerVisible = ref(currentVisible.value);

const renderTNodeContent = useContent();

const renderTNodeJSX = useTNodeJSX();

// 因为开启 destroyOnClose,会影响 transition 的动画,因此需要前后设置 visible
watch(currentVisible, (v) => {
wrapperVisible.value = v;
Expand All @@ -69,7 +59,7 @@ export default defineComponent({
if (props.zIndex) {
styles.zIndex = `${props.zIndex}`;
}
return { ...(context.attrs.style as Object), ...styles };
return { ...(context.attrs.style as Object), ...styles, display: innerVisible.value ? 'block' : 'none' };
});

const contentClasses = computed(() => ({
Expand All @@ -84,11 +74,7 @@ export default defineComponent({
return `slide-${placement}`;
});

const closeBtnNode = computed(() =>
renderTNode(currentInstance, 'closeBtn', {
defaultNode: h(CloseIcon, { size: '24px' }),
}),
);
const closeBtnNode = computed(() => renderTNodeJSX('closeBtn', h(CloseIcon, { size: '24px' })));

const handleCloseClick = (e: MouseEvent) => {
props.onClose?.({ e });
Expand Down Expand Up @@ -151,21 +137,43 @@ export default defineComponent({
},
);

return {
name,
to,
wrapperVisible,
innerVisible,
currentVisible,
rootStyles,
contentClasses,
contentTransitionName,
closeBtnNode,
afterEnter,
afterLeave,
handleOverlayClick,
handleCloseClick,
return () => {
const renderOverlayContent = computed(() => (
<TOverlay
{...props.overlayProps}
visible={innerVisible.value && props.showOverlay}
onClick={handleOverlayClick}
/>
));

const renderCloseBtn = computed(() =>
closeBtnNode.value ? (
<div class={`${name}__close`} onClick={handleCloseClick}>
{closeBtnNode.value}
</div>
) : null,
);

const renderContent = computed(() => (
<Transition name={contentTransitionName.value} onAfterEnter={afterEnter} onAfterLeave={afterLeave}>
<div {...context.attrs} class={[name, contentClasses.value]} style={rootStyles.value}>
{renderCloseBtn.value}

{renderTNodeContent('default', 'content')}
</div>
</Transition>
));

const renderPopupContent = computed(() =>
!props.destroyOnClose || wrapperVisible.value ? (
<Teleport to={to.value} disabled={!to.value}>
{renderOverlayContent.value}
{renderContent.value}
</Teleport>
) : null,
);

return <>{renderPopupContent.value}</>;
};
},
});
</script>
Loading