pull/165/head
tangjinzhou 2018-02-08 18:56:58 +08:00
parent f4187ee1fe
commit e888c64626
2 changed files with 96 additions and 15 deletions

View File

@ -94,13 +94,15 @@ export default {
if (sOpen === undefined) { if (sOpen === undefined) {
sOpen = defaultOpen sOpen = defaultOpen
} }
this.adjustOpenState()
return { return {
sValue, sValue,
inputValue, inputValue,
sOpen, sOpen,
} }
}, },
beforeMount () {
this.adjustOpenState()
},
mounted () { mounted () {
this.$nextTick(() => { this.$nextTick(() => {
this.autoFocus && this.focus() this.autoFocus && this.focus()
@ -144,7 +146,10 @@ export default {
} }
}) })
}, },
beforeUpdate () {
console.log('beforeUpdate')
this.adjustOpenState()
},
beforeDestroy () { beforeDestroy () {
this.clearFocusTime() this.clearFocusTime()
this.clearBlurTime() this.clearBlurTime()
@ -315,19 +320,23 @@ export default {
}, },
onArrowClick (e) { onArrowClick (e) {
e.stopPropagation() // e.stopPropagation()
if (!this.disabled) { // if (!this.disabled) {
this.setOpenState(!this.sOpen, !this.sOpen) // this.setOpenState(!this.sOpen, !this.sOpen)
} // }
}, },
onPlaceholderClick () { onPlaceholderClick (e) {
if (this._focused) {
e.stopPropagation()
}
if (this.getInputDOMNode()) { if (this.getInputDOMNode()) {
this.getInputDOMNode().focus() this.getInputDOMNode().focus()
} }
}, },
onOuterFocus (e) { onOuterFocus (e) {
console.log('onOuterFocus')
if (this.disabled) { if (this.disabled) {
e.preventDefault() e.preventDefault()
return return
@ -386,7 +395,7 @@ export default {
// why not use setState? // why not use setState?
this.inputValue = this.getInputDOMNode().value = '' this.inputValue = this.getInputDOMNode().value = ''
} }
this._emit('blur', this.getVLForOnChange(sValue)) this.__emit('blur', this.getVLForOnChange(sValue))
this.setOpenState(false) this.setOpenState(false)
}, 10) }, 10)
}, },
@ -528,7 +537,18 @@ export default {
} }
return null return null
}, },
inputClick (e) {
if (this._focused) {
e.stopPropagation()
}
},
inputBlur (e) {
// console.log(e.target)
this.onOuterBlur()
if (!this.disabled) {
this.setOpenState(!this.sOpen, !this.sOpen)
}
},
_getInputElement () { _getInputElement () {
const props = this.$props const props = this.$props
const inputElement = props.getInputElement const inputElement = props.getInputElement
@ -537,15 +557,15 @@ export default {
const inputCls = classnames(getClass(inputElement), { const inputCls = classnames(getClass(inputElement), {
[`${props.prefixCls}-search__field`]: true, [`${props.prefixCls}-search__field`]: true,
}) })
const inputEvents = getEvents(inputElement)
// 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
return ( return (
<div class={`${props.prefixCls}-search__field__wrap`}> <div class={`${props.prefixCls}-search__field__wrap`}>
{cloneElement(inputElement, { {cloneElement(inputElement, {
props: { attrs: {
value: this.inputValue, value: this.inputValue,
disabled: props.disabled, disabled: props.disabled,
}, },
class: inputCls, class: inputCls,
ref: 'inputRef', ref: 'inputRef',
@ -553,9 +573,21 @@ export default {
input: this.onInputChange, input: this.onInputChange,
keydown: chaining( keydown: chaining(
this.onInputKeydown, this.onInputKeydown,
getEvents(inputElement).keydown || noop, inputEvents.keydown || noop,
this.$listeners.inputKeydown this.$listeners.inputKeydown
), ),
// focus: chaining(
// this.onOuterFocus,
// inputEvents.focus || noop,
// ),
blur: chaining(
this.inputBlur,
inputEvents.blur || noop,
),
click: chaining(
this.inputClick,
inputEvents.click || noop,
),
}, },
})} })}
<span <span
@ -858,7 +890,9 @@ export default {
// If hidden menu due to no options, then it should be calculated again // If hidden menu due to no options, then it should be calculated again
if (sOpen || this.hiddenForNoOptions) { if (sOpen || this.hiddenForNoOptions) {
options = this.renderFilterOptions() options = this.renderFilterOptions()
console.log('options', options)
} }
console.log('options1', options)
this._options = options this._options = options
if (isMultipleOrTagsOrCombobox($props) || !showSearch) { if (isMultipleOrTagsOrCombobox($props) || !showSearch) {
@ -874,7 +908,38 @@ export default {
} }
this.sOpen = sOpen this.sOpen = sOpen
}, },
getOptionsAndOpenStatus () {
let sOpen = this.sOpen
if (this.skipAdjustOpen) {
return {
option: this._options,
open: sOpen,
}
}
const { $props, showSearch } = this
let options = []
// If hidden menu due to no options, then it should be calculated again
if (sOpen || this.hiddenForNoOptions) {
options = this.renderFilterOptions()
}
this._options = options
if (isMultipleOrTagsOrCombobox($props) || !showSearch) {
if (sOpen && !options.length) {
sOpen = false
this.hiddenForNoOptions = true
}
// Keep menu open if there are options and hidden for no options before
if (this.hiddenForNoOptions && options.length) {
sOpen = true
this.hiddenForNoOptions = false
}
}
return {
option: this._options,
open: sOpen,
}
},
renderFilterOptions () { renderFilterOptions () {
const { inputValue } = this const { inputValue } = this
const { $slots, tags, filterOption, notFoundContent } = this const { $slots, tags, filterOption, notFoundContent } = this
@ -1219,6 +1284,16 @@ export default {
} }
return null return null
}, },
rootRefClick (e) {
// e.stopPropagation()
if (this._focused) {
// this.getInputDOMNode().blur()
this.onOuterBlur()
} else {
this.onOuterFocus()
// this.getInputDOMNode().focus()
}
},
}, },
render () { render () {
@ -1244,6 +1319,7 @@ export default {
[`${prefixCls}-enabled`]: !disabled, [`${prefixCls}-enabled`]: !disabled,
[`${prefixCls}-allow-clear`]: !!props.allowClear, [`${prefixCls}-allow-clear`]: !!props.allowClear,
} }
console.log(options)
return ( return (
<SelectTrigger <SelectTrigger
dropdownAlign={props.dropdownAlign} dropdownAlign={props.dropdownAlign}
@ -1277,8 +1353,9 @@ export default {
> >
<div <div
ref='rootRef' ref='rootRef'
onBlur={this.onOuterBlur} // onBlur={this.onOuterBlur}
onFocus={this.onOuterFocus} // onFocus={this.onOuterFocus}
onClick={this.rootRefClick}
class={classnames(rootCls)} class={classnames(rootCls)}
> >
<div <div
@ -1291,6 +1368,7 @@ export default {
aria-haspopup='true' aria-haspopup='true'
aria-expanded={sOpen} aria-expanded={sOpen}
{...extraSelectionProps} {...extraSelectionProps}
// onClick={this.stopPropagation}
> >
{ctrlNode} {ctrlNode}
{this.renderClear()} {this.renderClear()}

View File

@ -49,6 +49,7 @@ export default {
value: PropTypes.array, value: PropTypes.array,
// children: PropTypes.any, // children: PropTypes.any,
showAction: PropTypes.arrayOf(PropTypes.string), showAction: PropTypes.arrayOf(PropTypes.string),
combobox: PropTypes.bool,
}, },
data () { data () {
return { return {
@ -176,7 +177,6 @@ export default {
builtinPlacements: BUILT_IN_PLACEMENTS, builtinPlacements: BUILT_IN_PLACEMENTS,
prefixCls: dropdownPrefixCls, prefixCls: dropdownPrefixCls,
popupTransitionName: this.getDropdownTransitionName(), popupTransitionName: this.getDropdownTransitionName(),
popup: popupElement,
popupAlign: dropdownAlign, popupAlign: dropdownAlign,
popupVisible: visible, popupVisible: visible,
getPopupContainer, getPopupContainer,
@ -191,6 +191,9 @@ export default {
return ( return (
<Trigger {...triggerProps}> <Trigger {...triggerProps}>
{$slots.default} {$slots.default}
<template slot='popup'>
{popupElement}
</template>
</Trigger> </Trigger>
) )
}, },