Skip to content

Commit

Permalink
fix: 新增label时首项是空白
Browse files Browse the repository at this point in the history
  • Loading branch information
yt0379 committed Sep 14, 2020
1 parent c353916 commit 7902b9d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 108 deletions.
2 changes: 1 addition & 1 deletion examples/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","0.0.8":"2.13"}
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","0.0.11":"2.13"}
4 changes: 2 additions & 2 deletions packages/option/Option.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default {
})
const currentLabel = computed(() => {
return unref(label) || (isObject ? '' : unref(value))
return unref(label) || (unref(isObject) ? '' : unref(value))
})
const currentValue = computed(() => {
Expand All @@ -87,7 +87,7 @@ export default {
const limitReached = computed(() => {
if (select.multiple) {
return (
!itemSelected &&
!unref(itemSelected) &&
(select.modelValue || []).length >= select.multipleLimit &&
select.multipleLimit > 0
)
Expand Down
173 changes: 68 additions & 105 deletions src/use/emitter.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,92 @@
import { getCurrentInstance } from 'vue'
import { capitalize } from 'element-ui/src/utils/util'
const EVENT_NAME_KEY = Symbol('ELEMENT_EVENTS')
import mitt from 'mitt'

export function useEmitter(instance = getCurrentInstance()) {
return {
dispatch: dispatch(instance),
broadcast: broadcast(instance),
on: on(instance),
once: once(instance),
off: off(instance)
}
}
const DISPATCH = 'dispatch'
const BROADCAST = 'broadcast'

function on(instance) {
return (originalEventName, callback) => {
const eventName = 'on' + capitalize(originalEventName)
const wrapper = Symbol('wrapper')

if (!instance.vnode.props) {
instance.vnode.props = {}
}
const emitter = mitt()

if (!instance.vnode.props[EVENT_NAME_KEY]) {
instance.vnode.props[EVENT_NAME_KEY] = new Set()
}
instance.vnode.props[EVENT_NAME_KEY].add(eventName)
export function useEmitter() {
const currentComponentInstance = getCurrentInstance()

if (!instance.vnode.props[eventName]) {
instance.vnode.props[eventName] = (...params) => {
const callbacks = instance.vnode.props[eventName].__events
if (callbacks) {
callbacks.forEach((cf) => {
cf(...params)
})
function on(type, handler) {
const handleWrapper = (e) => {
const { value, type, emitComponentInstance } = e
if (type === BROADCAST) {
if (isChildComponent(currentComponentInstance, emitComponentInstance)) {
handler && handler(value)
}
} else if (type === DISPATCH) {
if (isChildComponent(emitComponentInstance, currentComponentInstance)) {
handler && handler(value)
}
} else {
handler && handler(value)
}
instance.vnode.props[eventName].__events = []
}
instance.vnode.props[eventName].__events.push(callback)
}
}
function once(instance) {
const $off = off(instance)
const $on = on(instance)
return (originalEventName, handle) => {
const _on = (...params) => {
$off(originalEventName, _on)
handle(...params)
}
$on(originalEventName, _on)
}
}

function off(instance) {
return (originalEventName, callback) => {
const eventNameList =
instance.vnode.props && instance.vnode.props[EVENT_NAME_KEY]
if (!eventNameList || !eventNameList.size) {
return
}
// Save the real handler because the need to call off
handler[wrapper] = handleWrapper
emitter.on(type, handleWrapper)
}

if (!originalEventName) {
eventNameList.forEach((eventName) => {
delete instance.vnode.props[eventName]
})
eventNameList.clear()
return
}
function broadcast(type, evt) {
emitter.emit(type, {
type: BROADCAST,
emitComponentInstance: currentComponentInstance,
value: evt
})
}

const eventName = 'on' + capitalize(originalEventName)
function dispatch(type, evt) {
emitter.emit(type, {
type: DISPATCH,
emitComponentInstance: currentComponentInstance,
value: evt
})
}

if (!callback) {
delete instance.vnode.props[eventName]
eventNameList.delete(eventName)
return
}
function off(type, handler) {
emitter.off(type, handler[wrapper])
}

const handlers =
instance.vnode.props[eventName] &&
instance.vnode.props[eventName].__events
if (handlers && handlers.length) {
const index = handlers.indexOf(callback)
if (index > -1) {
handlers.splice(index, 1)
}
}
function emit(type, evt) {
emitter.emit(type, {
value: evt
})
}
}

function dispatch(instance) {
return (componentName, eventName, ...params) => {
let parent = instance.parent
while (parent && parent.type.name !== componentName) {
parent = parent.parent
function once(type, handler) {
const handleOn = () => {
handler && handler()
off(type, handleOn)
}
on(type, handleOn)
}

if (parent) {
parent.emit(eventName, ...params)
}
return {
on,
broadcast,
dispatch,
off,
emit,
once
}
}

function broadcast(instance) {
return (componentName, eventName, ...params) => {
const _broadcast = (componentInstance) => {
if (!componentInstance) return
/**
* check componentChild is componentParent child components
* @param {*} componentChild
* @param {*} componentParent
*/
function isChildComponent(componentChild, componentParent) {
const parentUId = componentParent.uid

let children = componentInstance.subTree.children

if (children.default) {
children = children.default()
}

if (children) {
children.forEach((vnode) => {
const childComponent = vnode.component

if (childComponent?.type?.name === componentName) {
childComponent.emit(eventName, ...params)
} else {
_broadcast(childComponent)
}
})
}
}

_broadcast(instance)
while (componentChild && componentChild?.parent?.uid !== parentUId) {
componentChild = componentChild.parent
}

return Boolean(componentChild)
}

0 comments on commit 7902b9d

Please sign in to comment.