refactor(message): use use-container

This commit is contained in:
07akioni 2020-09-14 00:46:51 +08:00
parent f2511c8560
commit ce46f0de8e
6 changed files with 63 additions and 47 deletions

View File

@ -44,3 +44,4 @@ export function useIsMounted () {
}
export { default as useLastClickPosition } from './use-last-click-position'
export { default as useContainer } from './use-container'

View File

@ -0,0 +1,31 @@
import { computed } from 'vue'
export default function useContainer (
toRef,
containerClassNameRef
) {
const getContainerTarget = computed(
() => typeof toRef.value === 'string' ? () => document.querySelector(toRef.value) : () => toRef.value
)
const getContainer = computed(
() => () => getContainerTarget.value().querySelector('.' + containerClassNameRef.value)
)
const mountIfNotExist = () => {
const targetEl = getContainerTarget.value()
if (!targetEl.querySelector('.' + containerClassNameRef.value)) {
const containerEl = document.createElement('div')
containerEl.className = containerClassNameRef.value
targetEl.appendChild(containerEl)
}
}
const unmountIfEmpty = () => {
const container = getContainer.value()
if (!container.childElementCount) {
container.parentNode.removeChild(container)
}
}
return [
mountIfNotExist,
unmountIfEmpty
]
}

View File

@ -21,14 +21,10 @@ export default {
themeable,
usecssr(styles)
],
cssrName: 'Message',
emits: [
'close'
],
props,
methods: {
handleClose () {
this.$emit('close')
this.onClose()
}
},
render () {

View File

@ -1,30 +1,20 @@
import { Fragment, ref, h, nextTick } from 'vue'
import { Fragment, ref, h, nextTick, toRef } from 'vue'
import createId from '../../_utils/vue/createId'
import MessageEnvironment from './MessageEnvironment.js'
function getContainerTarget (to) {
return typeof to === 'string' ? document.querySelector(to) : to
}
function getContainer (to) {
return getContainerTarget(to).querySelector('.n-message-container')
}
function mountContainer (to) {
const targetEl = getContainerTarget(to)
if (!targetEl.querySelector('.n-message-container')) {
const containerEl = document.createElement('div')
containerEl.className = 'n-message-container'
targetEl.appendChild(containerEl)
}
}
import { useContainer } from '../../_utils/composition'
export default {
name: 'MessageController',
setup () {
setup (props) {
const messageListRef = ref([])
const [mountContainerIfNotExist, unmountContainerIfEmpty] = useContainer(
toRef(props, 'to'),
ref('n-message-container')
)
return {
messageList: messageListRef
messageList: messageListRef,
mountContainerIfNotExist,
unmountContainerIfEmpty
}
},
props: {
@ -35,7 +25,7 @@ export default {
},
methods: {
create (content, options = {}) {
mountContainer(this.to)
this.mountContainerIfNotExist()
const key = createId()
const messageReactive = {
...options,
@ -65,10 +55,7 @@ export default {
1
)
nextTick(() => {
const container = getContainer(this.to)
if (!container.childElementCount) {
container.parentNode.removeChild(container)
}
this.unmountContainerIfEmpty()
})
}
},

View File

@ -5,9 +5,6 @@ import props from './message-props'
export default {
name: 'MessageEnvironment',
emits: [
'internal-after-leave'
],
props: {
...props,
duration: {
@ -16,33 +13,33 @@ export default {
},
onClose: {
type: Function,
default: null
default: () => {}
},
onAfterLeave: {
type: Function,
default: null
},
destroy: {
type: Function,
default: null
default: () => {}
},
// private
controller: {
type: Object,
required: true
},
onInternalAfterLeave: {
destroy: {
type: Function,
default: null
},
onInternalAfterLeave: {
type: Function,
default: () => {}
},
// deprecated
onHide: {
type: Function,
default: null
default: () => {}
},
onAfterHide: {
type: Function,
default: null
default: () => {}
}
},
data () {
@ -71,9 +68,9 @@ export default {
if (timerId) {
window.clearTimeout(timerId)
}
if (onClose) onClose()
onClose()
// deprecated
if (onHide) onHide()
onHide()
},
handleClose () {
this.hide()
@ -84,10 +81,10 @@ export default {
onInternalAfterLeave,
onAfterHide
} = this
if (onAfterLeave) onAfterLeave()
if (onInternalAfterLeave) onInternalAfterLeave(this._.vnode.key)
onAfterLeave()
onInternalAfterLeave(this._.vnode.key)
// deprecated
if (onAfterHide) onAfterHide()
onAfterHide()
},
// deprecated
deactivate () {

View File

@ -18,5 +18,9 @@ export default {
theme: {
type: String,
default: null
},
onClose: {
type: Function,
default: () => {}
}
}