fix: type

This commit is contained in:
07akioni 2024-07-12 01:03:34 +08:00
parent 30d25339c5
commit 10f515f8f6
2 changed files with 102 additions and 104 deletions

View File

@ -9,27 +9,28 @@ export default antfu(
'test/unit/coverage',
'src/_deprecated/icons',
'dist',
'es',
],
'es'
]
},
{
files: ['**/*.demo.vue'],
rules: {
'no-console': 'off',
'vue/one-component-per-file': 'off',
},
'vue/one-component-per-file': 'off'
}
},
{
files: ['**/*.tsx'],
rules: {
'unused-imports/no-unused-imports': 'off',
},
'unused-imports/no-unused-imports': 'off'
}
},
{
files: ['**/*'],
rules: {
'style/multiline-ternary': 'off',
'style/max-statements-per-line': 'off',
},
},
'style/comma-dangle': 'off'
}
}
)

View File

@ -1,17 +1,13 @@
import {
defineComponent,
Fragment,
ref,
h,
type ExtractPropTypes,
provide,
type PropType,
reactive,
type Ref,
type CSSProperties
import { Fragment, defineComponent, h, provide, reactive, ref } from 'vue'
import type {
CSSProperties,
DefineComponent,
ExtractPropTypes,
PropType,
Ref
} from 'vue'
import { createId } from 'seemly'
import { useClicked, useClickPosition } from 'vooks'
import { useClickPosition, useClicked } from 'vooks'
import { omit } from '../../_utils'
import type { ExtractPublicPropTypes, Mutable } from '../../_utils'
import { NModalEnvironment } from './ModalEnvironment'
@ -20,13 +16,13 @@ import {
modalProviderInjectionKey,
modalReactiveListInjectionKey
} from './context'
import { type modalProps } from './Modal'
import type { modalProps } from './Modal'
export type ModalOptions = Mutable<
Omit<Partial<ExtractPropTypes<typeof modalProps>>, 'internalStyle'> & {
class?: any
style?: string | CSSProperties
}
Omit<Partial<ExtractPropTypes<typeof modalProps>>, 'internalStyle'> & {
class?: any
style?: string | CSSProperties
}
>
export type ModalReactive = {
@ -68,85 +64,86 @@ export type ModalProviderProps = ExtractPublicPropTypes<
typeof modalProviderProps
>
export const NModalProvider = defineComponent({
name: 'ModalProvider',
props: modalProviderProps,
setup () {
const clickedRef = useClicked(64)
const clickedPositionRef = useClickPosition()
export const NModalProvider: DefineComponent<{ to?: string | HTMLElement }>
= defineComponent({
name: 'ModalProvider',
props: modalProviderProps,
setup() {
const clickedRef = useClicked(64)
const clickedPositionRef = useClickPosition()
const modalListRef = ref<TypeSafeModalReactive[]>([])
const modalInstRefs: Record<string, ModalInst | undefined> = {}
function create (options: ModalOptions = {}): ModalReactive {
const key = createId()
const modalReactive = reactive({
...options,
key,
destroy: () => {
modalInstRefs[`n-modal-${key}`]?.hide()
}
})
modalListRef.value.push(modalReactive)
return modalReactive
}
const modalListRef = ref<TypeSafeModalReactive[]>([])
const modalInstRefs: Record<string, ModalInst | undefined> = {}
function create(options: ModalOptions = {}): ModalReactive {
const key = createId()
const modalReactive = reactive({
...options,
key,
destroy: () => {
modalInstRefs[`n-modal-${key}`]?.hide()
}
})
modalListRef.value.push(modalReactive)
return modalReactive
}
function handleAfterLeave (key: string): void {
const { value: modalList } = modalListRef
modalList.splice(
modalList.findIndex((modal) => modal.key === key),
1
)
}
function destroyAll (): void {
Object.values(modalInstRefs).forEach((modalInstRef) => {
modalInstRef?.hide()
})
}
const api = {
create,
destroyAll
}
provide(modalApiInjectionKey, api)
provide(modalProviderInjectionKey, {
clickedRef: useClicked(64),
clickedPositionRef: useClickPosition()
})
provide(modalReactiveListInjectionKey, modalListRef)
provide(modalProviderInjectionKey, {
clickedRef,
clickedPositionRef
})
return {
...api,
modalList: modalListRef,
modalInstRefs,
handleAfterLeave
}
},
render () {
return h(Fragment, null, [
this.modalList.map((modal) =>
h(
NModalEnvironment,
omit(modal, ['destroy'], {
to: modal.to ?? this.to,
ref: ((inst: ModalInst | null) => {
if (inst === null) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.modalInstRefs[`n-modal-${modal.key}`]
} else {
this.modalInstRefs[`n-modal-${modal.key}`] = inst
}
}) as any,
internalKey: modal.key,
onInternalAfterLeave: this.handleAfterLeave
})
function handleAfterLeave(key: string): void {
const { value: modalList } = modalListRef
modalList.splice(
modalList.findIndex(modal => modal.key === key),
1
)
),
this.$slots.default?.()
])
}
})
}
function destroyAll(): void {
Object.values(modalInstRefs).forEach((modalInstRef) => {
modalInstRef?.hide()
})
}
const api = {
create,
destroyAll
}
provide(modalApiInjectionKey, api)
provide(modalProviderInjectionKey, {
clickedRef: useClicked(64),
clickedPositionRef: useClickPosition()
})
provide(modalReactiveListInjectionKey, modalListRef)
provide(modalProviderInjectionKey, {
clickedRef,
clickedPositionRef
})
return {
...api,
modalList: modalListRef,
modalInstRefs,
handleAfterLeave
}
},
render() {
return h(Fragment, null, [
this.modalList.map(modal =>
h(
NModalEnvironment,
omit(modal, ['destroy'], {
to: modal.to ?? this.to,
ref: ((inst: ModalInst | null) => {
if (inst === null) {
delete this.modalInstRefs[`n-modal-${modal.key}`]
}
else {
this.modalInstRefs[`n-modal-${modal.key}`] = inst
}
}) as any,
internalKey: modal.key,
onInternalAfterLeave: this.handleAfterLeave
})
)
),
this.$slots.default?.()
])
}
})