types(Popover): use tsx (#8166)

This commit is contained in:
neverland 2021-02-16 13:16:24 +08:00 committed by GitHub
parent 68555aa29a
commit 6c89575946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 24 deletions

View File

@ -59,7 +59,7 @@
"@babel/runtime": "7.x",
"@vant/icons": "^1.5.2",
"@vant/lazyload": "^1.0.2",
"@vant/popperjs": "^1.0.2",
"@vant/popperjs": "^1.0.4",
"@vant/use": "^1.0.5"
},
"peerDependencies": {

View File

@ -1,8 +1,16 @@
import { ref, watch, nextTick, onMounted, onBeforeUnmount } from 'vue';
import { createPopper, offsetModifier } from '@vant/popperjs';
import {
ref,
watch,
nextTick,
PropType,
onMounted,
TeleportProps,
onBeforeUnmount,
} from 'vue';
import { Instance, createPopper, offsetModifier } from '@vant/popperjs';
// Utils
import { createNamespace } from '../utils';
import { ComponentInstance, createNamespace } from '../utils';
import { BORDER_BOTTOM } from '../utils/constant';
// Composition
@ -14,32 +22,56 @@ import Popup from '../popup';
const [createComponent, bem] = createNamespace('popover');
export type PopoverTheme = 'light' | 'dark';
export type PopoverTrigger = 'manual' | 'click';
export type PopoverPlacement =
| 'top'
| 'top-start'
| 'top-end'
| 'left'
| 'left-start'
| 'left-end'
| 'right'
| 'right-start'
| 'right-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end';
export type PopoverAction = {
text: string;
icon?: string;
color?: string;
disabled?: boolean;
className?: string;
};
export default createComponent({
props: {
show: Boolean,
overlay: Boolean,
offset: {
type: Array,
type: (Array as unknown) as PropType<[number, number]>,
default: () => [0, 8],
},
theme: {
type: String,
type: String as PropType<PopoverTheme>,
default: 'light',
},
trigger: {
type: String,
type: String as PropType<PopoverTrigger>,
default: 'click',
},
actions: {
type: Array,
type: Array as PropType<PopoverAction[]>,
default: () => [],
},
placement: {
type: String,
type: String as PropType<PopoverPlacement>,
default: 'bottom',
},
teleport: {
type: [String, Object],
type: [String, Object] as PropType<TeleportProps['to']>,
default: 'body',
},
closeOnClickAction: {
@ -51,12 +83,13 @@ export default createComponent({
emits: ['select', 'touchstart', 'update:show'],
setup(props, { emit, slots, attrs }) {
let popper;
const wrapperRef = ref();
const popoverRef = ref();
let popper: Instance | null;
const wrapperRef = ref<HTMLElement>();
const popoverRef = ref<ComponentInstance>();
const createPopperInstance = () => {
return createPopper(wrapperRef.value, popoverRef.value.popupRef.value, {
return createPopper(wrapperRef.value!, popoverRef.value!.popupRef.value, {
placement: props.placement,
modifiers: [
{
@ -92,7 +125,7 @@ export default createComponent({
});
};
const toggle = (value) => {
const toggle = (value: boolean) => {
emit('update:show', value);
};
@ -102,12 +135,12 @@ export default createComponent({
}
};
const onTouchstart = (event) => {
const onTouchstart = (event: TouchEvent) => {
event.stopPropagation();
emit('touchstart', event);
};
const onClickAction = (action, index) => {
const onClickAction = (action: PopoverAction, index: number) => {
if (action.disabled) {
return;
}
@ -123,7 +156,7 @@ export default createComponent({
toggle(false);
};
const renderAction = (action, index) => {
const renderAction = (action: PopoverAction, index: number) => {
const { icon, text, color, disabled, className } = action;
return (
<div
@ -160,7 +193,7 @@ export default createComponent({
show={props.show}
class={bem([props.theme])}
overlay={props.overlay}
position={null}
position={''}
teleport={props.teleport}
transition="van-popover-zoom"
lockScroll={false}

View File

@ -25,7 +25,7 @@ import { useLazyRender } from '../composables/use-lazy-render';
import Icon from '../icon';
import Overlay from '../overlay';
export type PopupPosition = 'top' | 'left' | 'bottom' | 'right' | 'center';
export type PopupPosition = 'top' | 'left' | 'bottom' | 'right' | 'center' | '';
export type PopupCloseIconPosition =
| 'top-left'

View File

@ -1961,10 +1961,10 @@
fast-glob "^3.2.2"
fs-extra "^9.0.0"
"@vant/popperjs@^1.0.2":
version "1.0.2"
resolved "https://registry.npm.taobao.org/@vant/popperjs/download/@vant/popperjs-1.0.2.tgz#522d71656235125a3ae08e9c701a3bae6fd88d7b"
integrity sha1-Ui1xZWI1Elo64I6ccBo7rm/YjXs=
"@vant/popperjs@^1.0.4":
version "1.0.4"
resolved "https://registry.npm.taobao.org/@vant/popperjs/download/@vant/popperjs-1.0.4.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40vant%2Fpopperjs%2Fdownload%2F%40vant%2Fpopperjs-1.0.4.tgz#49c20809a3828e7b036e23d50fb56121e997618f"
integrity sha1-ScIICaOCjnsDbiPVD7VhIemXYY8=
dependencies:
"@popperjs/core" "^2.5.4"