2018-03-19 02:16:27 +00:00
|
|
|
|
|
2018-02-07 10:56:58 +00:00
|
|
|
|
import KeyCode from '../_util/KeyCode'
|
|
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
|
import classnames from 'classnames'
|
|
|
|
|
import classes from 'component-classes'
|
|
|
|
|
import { Item as MenuItem, ItemGroup as MenuItemGroup } from '../vc-menu'
|
|
|
|
|
import warning from 'warning'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
import Vue from 'vue'
|
2018-02-12 03:47:57 +00:00
|
|
|
|
import Option from './Option'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
import { hasProp, getSlotOptions, getPropsData, getValueByProp as getValue, getComponentFromProp, getEvents, getClass, getStyle, getAttrs, getOptionProps } from '../_util/props-util'
|
2018-02-07 10:56:58 +00:00
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps'
|
2018-02-28 15:30:40 +00:00
|
|
|
|
import { cloneElement } from '../_util/vnode'
|
2018-02-07 10:56:58 +00:00
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
import proxyComponent from '../_util/proxyComponent'
|
2018-11-26 13:49:05 +00:00
|
|
|
|
import ref from 'vue-ref'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
2018-11-26 13:49:05 +00:00
|
|
|
|
Vue.use(ref, { name: 'ant-ref' })
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
2018-02-07 10:56:58 +00:00
|
|
|
|
import {
|
|
|
|
|
getPropValue,
|
|
|
|
|
getValuePropValue,
|
|
|
|
|
isCombobox,
|
|
|
|
|
isMultipleOrTags,
|
|
|
|
|
isMultipleOrTagsOrCombobox,
|
|
|
|
|
isSingleMode,
|
|
|
|
|
toArray,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getMapKey,
|
|
|
|
|
findIndexInValueBySingleValue,
|
|
|
|
|
getLabelFromPropsValue,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
UNSELECTABLE_ATTRIBUTE,
|
|
|
|
|
UNSELECTABLE_STYLE,
|
|
|
|
|
preventDefaultEvent,
|
|
|
|
|
findFirstMenuItem,
|
|
|
|
|
includesSeparators,
|
|
|
|
|
splitBySeparators,
|
|
|
|
|
defaultFilterFn,
|
|
|
|
|
validateOptionValue,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
saveRef,
|
|
|
|
|
toTitle,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} from './util'
|
|
|
|
|
import SelectTrigger from './SelectTrigger'
|
|
|
|
|
import { SelectPropTypes } from './PropTypes'
|
|
|
|
|
|
2018-11-09 06:23:19 +00:00
|
|
|
|
const SELECT_EMPTY_VALUE_KEY = 'RC_SELECT_EMPTY_VALUE_KEY'
|
|
|
|
|
|
2018-02-07 10:56:58 +00:00
|
|
|
|
function noop () {}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const Select = {
|
2018-05-06 12:37:39 +00:00
|
|
|
|
inheritAttrs: false,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
name: 'Select',
|
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
|
props: {
|
|
|
|
|
...SelectPropTypes,
|
|
|
|
|
prefixCls: SelectPropTypes.prefixCls.def('rc-select'),
|
|
|
|
|
defaultOpen: PropTypes.bool.def(false),
|
|
|
|
|
labelInValue: SelectPropTypes.labelInValue.def(false),
|
|
|
|
|
defaultActiveFirstOption: SelectPropTypes.defaultActiveFirstOption.def(true),
|
|
|
|
|
showSearch: SelectPropTypes.showSearch.def(true),
|
|
|
|
|
allowClear: SelectPropTypes.allowClear.def(false),
|
|
|
|
|
placeholder: SelectPropTypes.placeholder.def(''),
|
|
|
|
|
showArrow: SelectPropTypes.showArrow.def(true),
|
|
|
|
|
dropdownMatchSelectWidth: PropTypes.bool.def(true),
|
|
|
|
|
dropdownStyle: SelectPropTypes.dropdownStyle.def({}),
|
|
|
|
|
dropdownMenuStyle: PropTypes.object.def({}),
|
|
|
|
|
optionFilterProp: SelectPropTypes.optionFilterProp.def('value'),
|
|
|
|
|
optionLabelProp: SelectPropTypes.optionLabelProp.def('value'),
|
2018-02-11 10:04:31 +00:00
|
|
|
|
notFoundContent: PropTypes.any.def('Not Found'),
|
2018-02-07 10:56:58 +00:00
|
|
|
|
backfill: PropTypes.bool.def(false),
|
|
|
|
|
showAction: SelectPropTypes.showAction.def(['click']),
|
2018-02-07 15:03:47 +00:00
|
|
|
|
combobox: PropTypes.bool.def(false),
|
2018-03-14 02:36:52 +00:00
|
|
|
|
tokenSeparators: PropTypes.arrayOf(PropTypes.string).def([]),
|
2018-09-05 13:28:54 +00:00
|
|
|
|
autoClearSearchValue: PropTypes.bool.def(true),
|
2018-02-07 10:56:58 +00:00
|
|
|
|
// onChange: noop,
|
|
|
|
|
// onFocus: noop,
|
|
|
|
|
// onBlur: noop,
|
|
|
|
|
// onSelect: noop,
|
|
|
|
|
// onSearch: noop,
|
|
|
|
|
// onDeselect: noop,
|
|
|
|
|
// onInputKeydown: noop,
|
|
|
|
|
},
|
2018-02-25 08:48:19 +00:00
|
|
|
|
model: {
|
|
|
|
|
prop: 'value',
|
|
|
|
|
event: 'change',
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
created () {
|
|
|
|
|
this.saveInputRef = saveRef(this, 'inputRef')
|
|
|
|
|
this.saveInputMirrorRef = saveRef(this, 'inputMirrorRef')
|
|
|
|
|
this.saveTopCtrlRef = saveRef(this, 'topCtrlRef')
|
|
|
|
|
this.saveSelectTriggerRef = saveRef(this, 'selectTriggerRef')
|
|
|
|
|
this.saveRootRef = saveRef(this, 'rootRef')
|
|
|
|
|
this.saveSelectionRef = saveRef(this, 'selectionRef')
|
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
data () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const props = getOptionProps(this)
|
|
|
|
|
const optionsInfo = this.getOptionsInfoFromProps(props)
|
|
|
|
|
warning(
|
|
|
|
|
this.__propsSymbol__,
|
|
|
|
|
'Replace slots.default with props.children and pass props.__propsSymbol__'
|
|
|
|
|
)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
_value: this.getValueFromProps(props, true), // true: use default value
|
|
|
|
|
_inputValue: props.combobox ? this.getInputValueForCombobox(
|
|
|
|
|
props,
|
|
|
|
|
optionsInfo,
|
|
|
|
|
true, // use default value
|
|
|
|
|
) : '',
|
|
|
|
|
_open: props.defaultOpen,
|
|
|
|
|
_optionsInfo: optionsInfo,
|
|
|
|
|
// a flag for aviod redundant getOptionsInfoFromProps call
|
|
|
|
|
_skipBuildOptionsInfo: true,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
beforeMount () {
|
|
|
|
|
const state = this.getDerivedStateFromProps(getOptionProps(this), this.$data)
|
|
|
|
|
Object.assign(this.$data, state)
|
|
|
|
|
},
|
2018-02-22 14:09:03 +00:00
|
|
|
|
|
2018-02-07 10:56:58 +00:00
|
|
|
|
mounted () {
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
this.autoFocus && this.focus()
|
|
|
|
|
})
|
|
|
|
|
},
|
2018-02-07 15:03:47 +00:00
|
|
|
|
watch: {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
__propsSymbol__ () {
|
|
|
|
|
Object.assign(this.$data, this.getDerivedStateFromProps(getOptionProps(this), this.$data))
|
2018-02-07 15:03:47 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
updated () {
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
if (isMultipleOrTags(this.$props)) {
|
|
|
|
|
const inputNode = this.getInputDOMNode()
|
|
|
|
|
const mirrorNode = this.getInputMirrorDOMNode()
|
|
|
|
|
if (inputNode.value) {
|
|
|
|
|
inputNode.style.width = ''
|
2018-02-26 14:19:48 +00:00
|
|
|
|
inputNode.style.width = `${mirrorNode.clientWidth + 10}px`
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
inputNode.style.width = ''
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.forcePopupAlign()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
beforeDestroy () {
|
|
|
|
|
this.clearFocusTime()
|
|
|
|
|
this.clearBlurTime()
|
|
|
|
|
if (this.dropdownContainer) {
|
|
|
|
|
document.body.removeChild(this.dropdownContainer)
|
|
|
|
|
this.dropdownContainer = null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getDerivedStateFromProps (nextProps, prevState) {
|
|
|
|
|
const optionsInfo = prevState._skipBuildOptionsInfo
|
|
|
|
|
? prevState._optionsInfo
|
|
|
|
|
: this.getOptionsInfoFromProps(nextProps, prevState)
|
2018-02-23 03:28:43 +00:00
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const newState = {
|
|
|
|
|
_optionsInfo: optionsInfo,
|
|
|
|
|
_skipBuildOptionsInfo: false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('open' in nextProps) {
|
|
|
|
|
newState._open = nextProps.open
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('value' in nextProps) {
|
|
|
|
|
const value = this.getValueFromProps(nextProps)
|
|
|
|
|
newState._value = value
|
|
|
|
|
if (nextProps.combobox) {
|
|
|
|
|
newState._inputValue = this.getInputValueForCombobox(
|
|
|
|
|
nextProps,
|
|
|
|
|
optionsInfo,
|
|
|
|
|
)
|
2018-02-22 09:19:47 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
return newState
|
2018-02-22 07:09:03 +00:00
|
|
|
|
},
|
2018-11-09 06:23:19 +00:00
|
|
|
|
|
2018-02-07 10:56:58 +00:00
|
|
|
|
onInputChange (event) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { tokenSeparators } = this.$props
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const val = event.target.value
|
|
|
|
|
if (
|
|
|
|
|
isMultipleOrTags(this.$props) &&
|
2018-03-14 02:36:52 +00:00
|
|
|
|
tokenSeparators.length &&
|
2018-02-07 10:56:58 +00:00
|
|
|
|
includesSeparators(val, tokenSeparators)
|
|
|
|
|
) {
|
2018-03-14 02:36:52 +00:00
|
|
|
|
const nextValue = this.getValueByInput(val)
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (nextValue !== undefined) {
|
|
|
|
|
this.fireChange(nextValue)
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setOpenState(false, true)
|
|
|
|
|
this.setInputValue('', false)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.setInputValue(val)
|
|
|
|
|
this.setState({
|
2018-09-05 13:28:54 +00:00
|
|
|
|
_open: true,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
if (isCombobox(this.$props)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.fireChange([val])
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onDropdownVisibleChange (open) {
|
|
|
|
|
if (open && !this._focused) {
|
|
|
|
|
this.clearBlurTime()
|
|
|
|
|
this.timeoutFocus()
|
|
|
|
|
this._focused = true
|
|
|
|
|
this.updateFocusClassName()
|
|
|
|
|
}
|
|
|
|
|
this.setOpenState(open)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// combobox ignore
|
|
|
|
|
onKeyDown (event) {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
const { _open: open } = this.$data
|
|
|
|
|
const { disabled } = this.$props
|
|
|
|
|
if (disabled) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const keyCode = event.keyCode
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (open && !this.getInputDOMNode()) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.onInputKeydown(event)
|
2018-11-09 06:23:19 +00:00
|
|
|
|
} else if (
|
|
|
|
|
keyCode === KeyCode.ENTER ||
|
|
|
|
|
keyCode === KeyCode.DOWN
|
|
|
|
|
) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
// vue state是同步更新,onKeyDown在onMenuSelect后会再次调用,单选时不在调用setOpenState
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (keyCode === KeyCode.ENTER && !isMultipleOrTags(this.$props)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.maybeFocus(true)
|
2018-11-09 06:23:19 +00:00
|
|
|
|
} else if (!open) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.setOpenState(true)
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
event.preventDefault()
|
2018-11-09 06:23:19 +00:00
|
|
|
|
} else if (keyCode === KeyCode.SPACE) {
|
|
|
|
|
// Not block space if popup is shown
|
|
|
|
|
if (!open) {
|
|
|
|
|
this.setOpenState(true)
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onInputKeydown (event) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const props = this.$props
|
|
|
|
|
if (props.disabled) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const state = this.$data
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const keyCode = event.keyCode
|
|
|
|
|
if (
|
2018-09-05 13:28:54 +00:00
|
|
|
|
isMultipleOrTags(props) &&
|
2018-02-07 10:56:58 +00:00
|
|
|
|
!event.target.value &&
|
|
|
|
|
keyCode === KeyCode.BACKSPACE
|
|
|
|
|
) {
|
|
|
|
|
event.preventDefault()
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { _value: value } = state
|
|
|
|
|
if (value.length) {
|
|
|
|
|
this.removeSelected(value[value.length - 1])
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (keyCode === KeyCode.DOWN) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (!state._open) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.openIfHasChildren()
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
event.stopPropagation()
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
} else if (keyCode === KeyCode.ENTER && state._open) {
|
|
|
|
|
// Aviod trigger form submit when select item
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/10861
|
|
|
|
|
event.preventDefault()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else if (keyCode === KeyCode.ESC) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (state._open) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setOpenState(false)
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
event.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (this.getRealOpenState(state)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const menu = this.selectTriggerRef.getInnerMenu()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (menu && menu.onKeyDown(event, this.handleBackfill)) {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
event.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onMenuSelect ({ item }) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (!item) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let value = this.$data._value
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const props = this.$props
|
|
|
|
|
const selectedValue = getValuePropValue(item)
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const lastValue = value[value.length - 1]
|
|
|
|
|
this.fireSelect(selectedValue)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (isMultipleOrTags(props)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (findIndexInValueBySingleValue(value, selectedValue) !== -1) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value = value.concat([selectedValue])
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (lastValue !== undefined &&
|
|
|
|
|
lastValue === selectedValue &&
|
|
|
|
|
selectedValue !== this.$data._backfillValue) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setOpenState(false, true)
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value = [selectedValue]
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setOpenState(false, true)
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.fireChange(value)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
let inputValue
|
|
|
|
|
if (isCombobox(props)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
inputValue = getPropValue(item, props.optionLabelProp)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
inputValue = ''
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (props.autoClearSearchValue) {
|
|
|
|
|
this.setInputValue(inputValue, false)
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onMenuDeselect ({ item, domEvent }) {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (domEvent.type === 'keydown' && domEvent.keyCode === KeyCode.ENTER) {
|
|
|
|
|
this.removeSelected(getValuePropValue(item))
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (domEvent.type === 'click') {
|
|
|
|
|
this.removeSelected(getValuePropValue(item))
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (this.autoClearSearchValue) {
|
|
|
|
|
this.setInputValue('', false)
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onArrowClick (e) {
|
2018-02-09 10:42:19 +00:00
|
|
|
|
e.stopPropagation()
|
2018-03-09 04:20:21 +00:00
|
|
|
|
e.preventDefault()
|
2018-02-09 10:42:19 +00:00
|
|
|
|
if (!this.disabled) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.setOpenState(!this.$data._open, !this.$data._open)
|
2018-02-09 10:42:19 +00:00
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-02-08 10:56:58 +00:00
|
|
|
|
onPlaceholderClick (e) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (this.getInputDOMNode()) {
|
|
|
|
|
this.getInputDOMNode().focus()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onPopupFocus () {
|
|
|
|
|
// fix ie scrollbar, focus element again
|
|
|
|
|
this.maybeFocus(true, true)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onClearSelection (event) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const props = this.$props
|
|
|
|
|
const state = this.$data
|
|
|
|
|
if (props.disabled) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { _inputValue: inputValue, _value: value } = state
|
|
|
|
|
event.stopPropagation()
|
|
|
|
|
if (inputValue || value.length) {
|
|
|
|
|
if (value.length) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.fireChange([])
|
|
|
|
|
}
|
2018-02-11 10:04:31 +00:00
|
|
|
|
this.setOpenState(false, true)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (inputValue) {
|
|
|
|
|
this.setInputValue('')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onChoiceAnimationLeave () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.forcePopupAlign()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getOptionsFromChildren (children = [], options = []) {
|
2018-02-12 03:47:57 +00:00
|
|
|
|
children.forEach(child => {
|
2018-02-24 11:53:21 +00:00
|
|
|
|
if (!child.data || child.data.slot !== undefined) {
|
2018-02-12 03:47:57 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (getSlotOptions(child).isSelectOptGroup) {
|
|
|
|
|
this.getOptionsFromChildren(child.componentOptions.children, options)
|
|
|
|
|
} else {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
options.push(child)
|
2018-02-12 03:47:57 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return options
|
|
|
|
|
},
|
|
|
|
|
getInputValueForCombobox (props, optionsInfo, useDefaultValue) {
|
|
|
|
|
let value = []
|
|
|
|
|
if ('value' in props && !useDefaultValue) {
|
|
|
|
|
value = toArray(props.value)
|
|
|
|
|
}
|
|
|
|
|
if ('defaultValue' in props && useDefaultValue) {
|
|
|
|
|
value = toArray(props.defaultValue)
|
|
|
|
|
}
|
|
|
|
|
if (value.length) {
|
|
|
|
|
value = value[0]
|
|
|
|
|
} else {
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
let label = value
|
|
|
|
|
if (props.labelInValue) {
|
|
|
|
|
label = value.label
|
|
|
|
|
} else if (optionsInfo[getMapKey(value)]) {
|
|
|
|
|
label = optionsInfo[getMapKey(value)].label
|
|
|
|
|
}
|
|
|
|
|
if (label === undefined) {
|
|
|
|
|
label = ''
|
|
|
|
|
}
|
|
|
|
|
return label
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getLabelFromOption (props, option) {
|
|
|
|
|
return getPropValue(option, props.optionLabelProp)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getOptionsInfoFromProps (props, preState) {
|
|
|
|
|
const options = this.getOptionsFromChildren(this.$props.children)
|
|
|
|
|
const optionsInfo = {}
|
|
|
|
|
options.forEach((option) => {
|
|
|
|
|
const singleValue = getValuePropValue(option)
|
|
|
|
|
optionsInfo[getMapKey(singleValue)] = {
|
|
|
|
|
option,
|
|
|
|
|
value: singleValue,
|
|
|
|
|
label: this.getLabelFromOption(props, option),
|
|
|
|
|
title: getValue(option, 'title'),
|
2018-02-12 03:47:57 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (preState) {
|
|
|
|
|
// keep option info in pre state value.
|
|
|
|
|
const oldOptionsInfo = preState._optionsInfo
|
|
|
|
|
const value = preState._value
|
|
|
|
|
value.forEach(v => {
|
|
|
|
|
const key = getMapKey(v)
|
|
|
|
|
if (!optionsInfo[key] && oldOptionsInfo[key] !== undefined) {
|
|
|
|
|
optionsInfo[key] = oldOptionsInfo[key]
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-12 03:47:57 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return optionsInfo
|
2018-02-12 03:47:57 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getValueFromProps (props, useDefaultValue) {
|
|
|
|
|
let value = []
|
|
|
|
|
if ('value' in props && !useDefaultValue) {
|
|
|
|
|
value = toArray(props.value)
|
|
|
|
|
}
|
|
|
|
|
if ('defaultValue' in props && useDefaultValue) {
|
|
|
|
|
value = toArray(props.defaultValue)
|
2018-02-12 03:47:57 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (props.labelInValue) {
|
|
|
|
|
value = value.map((v) => {
|
|
|
|
|
return v.key
|
|
|
|
|
})
|
2018-02-12 03:47:57 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return value
|
2018-02-12 03:47:57 +00:00
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
|
|
getOptionInfoBySingleValue (value, optionsInfo) {
|
|
|
|
|
let info
|
|
|
|
|
optionsInfo = optionsInfo || this.$data._optionsInfo
|
|
|
|
|
if (optionsInfo[getMapKey(value)]) {
|
|
|
|
|
info = optionsInfo[getMapKey(value)]
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (info) {
|
|
|
|
|
return info
|
|
|
|
|
}
|
|
|
|
|
let defaultLabel = value
|
|
|
|
|
if (this.$props.labelInValue) {
|
|
|
|
|
const label = getLabelFromPropsValue(this.$props.value, value)
|
|
|
|
|
if (label !== undefined) {
|
|
|
|
|
defaultLabel = label
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
const defaultInfo = {
|
|
|
|
|
option: <Option value={value} key={value}>{value}</Option>,
|
|
|
|
|
value,
|
|
|
|
|
label: defaultLabel,
|
|
|
|
|
}
|
|
|
|
|
return defaultInfo
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getOptionBySingleValue (value) {
|
|
|
|
|
const { option } = this.getOptionInfoBySingleValue(value)
|
|
|
|
|
return option
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getOptionsBySingleValue (values) {
|
|
|
|
|
return values.map(value => {
|
|
|
|
|
return this.getOptionBySingleValue(value)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
},
|
2018-11-09 06:23:19 +00:00
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getValueByLabel (label) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (label === undefined) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
let value = null
|
2018-09-05 13:28:54 +00:00
|
|
|
|
Object.keys(this.$data._optionsInfo).forEach(key => {
|
|
|
|
|
const info = this.$data._optionsInfo[key]
|
|
|
|
|
if (toArray(info.label).join('') === label) {
|
|
|
|
|
value = info.value
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return value
|
|
|
|
|
},
|
2018-11-09 06:23:19 +00:00
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getVLBySingleValue (value) {
|
|
|
|
|
if (this.$props.labelInValue) {
|
|
|
|
|
return {
|
|
|
|
|
key: value,
|
|
|
|
|
label: this.getLabelBySingleValue(value),
|
|
|
|
|
}
|
2018-02-24 12:54:55 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return value
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getVLForOnChange (vls_) {
|
|
|
|
|
let vls = vls_
|
|
|
|
|
if (vls !== undefined) {
|
|
|
|
|
if (!this.labelInValue) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
vls = vls.map(v => v)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
vls = vls.map(vl => ({
|
|
|
|
|
key: vl,
|
|
|
|
|
label: this.getLabelBySingleValue(vl),
|
|
|
|
|
}))
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
return isMultipleOrTags(this.$props) ? vls : vls[0]
|
|
|
|
|
}
|
|
|
|
|
return vls
|
|
|
|
|
},
|
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getLabelBySingleValue (value, optionsInfo) {
|
|
|
|
|
const { label } = this.getOptionInfoBySingleValue(value, optionsInfo)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return label
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDropdownContainer () {
|
|
|
|
|
if (!this.dropdownContainer) {
|
|
|
|
|
this.dropdownContainer = document.createElement('div')
|
|
|
|
|
document.body.appendChild(this.dropdownContainer)
|
|
|
|
|
}
|
|
|
|
|
return this.dropdownContainer
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getPlaceholderElement () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { $props: props, $data: state } = this
|
2018-02-07 10:56:58 +00:00
|
|
|
|
let hidden = false
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (state._inputValue) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
hidden = true
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (state._value.length) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
hidden = true
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (isCombobox(props) && state._value.length === 1 && !state._value[0]) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
hidden = false
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const placeholder = props.placeholder
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (placeholder) {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
const p = {
|
|
|
|
|
on: {
|
|
|
|
|
mousedown: preventDefaultEvent,
|
|
|
|
|
click: this.onPlaceholderClick,
|
|
|
|
|
},
|
|
|
|
|
attrs: UNSELECTABLE_ATTRIBUTE,
|
|
|
|
|
style: {
|
|
|
|
|
display: hidden ? 'none' : 'block',
|
|
|
|
|
...UNSELECTABLE_STYLE,
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
class: `${props.prefixCls}-selection__placeholder`,
|
2018-02-07 15:03:47 +00:00
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return (
|
2018-02-07 15:03:47 +00:00
|
|
|
|
<div {...p}>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{placeholder}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
},
|
2018-02-08 10:56:58 +00:00
|
|
|
|
inputClick (e) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (this.$data._open) {
|
2018-03-01 04:34:31 +00:00
|
|
|
|
this.clearBlurTime()
|
2018-02-26 14:19:48 +00:00
|
|
|
|
e.stopPropagation()
|
|
|
|
|
} else {
|
|
|
|
|
this._focused = false
|
2018-02-08 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
inputBlur (e) {
|
2018-02-08 13:58:43 +00:00
|
|
|
|
this.clearBlurTime()
|
2018-02-11 10:04:31 +00:00
|
|
|
|
if (this.disabled) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-02-08 13:58:43 +00:00
|
|
|
|
this.blurTimer = setTimeout(() => {
|
2018-02-11 10:04:31 +00:00
|
|
|
|
this._focused = false
|
|
|
|
|
this.updateFocusClassName()
|
|
|
|
|
const props = this.$props
|
2018-09-05 13:28:54 +00:00
|
|
|
|
let { _value: value } = this.$data
|
|
|
|
|
const { _inputValue: inputValue } = this.$data
|
2018-02-11 10:04:31 +00:00
|
|
|
|
if (
|
|
|
|
|
isSingleMode(props) &&
|
|
|
|
|
props.showSearch &&
|
|
|
|
|
inputValue &&
|
|
|
|
|
props.defaultActiveFirstOption
|
|
|
|
|
) {
|
|
|
|
|
const options = this._options || []
|
|
|
|
|
if (options.length) {
|
|
|
|
|
const firstOption = findFirstMenuItem(options)
|
|
|
|
|
if (firstOption) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value = [getValuePropValue(firstOption)]
|
|
|
|
|
this.fireChange(value)
|
2018-02-11 10:04:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (isMultipleOrTags(props) && inputValue) {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (this._mouseDown) {
|
|
|
|
|
// need update dropmenu when not blur
|
|
|
|
|
this.setInputValue('')
|
|
|
|
|
} else {
|
|
|
|
|
// why not use setState?
|
|
|
|
|
this.$data._inputValue = this.getInputDOMNode().value = ''
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value = this.getValueByInput(inputValue)
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
this.fireChange(value)
|
|
|
|
|
}
|
2018-02-08 13:58:43 +00:00
|
|
|
|
}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
// if click the rest space of Select in multiple mode
|
|
|
|
|
if (isMultipleOrTags(props) && this._mouseDown) {
|
|
|
|
|
this.maybeFocus(true, true)
|
|
|
|
|
this._mouseDown = false
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-02-11 10:04:31 +00:00
|
|
|
|
this.setOpenState(false)
|
2018-11-09 06:23:19 +00:00
|
|
|
|
this.$emit('blur', this.getVLForOnChange(value))
|
2018-02-08 13:58:43 +00:00
|
|
|
|
}, 10)
|
2018-02-08 10:56:58 +00:00
|
|
|
|
},
|
2018-02-09 10:42:19 +00:00
|
|
|
|
inputFocus (e) {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (this.$props.disabled) {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-02-09 10:42:19 +00:00
|
|
|
|
this.clearBlurTime()
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (
|
|
|
|
|
!isMultipleOrTagsOrCombobox(this.$props) &&
|
|
|
|
|
e.target === this.getInputDOMNode()
|
|
|
|
|
) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (this._focused) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this._focused = true
|
|
|
|
|
this.updateFocusClassName()
|
|
|
|
|
// only effect multiple or tag mode
|
|
|
|
|
if (!isMultipleOrTags(this.$props) || !this._mouseDown) {
|
|
|
|
|
this.timeoutFocus()
|
|
|
|
|
}
|
2018-02-09 10:42:19 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
_getInputElement () {
|
|
|
|
|
const props = this.$props
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { _inputValue: inputValue } = this.$data
|
2018-05-06 12:37:39 +00:00
|
|
|
|
const attrs = getAttrs(this)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const inputElement = props.getInputElement
|
|
|
|
|
? props.getInputElement()
|
2018-05-06 12:37:39 +00:00
|
|
|
|
: <input id={attrs.id} autoComplete='off'/>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const inputCls = classnames(getClass(inputElement), {
|
|
|
|
|
[`${props.prefixCls}-search__field`]: true,
|
|
|
|
|
})
|
2018-02-08 10:56:58 +00:00
|
|
|
|
const inputEvents = getEvents(inputElement)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4992#issuecomment-281542159
|
|
|
|
|
// Add space to the end of the inputValue as the width measurement tolerance
|
2018-02-09 10:42:19 +00:00
|
|
|
|
inputElement.data = inputElement.data || {}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return (
|
2018-03-01 04:34:31 +00:00
|
|
|
|
<div class={`${props.prefixCls}-search__field__wrap`} onClick={this.inputClick}>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{cloneElement(inputElement, {
|
2018-02-28 11:07:04 +00:00
|
|
|
|
props: {
|
|
|
|
|
disabled: props.disabled,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: inputValue,
|
2018-02-28 11:07:04 +00:00
|
|
|
|
},
|
2018-02-08 10:56:58 +00:00
|
|
|
|
attrs: {
|
2018-02-09 10:42:19 +00:00
|
|
|
|
...(inputElement.data.attrs || {}),
|
2018-02-07 10:56:58 +00:00
|
|
|
|
disabled: props.disabled,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: inputValue,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
2018-02-09 10:42:19 +00:00
|
|
|
|
domProps: {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: inputValue,
|
2018-02-09 10:42:19 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
class: inputCls,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
directives: [{
|
|
|
|
|
name: 'ant-ref',
|
|
|
|
|
value: this.saveInputRef,
|
|
|
|
|
}],
|
2018-02-07 10:56:58 +00:00
|
|
|
|
on: {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
input: this.onInputChange,
|
|
|
|
|
keydown: chaining(
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.onInputKeydown,
|
2018-02-28 15:30:40 +00:00
|
|
|
|
inputEvents.keydown,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.$listeners.inputKeydown
|
|
|
|
|
),
|
2018-02-09 10:42:19 +00:00
|
|
|
|
focus: chaining(
|
|
|
|
|
this.inputFocus,
|
2018-02-28 15:30:40 +00:00
|
|
|
|
inputEvents.focus,
|
2018-02-09 10:42:19 +00:00
|
|
|
|
),
|
2018-02-08 10:56:58 +00:00
|
|
|
|
blur: chaining(
|
|
|
|
|
this.inputBlur,
|
2018-02-28 15:30:40 +00:00
|
|
|
|
inputEvents.blur,
|
2018-02-08 10:56:58 +00:00
|
|
|
|
),
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
})}
|
|
|
|
|
<span
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{...{ directives: [{
|
2018-12-14 15:31:02 +00:00
|
|
|
|
name: 'ant-ref',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: this.saveInputMirrorRef,
|
|
|
|
|
}] }}
|
|
|
|
|
// ref='inputMirrorRef'
|
2018-02-07 10:56:58 +00:00
|
|
|
|
class={`${props.prefixCls}-search__field__mirror`}
|
|
|
|
|
>
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{inputValue}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getInputDOMNode () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return this.topCtrlRef
|
|
|
|
|
? this.topCtrlRef.querySelector('input,textarea,div[contentEditable]')
|
|
|
|
|
: this.inputRef
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getInputMirrorDOMNode () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return this.inputMirrorRef
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getPopupDOMNode () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return this.selectTriggerRef.getPopupDOMNode()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getPopupMenuComponent () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return this.selectTriggerRef.getInnerMenu()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
2018-02-11 10:04:31 +00:00
|
|
|
|
setOpenState (open, needFocus) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { $props: props, $data: state } = this
|
|
|
|
|
if (state._open === open) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.maybeFocus(open, needFocus)
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
this.__emit('dropdownVisibleChange', open)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const nextState = {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
_open: open,
|
|
|
|
|
_backfillValue: undefined,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
// clear search input value when open is false in singleMode.
|
|
|
|
|
if (!open && isSingleMode(props) && props.showSearch) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.setInputValue('', false)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
if (!open) {
|
|
|
|
|
this.maybeFocus(open, needFocus)
|
|
|
|
|
}
|
|
|
|
|
this.setState(nextState, () => {
|
|
|
|
|
if (open) {
|
|
|
|
|
this.maybeFocus(open, needFocus)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setInputValue (inputValue, fireSearch = true) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (inputValue !== this.$data._inputValue) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setState({
|
2018-09-05 13:28:54 +00:00
|
|
|
|
_inputValue: inputValue,
|
|
|
|
|
}, this.forcePopupAlign)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (fireSearch) {
|
2018-02-24 10:12:24 +00:00
|
|
|
|
this.$emit('search', inputValue)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
getValueByInput (string) {
|
|
|
|
|
const { multiple, tokenSeparators } = this.$props
|
|
|
|
|
let nextValue = this.$data._value
|
|
|
|
|
let hasNewValue = false
|
2018-03-14 02:36:52 +00:00
|
|
|
|
splitBySeparators(string, tokenSeparators).forEach(label => {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const selectedValue = [label]
|
|
|
|
|
if (multiple) {
|
|
|
|
|
const value = this.getValueByLabel(label)
|
|
|
|
|
if (value && findIndexInValueBySingleValue(nextValue, value) === -1) {
|
|
|
|
|
nextValue = nextValue.concat(value)
|
|
|
|
|
hasNewValue = true
|
|
|
|
|
this.fireSelect(value)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// tag
|
|
|
|
|
if (findIndexInValueBySingleValue(nextValue, label) === -1) {
|
2018-03-14 02:36:52 +00:00
|
|
|
|
nextValue = nextValue.concat(selectedValue)
|
2018-09-05 13:28:54 +00:00
|
|
|
|
hasNewValue = true
|
|
|
|
|
this.fireSelect(label)
|
2018-03-14 02:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return hasNewValue ? nextValue : undefined
|
2018-03-14 02:36:52 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
|
2018-11-09 06:23:19 +00:00
|
|
|
|
getRealOpenState (state) {
|
|
|
|
|
const { open: _open } = this.$props
|
|
|
|
|
if (typeof _open === 'boolean') {
|
|
|
|
|
return _open
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let open = (state || this.$data)._open
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const options = this._options || []
|
|
|
|
|
if (isMultipleOrTagsOrCombobox(this.$props) || !this.$props.showSearch) {
|
|
|
|
|
if (open && !options.length) {
|
|
|
|
|
open = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return open
|
|
|
|
|
},
|
|
|
|
|
|
2018-02-07 10:56:58 +00:00
|
|
|
|
focus () {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
if (isSingleMode(this.$props)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.selectionRef.focus()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.getInputDOMNode().focus()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
blur () {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
if (isSingleMode(this.$props)) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.selectionRef.blur()
|
2018-02-07 10:56:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.getInputDOMNode().blur()
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-11-09 06:23:19 +00:00
|
|
|
|
markMouseDown () {
|
|
|
|
|
this._mouseDown = true
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
markMouseLeave () {
|
|
|
|
|
this._mouseDown = false
|
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
|
|
|
|
|
handleBackfill (item) {
|
|
|
|
|
if (!this.backfill || !(isSingleMode(this.$props) || isCombobox(this.$props))) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const key = getValuePropValue(item)
|
|
|
|
|
|
|
|
|
|
if (isCombobox(this.$props)) {
|
|
|
|
|
this.setInputValue(key, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState({
|
2018-09-05 13:28:54 +00:00
|
|
|
|
_value: [key],
|
|
|
|
|
_backfillValue: key,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_filterOption (input, child, defaultFilter = defaultFilterFn) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { _value: value, _backfillValue: backfillValue } = this.$data
|
|
|
|
|
const lastValue = value[value.length - 1]
|
|
|
|
|
if (!input || (lastValue && lastValue === backfillValue)) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
let filterFn = this.$props.filterOption
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (hasProp(this, 'filterOption')) {
|
|
|
|
|
if (this.filterOption === true) {
|
|
|
|
|
filterFn = defaultFilter
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
filterFn = defaultFilter
|
|
|
|
|
}
|
|
|
|
|
if (!filterFn) {
|
|
|
|
|
return true
|
|
|
|
|
} else if (typeof filterFn === 'function') {
|
|
|
|
|
return filterFn.call(this, input, child)
|
|
|
|
|
} else if (getValue(child, 'disabled')) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
timeoutFocus () {
|
|
|
|
|
if (this.focusTimer) {
|
|
|
|
|
this.clearFocusTime()
|
|
|
|
|
}
|
|
|
|
|
this.focusTimer = setTimeout(() => {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
// this._focused = true
|
|
|
|
|
// this.updateFocusClassName()
|
2018-02-24 10:12:24 +00:00
|
|
|
|
this.$emit('focus')
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}, 10)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearFocusTime () {
|
|
|
|
|
if (this.focusTimer) {
|
|
|
|
|
clearTimeout(this.focusTimer)
|
|
|
|
|
this.focusTimer = null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearBlurTime () {
|
|
|
|
|
if (this.blurTimer) {
|
|
|
|
|
clearTimeout(this.blurTimer)
|
|
|
|
|
this.blurTimer = null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateFocusClassName () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { rootRef, prefixCls } = this
|
2018-02-07 10:56:58 +00:00
|
|
|
|
// avoid setState and its side effect
|
|
|
|
|
if (this._focused) {
|
|
|
|
|
classes(rootRef).add(`${prefixCls}-focused`)
|
|
|
|
|
} else {
|
|
|
|
|
classes(rootRef).remove(`${prefixCls}-focused`)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
maybeFocus (open, needFocus) {
|
|
|
|
|
if (needFocus || open) {
|
|
|
|
|
const input = this.getInputDOMNode()
|
|
|
|
|
const { activeElement } = document
|
2018-02-07 15:03:47 +00:00
|
|
|
|
if (input && (open || isMultipleOrTagsOrCombobox(this.$props))) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (activeElement !== input) {
|
|
|
|
|
input.focus()
|
|
|
|
|
this._focused = true
|
|
|
|
|
}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
} else if (activeElement !== this.selectionRef) {
|
|
|
|
|
this.selectionRef.focus()
|
|
|
|
|
this._focused = true
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
removeSelected (selectedKey, e) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const props = this.$props
|
|
|
|
|
if (props.disabled || this.isChildDisabled(selectedKey)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
// Do not trigger Trigger popup
|
|
|
|
|
if (e && e.stopPropagation) {
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const value = this.$data._value.filter(singleValue => {
|
|
|
|
|
return singleValue !== selectedKey
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
const canMultiple = isMultipleOrTags(props)
|
|
|
|
|
|
|
|
|
|
if (canMultiple) {
|
|
|
|
|
let event = selectedKey
|
|
|
|
|
if (props.labelInValue) {
|
|
|
|
|
event = {
|
|
|
|
|
key: selectedKey,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
label: this.getLabelBySingleValue(selectedKey),
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.$emit('deselect', event, this.getOptionBySingleValue(selectedKey))
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
this.fireChange(value)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
openIfHasChildren () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { $props } = this
|
|
|
|
|
if (($props.children && $props.children.length) || isSingleMode($props)) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setOpenState(true)
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-02-12 03:47:57 +00:00
|
|
|
|
fireSelect (value) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.$emit('select', this.getVLBySingleValue(value), this.getOptionBySingleValue(value))
|
2018-02-12 03:47:57 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
fireChange (value) {
|
2018-02-12 10:10:51 +00:00
|
|
|
|
if (!hasProp(this, 'value')) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
this.setState({
|
2018-09-05 13:28:54 +00:00
|
|
|
|
_value: value,
|
|
|
|
|
}, this.forcePopupAlign)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
2018-02-12 03:47:57 +00:00
|
|
|
|
const vls = this.getVLForOnChange(value)
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const options = this.getOptionsBySingleValue(value)
|
2018-02-12 03:47:57 +00:00
|
|
|
|
this._valueOptions = options
|
2018-02-24 10:12:24 +00:00
|
|
|
|
this.$emit('change', vls, isMultipleOrTags(this.$props) ? options : options[0])
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isChildDisabled (key) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
return (this.$props.children || []).some(child => {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const childValue = getValuePropValue(child)
|
2018-02-12 10:10:51 +00:00
|
|
|
|
return childValue === key && getValue(child, 'disabled')
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
forcePopupAlign () {
|
2018-11-09 06:23:19 +00:00
|
|
|
|
if (!this.$data._open) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-21 02:00:13 +00:00
|
|
|
|
this.selectTriggerRef && this.selectTriggerRef.triggerRef.forcePopupAlign()
|
2018-02-08 10:56:58 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
renderFilterOptions () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { _inputValue: inputValue } = this.$data
|
|
|
|
|
const { children, tags, filterOption, notFoundContent } = this.$props
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const menuItems = []
|
|
|
|
|
const childrenKeys = []
|
|
|
|
|
let options = this.renderFilterOptionsFromChildren(
|
2018-09-05 13:28:54 +00:00
|
|
|
|
children,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
childrenKeys,
|
|
|
|
|
menuItems,
|
|
|
|
|
)
|
|
|
|
|
if (tags) {
|
|
|
|
|
// tags value must be string
|
2018-09-05 13:28:54 +00:00
|
|
|
|
let value = this.$data._value
|
2018-02-07 10:56:58 +00:00
|
|
|
|
value = value.filter(singleValue => {
|
|
|
|
|
return (
|
2018-09-05 13:28:54 +00:00
|
|
|
|
childrenKeys.indexOf(singleValue) === -1 &&
|
2018-02-07 10:56:58 +00:00
|
|
|
|
(!inputValue ||
|
2018-09-05 13:28:54 +00:00
|
|
|
|
String(singleValue).indexOf(String(inputValue)) > -1)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
value.forEach(singleValue => {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const key = singleValue
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const menuItem = (
|
|
|
|
|
<MenuItem
|
|
|
|
|
style={UNSELECTABLE_STYLE}
|
2018-02-07 15:03:47 +00:00
|
|
|
|
{...{ attrs: UNSELECTABLE_ATTRIBUTE }}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
value={key}
|
|
|
|
|
key={key}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
role='option'
|
2018-02-07 10:56:58 +00:00
|
|
|
|
>
|
|
|
|
|
{key}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
)
|
|
|
|
|
options.push(menuItem)
|
|
|
|
|
menuItems.push(menuItem)
|
|
|
|
|
})
|
|
|
|
|
if (inputValue) {
|
|
|
|
|
const notFindInputItem = menuItems.every(option => {
|
|
|
|
|
// this.filterOption return true has two meaning,
|
|
|
|
|
// 1, some one exists after filtering
|
|
|
|
|
// 2, filterOption is set to false
|
|
|
|
|
// condition 2 does not mean the option has same value with inputValue
|
|
|
|
|
const filterFn = () => getValuePropValue(option) === inputValue
|
|
|
|
|
if (filterOption !== false) {
|
|
|
|
|
return !this._filterOption(
|
|
|
|
|
inputValue,
|
|
|
|
|
option,
|
|
|
|
|
filterFn
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return !filterFn()
|
|
|
|
|
})
|
|
|
|
|
if (notFindInputItem) {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
const p = {
|
|
|
|
|
attrs: UNSELECTABLE_ATTRIBUTE,
|
|
|
|
|
key: inputValue,
|
|
|
|
|
props: {
|
|
|
|
|
value: inputValue,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
role: 'option',
|
2018-02-07 15:03:47 +00:00
|
|
|
|
},
|
|
|
|
|
style: UNSELECTABLE_STYLE,
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
options.unshift(
|
2018-02-07 15:03:47 +00:00
|
|
|
|
<MenuItem {...p}>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{inputValue}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!options.length && notFoundContent) {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
const p = {
|
|
|
|
|
attrs: UNSELECTABLE_ATTRIBUTE,
|
|
|
|
|
key: 'NOT_FOUND',
|
|
|
|
|
props: {
|
|
|
|
|
value: 'NOT_FOUND',
|
|
|
|
|
disabled: true,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
role: 'option',
|
2018-02-07 15:03:47 +00:00
|
|
|
|
},
|
|
|
|
|
style: UNSELECTABLE_STYLE,
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
options = [
|
2018-02-07 15:03:47 +00:00
|
|
|
|
<MenuItem {...p}>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{notFoundContent}
|
|
|
|
|
</MenuItem>,
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
return options
|
|
|
|
|
},
|
|
|
|
|
|
2018-02-11 10:04:31 +00:00
|
|
|
|
renderFilterOptionsFromChildren (children = [], childrenKeys, menuItems) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const sel = []
|
|
|
|
|
const props = this.$props
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { _inputValue: inputValue } = this.$data
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const tags = props.tags
|
|
|
|
|
children.forEach(child => {
|
2018-02-24 11:53:21 +00:00
|
|
|
|
if (!child.data || child.data.slot !== undefined) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (getSlotOptions(child).isSelectOptGroup) {
|
|
|
|
|
const innerItems = this.renderFilterOptionsFromChildren(
|
|
|
|
|
child.componentOptions.children,
|
|
|
|
|
childrenKeys,
|
|
|
|
|
menuItems,
|
|
|
|
|
)
|
|
|
|
|
if (innerItems.length) {
|
2018-02-24 10:12:24 +00:00
|
|
|
|
let label = getComponentFromProp(child, 'label')
|
2018-02-07 10:56:58 +00:00
|
|
|
|
let key = child.key
|
|
|
|
|
if (!key && typeof label === 'string') {
|
|
|
|
|
key = label
|
|
|
|
|
} else if (!label && key) {
|
|
|
|
|
label = key
|
|
|
|
|
}
|
|
|
|
|
sel.push(
|
2018-02-28 11:07:04 +00:00
|
|
|
|
<MenuItemGroup key={key} title={label} class ={getClass(child)}>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{innerItems}
|
|
|
|
|
</MenuItemGroup>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
warning(
|
2018-02-07 15:03:47 +00:00
|
|
|
|
getSlotOptions(child).isSelectOption,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
'the children of `Select` should be `Select.Option` or `Select.OptGroup`, ' +
|
2018-02-07 15:03:47 +00:00
|
|
|
|
`instead of \`${getSlotOptions(child).name ||
|
2018-02-07 10:56:58 +00:00
|
|
|
|
getSlotOptions(child)}\`.`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const childValue = getValuePropValue(child)
|
|
|
|
|
|
|
|
|
|
validateOptionValue(childValue, this.$props)
|
|
|
|
|
if (this._filterOption(inputValue, child)) {
|
2018-02-07 15:03:47 +00:00
|
|
|
|
const p = {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
attrs: {
|
|
|
|
|
...UNSELECTABLE_ATTRIBUTE,
|
|
|
|
|
...getAttrs(child),
|
|
|
|
|
},
|
2018-02-07 15:03:47 +00:00
|
|
|
|
key: childValue,
|
|
|
|
|
props: {
|
|
|
|
|
value: childValue,
|
|
|
|
|
...getPropsData(child),
|
2018-09-05 13:28:54 +00:00
|
|
|
|
role: 'option',
|
2018-02-07 15:03:47 +00:00
|
|
|
|
},
|
|
|
|
|
style: UNSELECTABLE_STYLE,
|
|
|
|
|
on: getEvents(child),
|
2018-02-28 11:07:04 +00:00
|
|
|
|
class: getClass(child),
|
2018-02-07 15:03:47 +00:00
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const menuItem = (
|
2018-02-07 15:03:47 +00:00
|
|
|
|
<MenuItem {...p}>{child.componentOptions.children}</MenuItem>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
)
|
|
|
|
|
sel.push(menuItem)
|
|
|
|
|
menuItems.push(menuItem)
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (tags) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
childrenKeys.push(childValue)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return sel
|
|
|
|
|
},
|
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
renderTopControlNode () {
|
|
|
|
|
const { $props: props } = this
|
|
|
|
|
const { _value: value, _inputValue: inputValue, _open: open } = this.$data
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const {
|
|
|
|
|
choiceTransitionName,
|
|
|
|
|
prefixCls,
|
|
|
|
|
maxTagTextLength,
|
|
|
|
|
maxTagCount,
|
|
|
|
|
maxTagPlaceholder,
|
|
|
|
|
showSearch,
|
|
|
|
|
} = props
|
2018-11-09 06:23:19 +00:00
|
|
|
|
const removeIcon = getComponentFromProp(this, 'removeIcon')
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const className = `${prefixCls}-selection__rendered`
|
|
|
|
|
// search input is inside topControlNode in single, multiple & combobox. 2016/04/13
|
|
|
|
|
let innerNode = null
|
|
|
|
|
if (isSingleMode(props)) {
|
|
|
|
|
let selectedValue = null
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (value.length) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
let showSelectedValue = false
|
|
|
|
|
let opacity = 1
|
|
|
|
|
if (!showSearch) {
|
|
|
|
|
showSelectedValue = true
|
2018-11-09 06:23:19 +00:00
|
|
|
|
} else if (open) {
|
|
|
|
|
showSelectedValue = !inputValue
|
|
|
|
|
if (showSelectedValue) {
|
|
|
|
|
opacity = 0.4
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
showSelectedValue = true
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const singleValue = value[0]
|
|
|
|
|
const { label, title } = this.getOptionInfoBySingleValue(singleValue)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
selectedValue = (
|
|
|
|
|
<div
|
|
|
|
|
key='value'
|
|
|
|
|
class={`${prefixCls}-selection-selected-value`}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
title={toTitle(title || label)}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
style={{
|
|
|
|
|
display: showSelectedValue ? 'block' : 'none',
|
|
|
|
|
opacity,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{label}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if (!showSearch) {
|
|
|
|
|
innerNode = [selectedValue]
|
|
|
|
|
} else {
|
|
|
|
|
innerNode = [
|
|
|
|
|
selectedValue,
|
|
|
|
|
<div
|
|
|
|
|
class={`${prefixCls}-search ${prefixCls}-search--inline`}
|
|
|
|
|
key='input'
|
|
|
|
|
style={{
|
2018-09-05 13:28:54 +00:00
|
|
|
|
display: open ? 'block' : 'none',
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{this._getInputElement()}
|
|
|
|
|
</div>,
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let selectedValueNodes = []
|
2018-09-05 13:28:54 +00:00
|
|
|
|
let limitedCountValue = value
|
2018-02-07 10:56:58 +00:00
|
|
|
|
let maxTagPlaceholderEl
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (maxTagCount !== undefined && value.length > maxTagCount) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
limitedCountValue = limitedCountValue.slice(0, maxTagCount)
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const omittedValues = this.getVLForOnChange(value.slice(maxTagCount, value.length))
|
|
|
|
|
let content = `+ ${value.length - maxTagCount} ...`
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (maxTagPlaceholder) {
|
|
|
|
|
content = typeof maxTagPlaceholder === 'function'
|
|
|
|
|
? maxTagPlaceholder(omittedValues) : maxTagPlaceholder
|
|
|
|
|
}
|
|
|
|
|
maxTagPlaceholderEl = (<li
|
|
|
|
|
style={UNSELECTABLE_STYLE}
|
2018-02-07 15:03:47 +00:00
|
|
|
|
unselectable='unselectable'
|
|
|
|
|
onMousedown={preventDefaultEvent}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
class={`${prefixCls}-selection__choice ${prefixCls}-selection__choice__disabled`}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
key='maxTagPlaceholder'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
title={toTitle(content)}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
>
|
|
|
|
|
<div class={`${prefixCls}-selection__choice__content`}>{content}</div>
|
|
|
|
|
</li>)
|
|
|
|
|
}
|
|
|
|
|
if (isMultipleOrTags(props)) {
|
|
|
|
|
selectedValueNodes = limitedCountValue.map(singleValue => {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const info = this.getOptionInfoBySingleValue(singleValue)
|
|
|
|
|
let content = info.label
|
|
|
|
|
const title = info.title || content
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (
|
|
|
|
|
maxTagTextLength &&
|
|
|
|
|
typeof content === 'string' &&
|
|
|
|
|
content.length > maxTagTextLength
|
|
|
|
|
) {
|
|
|
|
|
content = `${content.slice(0, maxTagTextLength)}...`
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const disabled = this.isChildDisabled(singleValue)
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const choiceClassName = disabled
|
|
|
|
|
? `${prefixCls}-selection__choice ${prefixCls}-selection__choice__disabled`
|
|
|
|
|
: `${prefixCls}-selection__choice`
|
|
|
|
|
return (
|
|
|
|
|
<li
|
|
|
|
|
style={UNSELECTABLE_STYLE}
|
2018-02-07 15:03:47 +00:00
|
|
|
|
unselectable='unselectable'
|
2018-02-07 10:56:58 +00:00
|
|
|
|
onMousedown={preventDefaultEvent}
|
|
|
|
|
class={choiceClassName}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
key={singleValue || SELECT_EMPTY_VALUE_KEY}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
title={toTitle(title)}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
>
|
|
|
|
|
<div class={`${prefixCls}-selection__choice__content`}>
|
|
|
|
|
{content}
|
|
|
|
|
</div>
|
|
|
|
|
{disabled ? null : (
|
|
|
|
|
<span
|
2018-09-05 13:28:54 +00:00
|
|
|
|
onClick={(event) => {
|
|
|
|
|
this.removeSelected(singleValue, event)
|
|
|
|
|
}}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
class={`${prefixCls}-selection__choice__remove`}
|
|
|
|
|
>
|
|
|
|
|
{removeIcon || <i class={`${prefixCls}-selection__choice__remove-icon`}>×</i>}
|
|
|
|
|
</span>)}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
</li>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if (maxTagPlaceholderEl) {
|
|
|
|
|
selectedValueNodes.push(maxTagPlaceholderEl)
|
|
|
|
|
}
|
|
|
|
|
selectedValueNodes.push(
|
|
|
|
|
<li
|
|
|
|
|
class={`${prefixCls}-search ${prefixCls}-search--inline`}
|
|
|
|
|
key='__input'
|
|
|
|
|
>
|
|
|
|
|
{this._getInputElement()}
|
|
|
|
|
</li>
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (isMultipleOrTags(props) && choiceTransitionName) {
|
|
|
|
|
const transitionProps = getTransitionProps(choiceTransitionName, {
|
|
|
|
|
tag: 'ul',
|
2018-02-12 10:10:51 +00:00
|
|
|
|
afterLeave: this.onChoiceAnimationLeave,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
})
|
|
|
|
|
innerNode = (
|
|
|
|
|
<transition-group
|
|
|
|
|
{...transitionProps}
|
|
|
|
|
>
|
|
|
|
|
{selectedValueNodes}
|
|
|
|
|
</transition-group>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
innerNode = (
|
2018-02-12 10:10:51 +00:00
|
|
|
|
<ul>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{selectedValueNodes}
|
|
|
|
|
</ul>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (
|
2018-09-05 13:28:54 +00:00
|
|
|
|
<div
|
|
|
|
|
class={className}
|
|
|
|
|
{...{ directives: [{
|
2018-12-14 15:31:02 +00:00
|
|
|
|
name: 'ant-ref',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: this.saveTopCtrlRef,
|
|
|
|
|
}] }}
|
|
|
|
|
onClick={this.topCtrlContainerClick}
|
|
|
|
|
>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{this.getPlaceholderElement()}
|
|
|
|
|
{innerNode}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
2018-02-24 12:54:55 +00:00
|
|
|
|
topCtrlContainerClick (e) {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (this.$data._open && !isSingleMode(this.$props)) {
|
2018-02-12 06:45:26 +00:00
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
renderClear () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { prefixCls, allowClear } = this.$props
|
|
|
|
|
const { _value: value, _inputValue: inputValue } = this.$data
|
2018-11-09 06:23:19 +00:00
|
|
|
|
const clearIcon = getComponentFromProp(this, 'clearIcon')
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const clear = (
|
|
|
|
|
<span
|
|
|
|
|
key='clear'
|
2018-11-09 06:23:19 +00:00
|
|
|
|
class={`${prefixCls}-selection__clear`}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
onMousedown={preventDefaultEvent}
|
|
|
|
|
style={UNSELECTABLE_STYLE}
|
2018-02-07 15:03:47 +00:00
|
|
|
|
unselectable='unselectable'
|
2018-02-07 10:56:58 +00:00
|
|
|
|
onClick={this.onClearSelection}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
>{clearIcon || <i class={`${prefixCls}-selection__clear-icon`}>×</i>}</span>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
)
|
|
|
|
|
if (!allowClear) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
if (isCombobox(this.$props)) {
|
|
|
|
|
if (inputValue) {
|
|
|
|
|
return clear
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (inputValue || value.length) {
|
2018-02-07 10:56:58 +00:00
|
|
|
|
return clear
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
},
|
2018-11-09 06:23:19 +00:00
|
|
|
|
|
2018-02-09 10:42:19 +00:00
|
|
|
|
selectionRefClick (e) {
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
if (!this.disabled) {
|
2018-02-12 10:10:51 +00:00
|
|
|
|
const input = this.getInputDOMNode()
|
2018-09-05 13:28:54 +00:00
|
|
|
|
if (this._focused && this.$data._open) {
|
2018-02-09 10:42:19 +00:00
|
|
|
|
this._focused = false
|
2018-02-11 10:04:31 +00:00
|
|
|
|
this.setOpenState(false, false)
|
2018-02-12 10:10:51 +00:00
|
|
|
|
input && input.blur()
|
2018-02-09 10:42:19 +00:00
|
|
|
|
} else {
|
2018-02-26 10:44:06 +00:00
|
|
|
|
this.clearBlurTime()
|
2018-02-09 10:42:19 +00:00
|
|
|
|
this._focused = true
|
2018-02-11 10:04:31 +00:00
|
|
|
|
this.setOpenState(true, true)
|
2018-02-12 10:10:51 +00:00
|
|
|
|
input && input.focus()
|
2018-02-09 10:42:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-02-11 10:50:23 +00:00
|
|
|
|
selectionRefFocus (e) {
|
2018-02-26 10:44:06 +00:00
|
|
|
|
if (this._focused || this.disabled) {
|
2018-02-11 10:50:23 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this._focused = true
|
|
|
|
|
this.updateFocusClassName()
|
2018-06-21 14:27:48 +00:00
|
|
|
|
this.$emit('focus')
|
2018-02-11 10:50:23 +00:00
|
|
|
|
},
|
2018-02-26 10:44:06 +00:00
|
|
|
|
selectionRefBlur (e) {
|
|
|
|
|
this._focused = false
|
|
|
|
|
this.updateFocusClassName()
|
2018-06-21 14:27:48 +00:00
|
|
|
|
this.$emit('blur')
|
2018-02-26 10:44:06 +00:00
|
|
|
|
},
|
2018-02-07 10:56:58 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render () {
|
|
|
|
|
const props = this.$props
|
|
|
|
|
const multiple = isMultipleOrTags(props)
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const state = this.$data
|
|
|
|
|
const { disabled, prefixCls } = props
|
2018-11-09 06:23:19 +00:00
|
|
|
|
const inputIcon = getComponentFromProp(this, 'inputIcon')
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const ctrlNode = this.renderTopControlNode()
|
|
|
|
|
const { _open: open, _inputValue: inputValue, _value: value } = this.$data
|
|
|
|
|
if (open) {
|
|
|
|
|
this._options = this.renderFilterOptions()
|
|
|
|
|
}
|
|
|
|
|
const realOpen = this.getRealOpenState()
|
|
|
|
|
const options = this._options || []
|
|
|
|
|
const { $listeners } = this
|
2018-02-07 10:56:58 +00:00
|
|
|
|
const { mouseenter = noop, mouseleave = noop, popupScroll = noop } = $listeners
|
2018-02-11 10:04:31 +00:00
|
|
|
|
const selectionProps = {
|
|
|
|
|
props: {},
|
|
|
|
|
attrs: {
|
|
|
|
|
role: 'combobox',
|
|
|
|
|
'aria-autocomplete': 'list',
|
|
|
|
|
'aria-haspopup': 'true',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
'aria-expanded': realOpen,
|
2018-02-11 10:04:31 +00:00
|
|
|
|
},
|
|
|
|
|
on: {
|
|
|
|
|
click: this.selectionRefClick,
|
|
|
|
|
},
|
|
|
|
|
class: `${prefixCls}-selection ${prefixCls}-selection--${multiple ? 'multiple' : 'single'}`,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
directives: [{
|
2018-12-14 15:31:02 +00:00
|
|
|
|
name: 'ant-ref',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: this.saveSelectionRef,
|
|
|
|
|
}],
|
2018-02-11 10:04:31 +00:00
|
|
|
|
key: 'selection',
|
|
|
|
|
|
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
if (!isMultipleOrTagsOrCombobox(props)) {
|
2018-02-11 10:04:31 +00:00
|
|
|
|
selectionProps.on.keydown = this.onKeyDown
|
2018-02-11 10:50:23 +00:00
|
|
|
|
selectionProps.on.focus = this.selectionRefFocus
|
2018-02-26 10:44:06 +00:00
|
|
|
|
selectionProps.on.blur = this.selectionRefBlur
|
2018-02-11 10:04:31 +00:00
|
|
|
|
selectionProps.attrs.tabIndex = props.disabled ? -1 : 0
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
const rootCls = {
|
2018-02-26 10:44:06 +00:00
|
|
|
|
[prefixCls]: true,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
[`${prefixCls}-open`]: open,
|
|
|
|
|
[`${prefixCls}-focused`]: open || !!this._focused,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
[`${prefixCls}-combobox`]: isCombobox(props),
|
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
|
[`${prefixCls}-enabled`]: !disabled,
|
|
|
|
|
[`${prefixCls}-allow-clear`]: !!props.allowClear,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
[`${prefixCls}-no-arrow`]: !props.showArrow,
|
2018-02-07 10:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<SelectTrigger
|
|
|
|
|
dropdownAlign={props.dropdownAlign}
|
2018-02-28 11:07:04 +00:00
|
|
|
|
dropdownClassName={props.dropdownClassName}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
dropdownMatchSelectWidth={props.dropdownMatchSelectWidth}
|
|
|
|
|
defaultActiveFirstOption={props.defaultActiveFirstOption}
|
|
|
|
|
dropdownMenuStyle={props.dropdownMenuStyle}
|
|
|
|
|
transitionName={props.transitionName}
|
|
|
|
|
animation={props.animation}
|
|
|
|
|
prefixCls={props.prefixCls}
|
|
|
|
|
dropdownStyle={props.dropdownStyle}
|
|
|
|
|
combobox={props.combobox}
|
|
|
|
|
showSearch={props.showSearch}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
options={options}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
multiple={multiple}
|
|
|
|
|
disabled={disabled}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
visible={realOpen}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
inputValue={inputValue}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value={value}
|
|
|
|
|
backfillValue={state._backfillValue}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
firstActiveValue={props.firstActiveValue}
|
|
|
|
|
onDropdownVisibleChange={this.onDropdownVisibleChange}
|
|
|
|
|
getPopupContainer={props.getPopupContainer}
|
|
|
|
|
onMenuSelect={this.onMenuSelect}
|
|
|
|
|
onMenuDeselect={this.onMenuDeselect}
|
|
|
|
|
onPopupScroll={popupScroll}
|
|
|
|
|
onPopupFocus={this.onPopupFocus}
|
|
|
|
|
onMouseenter={mouseenter}
|
|
|
|
|
onMouseleave={mouseleave}
|
|
|
|
|
showAction={props.showAction}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
menuItemSelectedIcon={getComponentFromProp(this, 'menuItemSelectedIcon')}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{...{ directives: [{
|
2018-12-14 15:31:02 +00:00
|
|
|
|
name: 'ant-ref',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: this.saveSelectTriggerRef,
|
|
|
|
|
}] }}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
>
|
|
|
|
|
<div
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{...{ directives: [{
|
2018-12-14 15:31:02 +00:00
|
|
|
|
name: 'ant-ref',
|
2018-09-05 13:28:54 +00:00
|
|
|
|
value: this.saveRootRef,
|
|
|
|
|
}] }}
|
2018-05-06 12:37:39 +00:00
|
|
|
|
style={getStyle(this)}
|
2018-02-12 03:47:57 +00:00
|
|
|
|
class={classnames(rootCls)}
|
2018-11-09 06:23:19 +00:00
|
|
|
|
onMousedown={this.markMouseDown}
|
|
|
|
|
onMouseup={this.markMouseLeave}
|
|
|
|
|
onMouseout={this.markMouseLeave}
|
2018-02-12 03:47:57 +00:00
|
|
|
|
// tabindex='-1'
|
2018-02-08 10:56:58 +00:00
|
|
|
|
// onBlur={this.onOuterBlur}
|
|
|
|
|
// onFocus={this.onOuterFocus}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
>
|
2018-02-11 10:04:31 +00:00
|
|
|
|
<div {...selectionProps}>
|
2018-02-07 10:56:58 +00:00
|
|
|
|
{ctrlNode}
|
|
|
|
|
{this.renderClear()}
|
|
|
|
|
{multiple || !props.showArrow ? null : (
|
|
|
|
|
<span
|
|
|
|
|
key='arrow'
|
|
|
|
|
class={`${prefixCls}-arrow`}
|
|
|
|
|
style={UNSELECTABLE_STYLE}
|
2018-02-07 15:03:47 +00:00
|
|
|
|
unselectable='unselectable'
|
2018-02-09 10:42:19 +00:00
|
|
|
|
// onClick={this.onArrowClick}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
>
|
2018-11-09 06:23:19 +00:00
|
|
|
|
{inputIcon || <i class={`${prefixCls}-arrow-icon`} />}
|
2018-02-07 10:56:58 +00:00
|
|
|
|
</span>)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
export { Select }
|
|
|
|
|
export default proxyComponent(Select)
|