fix select custom event bug

pull/9/head
tjz 2018-02-28 23:30:40 +08:00
parent 5ef851def4
commit 44cae3f86d
4 changed files with 29 additions and 17 deletions

View File

@ -1,6 +1,16 @@
<script> <script>
import PropTypes from '../_util/vue-types' import PropTypes from '../_util/vue-types'
import { cloneElement } from '../_util/vnode' import { cloneElement, cloneVNode } from '../_util/vnode'
function chaining (...fns) {
return function (...args) { // eslint-disable-line
// eslint-disable-line
for (let i = 0; i < fns.length; i++) {
if (fns[i] && typeof fns[i] === 'function') {
fns[i].apply(this, args)
}
}
}
}
export default { export default {
props: { props: {
value: PropTypes.any, value: PropTypes.any,
@ -21,12 +31,21 @@ export default {
render () { render () {
const { $slots = {}, $listeners = {}, $props = {}, $attrs = {}} = this const { $slots = {}, $listeners = {}, $props = {}, $attrs = {}} = this
const value = $props.value === undefined ? '' : $props.value const value = $props.value === undefined ? '' : $props.value
return cloneElement($slots.default[0], { const children = cloneVNode($slots.default[0])
const { componentOptions = {}} = $slots.default[0]
const { listeners = {}} = componentOptions
const newEvent = { ...listeners }
for (const [eventName, event] of Object.entries($listeners)) {
newEvent[eventName] = chaining(event, listeners[eventName])
}
return cloneElement(children, {
domProps: { domProps: {
value, value,
}, },
props: $props, props: $props,
on: $listeners, on: newEvent,
attrs: { ...$attrs, value }, attrs: { ...$attrs, value },
ref: 'ele', ref: 'ele',
}) })

View File

@ -21,7 +21,7 @@ Customize Input Component
placeholder="input here" placeholder="input here"
class="custom" class="custom"
style="height: 50px" style="height: 50px"
@keydown="handleKeyPress" @keypress="handleKeyPress"
/> />
</a-auto-complete> </a-auto-complete>
</template> </template>

View File

@ -56,13 +56,8 @@ export default {
const { $slots } = this const { $slots } = this
const children = filterEmpty($slots.default) const children = filterEmpty($slots.default)
const element = children.length ? children[0] : <Input /> const element = children.length ? children[0] : <Input />
const { componentOptions = {}} = element
const { listeners = {}} = componentOptions
const elementProps = {
on: listeners,
}
return ( return (
<InputElement {...elementProps}>{element}</InputElement> <InputElement>{element}</InputElement>
) )
}, },

View File

@ -8,7 +8,7 @@ import warning from 'warning'
import Option from './Option' import Option from './Option'
import { hasProp, getSlotOptions, getPropsData, getValueByProp as getValue, getComponentFromProp, getEvents, getClass } from '../_util/props-util' import { hasProp, getSlotOptions, getPropsData, getValueByProp as getValue, getComponentFromProp, getEvents, getClass } from '../_util/props-util'
import getTransitionProps from '../_util/getTransitionProps' import getTransitionProps from '../_util/getTransitionProps'
import { cloneElement, cloneVNode } from '../_util/vnode' import { cloneElement } from '../_util/vnode'
import BaseMixin from '../_util/BaseMixin' import BaseMixin from '../_util/BaseMixin'
import { import {
getPropValue, getPropValue,
@ -707,9 +707,7 @@ export default {
const inputCls = classnames(getClass(inputElement), { const inputCls = classnames(getClass(inputElement), {
[`${props.prefixCls}-search__field`]: true, [`${props.prefixCls}-search__field`]: true,
}) })
// const inputElement = cloneVNode(inputElement, true)
const inputEvents = getEvents(inputElement) const inputEvents = getEvents(inputElement)
console.log(inputElement, this.inputValue)
// https://github.com/ant-design/ant-design/issues/4992#issuecomment-281542159 // https://github.com/ant-design/ant-design/issues/4992#issuecomment-281542159
// Add space to the end of the inputValue as the width measurement tolerance // Add space to the end of the inputValue as the width measurement tolerance
inputElement.data = inputElement.data || {} inputElement.data = inputElement.data || {}
@ -734,20 +732,20 @@ export default {
input: this.onInputChange, input: this.onInputChange,
keydown: chaining( keydown: chaining(
this.onInputKeydown, this.onInputKeydown,
inputEvents.keydown || noop, inputEvents.keydown,
this.$listeners.inputKeydown this.$listeners.inputKeydown
), ),
focus: chaining( focus: chaining(
this.inputFocus, this.inputFocus,
inputEvents.focus || noop, inputEvents.focus,
), ),
blur: chaining( blur: chaining(
this.inputBlur, this.inputBlur,
inputEvents.blur || noop, inputEvents.blur,
), ),
click: chaining( click: chaining(
this.inputClick, this.inputClick,
inputEvents.click || noop, inputEvents.click,
), ),
}, },
})} })}