2018-03-19 10:16:27 +08:00
|
|
|
|
|
2018-03-03 19:14:03 +08:00
|
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
|
import VcCascader from '../vc-cascader'
|
|
|
|
|
import arrayTreeFilter from 'array-tree-filter'
|
|
|
|
|
import classNames from 'classnames'
|
|
|
|
|
import omit from 'omit.js'
|
|
|
|
|
import KeyCode from '../_util/KeyCode'
|
|
|
|
|
import Input from '../input'
|
|
|
|
|
import Icon from '../icon'
|
2018-11-29 22:20:17 +08:00
|
|
|
|
import { hasProp, filterEmpty, getOptionProps, getStyle, getClass, getAttrs, getComponentFromProp, isValidElement } from '../_util/props-util'
|
2018-03-03 19:14:03 +08:00
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-11-29 22:20:17 +08:00
|
|
|
|
import { cloneElement } from '../_util/vnode'
|
2018-03-03 19:14:03 +08:00
|
|
|
|
|
|
|
|
|
const CascaderOptionType = PropTypes.shape({
|
2018-09-05 21:28:54 +08:00
|
|
|
|
value: PropTypes.string,
|
|
|
|
|
label: PropTypes.any,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
children: PropTypes.array,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
key: PropTypes.string,
|
|
|
|
|
}).loose
|
|
|
|
|
|
|
|
|
|
const FieldNamesType = PropTypes.shape({
|
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
|
children: PropTypes.string,
|
|
|
|
|
}).loose
|
|
|
|
|
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const CascaderExpandTrigger = PropTypes.oneOf(['click', 'hover'])
|
|
|
|
|
|
|
|
|
|
const ShowSearchType = PropTypes.shape({
|
|
|
|
|
filter: PropTypes.func,
|
|
|
|
|
render: PropTypes.func,
|
|
|
|
|
sort: PropTypes.func,
|
|
|
|
|
matchInputWidth: PropTypes.bool,
|
|
|
|
|
}).loose
|
|
|
|
|
function noop () {}
|
|
|
|
|
|
|
|
|
|
const CascaderProps = {
|
|
|
|
|
/** 可选项数据源 */
|
|
|
|
|
options: PropTypes.arrayOf(CascaderOptionType).def([]),
|
|
|
|
|
/** 默认的选中项 */
|
|
|
|
|
defaultValue: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
|
/** 指定选中项 */
|
|
|
|
|
value: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
|
/** 选择完成后的回调 */
|
|
|
|
|
// onChange?: (value: string[], selectedOptions?: CascaderOptionType[]) => void;
|
|
|
|
|
/** 选择后展示的渲染函数 */
|
|
|
|
|
displayRender: PropTypes.func,
|
|
|
|
|
transitionName: PropTypes.string.def('slide-up'),
|
|
|
|
|
popupStyle: PropTypes.object.def({}),
|
|
|
|
|
/** 自定义浮层类名 */
|
|
|
|
|
popupClassName: PropTypes.string,
|
|
|
|
|
/** 浮层预设位置:`bottomLeft` `bottomRight` `topLeft` `topRight` */
|
|
|
|
|
popupPlacement: PropTypes.oneOf(['bottomLeft', 'bottomRight', 'topLeft', 'topRight']).def('bottomLeft'),
|
|
|
|
|
/** 输入框占位文本*/
|
|
|
|
|
placeholder: PropTypes.string.def('Please select'),
|
|
|
|
|
/** 输入框大小,可选 `large` `default` `small` */
|
|
|
|
|
size: PropTypes.oneOf(['large', 'default', 'small']),
|
|
|
|
|
/** 禁用*/
|
|
|
|
|
disabled: PropTypes.bool.def(false),
|
|
|
|
|
/** 是否支持清除*/
|
|
|
|
|
allowClear: PropTypes.bool.def(true),
|
2018-06-07 14:40:10 +08:00
|
|
|
|
showSearch: PropTypes.oneOfType([Boolean, ShowSearchType]),
|
2018-03-03 19:14:03 +08:00
|
|
|
|
notFoundContent: PropTypes.any.def('Not Found'),
|
|
|
|
|
loadData: PropTypes.func,
|
|
|
|
|
/** 次级菜单的展开方式,可选 'click' 和 'hover' */
|
|
|
|
|
expandTrigger: CascaderExpandTrigger,
|
|
|
|
|
/** 当此项为 true 时,点选每级菜单选项值都会发生变化 */
|
|
|
|
|
changeOnSelect: PropTypes.bool,
|
|
|
|
|
/** 浮层可见变化时回调 */
|
|
|
|
|
// onPopupVisibleChange?: (popupVisible: boolean) => void;
|
|
|
|
|
prefixCls: PropTypes.string.def('ant-cascader'),
|
|
|
|
|
inputPrefixCls: PropTypes.string.def('ant-input'),
|
|
|
|
|
getPopupContainer: PropTypes.func,
|
|
|
|
|
popupVisible: PropTypes.bool,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
fieldNames: FieldNamesType,
|
2018-06-07 14:40:10 +08:00
|
|
|
|
autoFocus: PropTypes.bool,
|
2018-11-29 22:20:17 +08:00
|
|
|
|
suffixIcon: PropTypes.any,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
function defaultFilterOption (inputValue, path, names) {
|
|
|
|
|
return path.some(option => option[names.label].indexOf(inputValue) > -1)
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
function defaultSortFilteredOption (a, b, inputValue, names) {
|
2018-03-03 19:14:03 +08:00
|
|
|
|
function callback (elem) {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
return elem[names.label].indexOf(inputValue) > -1
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a.findIndex(callback) - b.findIndex(callback)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 22:20:17 +08:00
|
|
|
|
function getFilledFieldNames ({ fieldNames = {}}) {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const names = {
|
|
|
|
|
children: fieldNames.children || 'children',
|
|
|
|
|
label: fieldNames.label || 'label',
|
|
|
|
|
value: fieldNames.value || 'value',
|
|
|
|
|
}
|
|
|
|
|
return names
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-04 16:02:46 +08:00
|
|
|
|
const defaultDisplayRender = ({ labels }) => labels.join(' / ')
|
2018-03-03 19:14:03 +08:00
|
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
|
const Cascader = {
|
2018-05-06 20:37:39 +08:00
|
|
|
|
inheritAttrs: false,
|
2018-04-08 21:17:20 +08:00
|
|
|
|
name: 'ACascader',
|
2018-03-03 19:14:03 +08:00
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
|
props: CascaderProps,
|
2018-03-04 16:02:46 +08:00
|
|
|
|
model: {
|
|
|
|
|
prop: 'value',
|
|
|
|
|
event: 'change',
|
|
|
|
|
},
|
2018-03-03 19:14:03 +08:00
|
|
|
|
data () {
|
|
|
|
|
this.cachedOptions = []
|
2018-11-29 22:20:17 +08:00
|
|
|
|
const { value, defaultValue, popupVisible, showSearch, options, flattenTree } = this
|
2018-03-03 19:14:03 +08:00
|
|
|
|
return {
|
|
|
|
|
sValue: value || defaultValue || [],
|
|
|
|
|
inputValue: '',
|
|
|
|
|
inputFocused: false,
|
|
|
|
|
sPopupVisible: popupVisible,
|
2018-11-29 22:20:17 +08:00
|
|
|
|
flattenOptions: showSearch ? flattenTree(options, this.$props) : undefined,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-06-07 14:40:10 +08:00
|
|
|
|
mounted () {
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
if (this.autoFocus && !this.showSearch && !this.disabled) {
|
|
|
|
|
this.$refs.picker.focus()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
2018-03-03 19:14:03 +08:00
|
|
|
|
watch: {
|
|
|
|
|
value (val) {
|
|
|
|
|
this.setState({ sValue: val || [] })
|
|
|
|
|
},
|
|
|
|
|
popupVisible (val) {
|
|
|
|
|
this.setState({ sPopupVisible: val })
|
|
|
|
|
},
|
|
|
|
|
options (val) {
|
|
|
|
|
if (this.showSearch) {
|
2018-11-29 22:20:17 +08:00
|
|
|
|
this.setState({ flattenOptions: this.flattenTree(this.options, this.$props) })
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
highlightKeyword (str, keyword, prefixCls) {
|
|
|
|
|
return str.split(keyword)
|
|
|
|
|
.map((node, index) => index === 0 ? node : [
|
2018-06-07 14:40:10 +08:00
|
|
|
|
<span class={`${prefixCls}-menu-item-keyword`}>{keyword}</span>,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
node,
|
|
|
|
|
])
|
|
|
|
|
},
|
|
|
|
|
|
2018-09-05 21:28:54 +08:00
|
|
|
|
defaultRenderFilteredOption ({ inputValue, path, prefixCls, names }) {
|
|
|
|
|
return path.map((option, index) => {
|
|
|
|
|
const label = option[names.label]
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const node = label.indexOf(inputValue) > -1
|
|
|
|
|
? this.highlightKeyword(label, inputValue, prefixCls) : label
|
|
|
|
|
return index === 0 ? node : [' / ', node]
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
handleChange (value, selectedOptions) {
|
|
|
|
|
this.setState({ inputValue: '' })
|
|
|
|
|
if (selectedOptions[0].__IS_FILTERED_OPTION) {
|
|
|
|
|
const unwrappedValue = value[0]
|
|
|
|
|
const unwrappedSelectedOptions = selectedOptions[0].path
|
|
|
|
|
this.setValue(unwrappedValue, unwrappedSelectedOptions)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.setValue(value, selectedOptions)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handlePopupVisibleChange (popupVisible) {
|
|
|
|
|
if (!hasProp(this, 'popupVisible')) {
|
|
|
|
|
this.setState({
|
|
|
|
|
sPopupVisible: popupVisible,
|
|
|
|
|
inputFocused: popupVisible,
|
|
|
|
|
inputValue: popupVisible ? this.inputValue : '',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
this.$emit('popupVisibleChange', popupVisible)
|
|
|
|
|
},
|
2018-06-07 14:40:10 +08:00
|
|
|
|
handleInputFocus (e) {
|
|
|
|
|
this.$emit('focus', e)
|
|
|
|
|
},
|
2018-03-03 19:14:03 +08:00
|
|
|
|
|
2018-06-07 14:40:10 +08:00
|
|
|
|
handleInputBlur (e) {
|
2018-03-03 19:14:03 +08:00
|
|
|
|
this.setState({
|
|
|
|
|
inputFocused: false,
|
|
|
|
|
})
|
2018-06-07 14:40:10 +08:00
|
|
|
|
this.$emit('blur', e)
|
2018-03-03 19:14:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleInputClick (e) {
|
|
|
|
|
const { inputFocused, sPopupVisible } = this
|
|
|
|
|
// Prevent `Trigger` behaviour.
|
|
|
|
|
if (inputFocused || sPopupVisible) {
|
|
|
|
|
e.stopPropagation()
|
2018-09-05 21:28:54 +08:00
|
|
|
|
if (e.nativeEvent && e.nativeEvent.stopImmediatePropagation) {
|
|
|
|
|
e.nativeEvent.stopImmediatePropagation()
|
|
|
|
|
}
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleKeyDown (e) {
|
|
|
|
|
if (e.keyCode === KeyCode.BACKSPACE) {
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleInputChange (e) {
|
|
|
|
|
const inputValue = e.target.value
|
|
|
|
|
this.setState({ inputValue })
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setValue (value, selectedOptions) {
|
|
|
|
|
if (!hasProp(this, 'value')) {
|
|
|
|
|
this.setState({ sValue: value })
|
|
|
|
|
}
|
|
|
|
|
this.$emit('change', value, selectedOptions)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getLabel () {
|
2018-11-29 22:20:17 +08:00
|
|
|
|
const { options, $scopedSlots } = this
|
|
|
|
|
const names = getFilledFieldNames(this.$props)
|
2018-03-04 16:02:46 +08:00
|
|
|
|
const displayRender = this.displayRender || $scopedSlots.displayRender || defaultDisplayRender
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const value = this.sValue
|
|
|
|
|
const unwrappedValue = Array.isArray(value[0]) ? value[0] : value
|
|
|
|
|
const selectedOptions = arrayTreeFilter(options,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
(o, level) => o[names.value] === unwrappedValue[level],
|
|
|
|
|
{ childrenKeyName: names.children },
|
2018-03-03 19:14:03 +08:00
|
|
|
|
)
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const labels = selectedOptions.map(o => o[names.label])
|
2018-03-04 16:02:46 +08:00
|
|
|
|
return displayRender({ labels, selectedOptions })
|
2018-03-03 19:14:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearSelection (e) {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
if (!this.inputValue) {
|
|
|
|
|
this.setValue([])
|
|
|
|
|
this.handlePopupVisibleChange(false)
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({ inputValue: '' })
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-11-29 22:20:17 +08:00
|
|
|
|
flattenTree (options, props, ancestor = []) {
|
|
|
|
|
const names = getFilledFieldNames(props)
|
2018-03-03 19:14:03 +08:00
|
|
|
|
let flattenOptions = []
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const childrenName = names.children
|
2018-03-03 19:14:03 +08:00
|
|
|
|
options.forEach((option) => {
|
|
|
|
|
const path = ancestor.concat(option)
|
2018-11-29 22:20:17 +08:00
|
|
|
|
if (props.changeOnSelect || !option[childrenName] || !option[childrenName].length) {
|
2018-03-03 19:14:03 +08:00
|
|
|
|
flattenOptions.push(path)
|
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
if (option[childrenName]) {
|
|
|
|
|
flattenOptions = flattenOptions.concat(
|
2018-11-29 22:20:17 +08:00
|
|
|
|
this.flattenTree(option[childrenName], props, path)
|
2018-09-05 21:28:54 +08:00
|
|
|
|
)
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return flattenOptions
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
generateFilteredOptions (prefixCls) {
|
2018-11-29 22:20:17 +08:00
|
|
|
|
const { showSearch, notFoundContent, $scopedSlots } = this
|
|
|
|
|
const names = getFilledFieldNames(this.$props)
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const {
|
|
|
|
|
filter = defaultFilterOption,
|
2018-03-04 17:11:32 +08:00
|
|
|
|
// render = this.defaultRenderFilteredOption,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
sort = defaultSortFilteredOption,
|
|
|
|
|
} = showSearch
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const { flattenOptions = [], inputValue } = this.$data
|
2018-03-04 17:11:32 +08:00
|
|
|
|
const render = showSearch.render || $scopedSlots.showSearchRender || this.defaultRenderFilteredOption
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const filtered = flattenOptions.filter((path) => filter(inputValue, path, names))
|
|
|
|
|
.sort((a, b) => sort(a, b, inputValue, names))
|
2018-03-03 19:14:03 +08:00
|
|
|
|
|
|
|
|
|
if (filtered.length > 0) {
|
|
|
|
|
return filtered.map((path) => {
|
|
|
|
|
return {
|
|
|
|
|
__IS_FILTERED_OPTION: true,
|
|
|
|
|
path,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
[names.label]: render({ inputValue, path, prefixCls, names }),
|
|
|
|
|
[names.value]: path.map((o) => o[names.value]),
|
|
|
|
|
disabled: path.some((o) => !!o.disabled),
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
return [{ [names.label]: notFoundContent, [names.value]: 'ANT_CASCADER_NOT_FOUND', disabled: true }]
|
2018-03-03 19:14:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
focus () {
|
2018-06-07 14:40:10 +08:00
|
|
|
|
if (this.showSearch) {
|
|
|
|
|
this.$refs.input.focus()
|
|
|
|
|
} else {
|
|
|
|
|
this.$refs.picker.focus()
|
|
|
|
|
}
|
2018-03-03 19:14:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
blur () {
|
2018-06-07 14:40:10 +08:00
|
|
|
|
if (this.showSearch) {
|
|
|
|
|
this.$refs.input.blur()
|
|
|
|
|
} else {
|
|
|
|
|
this.$refs.picker.blur()
|
|
|
|
|
}
|
2018-03-03 19:14:03 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render () {
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const { $slots, sPopupVisible, inputValue, $listeners } = this
|
|
|
|
|
const { sValue: value, inputFocused } = this.$data
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const props = getOptionProps(this)
|
2018-12-09 17:34:27 +08:00
|
|
|
|
let suffixIcon = getComponentFromProp(this, 'suffixIcon')
|
|
|
|
|
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const {
|
|
|
|
|
prefixCls, inputPrefixCls, placeholder, size, disabled,
|
|
|
|
|
allowClear, showSearch = false, ...otherProps } = props
|
|
|
|
|
|
|
|
|
|
const sizeCls = classNames({
|
|
|
|
|
[`${inputPrefixCls}-lg`]: size === 'large',
|
|
|
|
|
[`${inputPrefixCls}-sm`]: size === 'small',
|
|
|
|
|
})
|
|
|
|
|
const clearIcon = (allowClear && !disabled && value.length > 0) || inputValue ? (
|
|
|
|
|
<Icon
|
2018-11-29 22:20:17 +08:00
|
|
|
|
type='close-circle'
|
|
|
|
|
theme='filled'
|
2018-03-03 19:14:03 +08:00
|
|
|
|
class={`${prefixCls}-picker-clear`}
|
|
|
|
|
onClick={this.clearSelection}
|
2018-03-05 19:05:23 +08:00
|
|
|
|
key='clear-icon'
|
2018-03-03 19:14:03 +08:00
|
|
|
|
/>
|
|
|
|
|
) : null
|
|
|
|
|
const arrowCls = classNames({
|
|
|
|
|
[`${prefixCls}-picker-arrow`]: true,
|
|
|
|
|
[`${prefixCls}-picker-arrow-expand`]: sPopupVisible,
|
|
|
|
|
})
|
|
|
|
|
const pickerCls = classNames(
|
2018-05-06 20:37:39 +08:00
|
|
|
|
getClass(this),
|
2018-03-03 19:14:03 +08:00
|
|
|
|
`${prefixCls}-picker`, {
|
|
|
|
|
[`${prefixCls}-picker-with-value`]: inputValue,
|
|
|
|
|
[`${prefixCls}-picker-disabled`]: disabled,
|
|
|
|
|
[`${prefixCls}-picker-${size}`]: !!size,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
[`${prefixCls}-picker-show-search`]: !!showSearch,
|
|
|
|
|
[`${prefixCls}-picker-focused`]: inputFocused,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Fix bug of https://github.com/facebook/react/pull/5004
|
|
|
|
|
// and https://fb.me/react-unknown-prop
|
|
|
|
|
const tempInputProps = omit(otherProps, [
|
|
|
|
|
'options',
|
|
|
|
|
'popupPlacement',
|
|
|
|
|
'transitionName',
|
|
|
|
|
'displayRender',
|
|
|
|
|
'changeOnSelect',
|
|
|
|
|
'expandTrigger',
|
|
|
|
|
'popupVisible',
|
|
|
|
|
'getPopupContainer',
|
|
|
|
|
'loadData',
|
|
|
|
|
'popupClassName',
|
|
|
|
|
'filterOption',
|
|
|
|
|
'renderFilteredOption',
|
|
|
|
|
'sortFilteredOption',
|
|
|
|
|
'notFoundContent',
|
2018-03-04 16:02:46 +08:00
|
|
|
|
'defaultValue',
|
2018-09-05 21:28:54 +08:00
|
|
|
|
'fieldNames',
|
2018-03-03 19:14:03 +08:00
|
|
|
|
])
|
|
|
|
|
|
2018-09-17 21:17:44 +08:00
|
|
|
|
let options = props.options
|
2018-03-03 19:14:03 +08:00
|
|
|
|
if (inputValue) {
|
|
|
|
|
options = this.generateFilteredOptions(prefixCls)
|
|
|
|
|
}
|
|
|
|
|
// Dropdown menu should keep previous status until it is fully closed.
|
|
|
|
|
if (!sPopupVisible) {
|
|
|
|
|
options = this.cachedOptions
|
|
|
|
|
} else {
|
|
|
|
|
this.cachedOptions = options
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dropdownMenuColumnStyle = {}
|
|
|
|
|
const isNotFound = (options || []).length === 1 && options[0].value === 'ANT_CASCADER_NOT_FOUND'
|
|
|
|
|
if (isNotFound) {
|
|
|
|
|
dropdownMenuColumnStyle.height = 'auto' // Height of one row.
|
|
|
|
|
}
|
|
|
|
|
// The default value of `matchInputWidth` is `true`
|
|
|
|
|
const resultListMatchInputWidth = showSearch.matchInputWidth !== false
|
|
|
|
|
if (resultListMatchInputWidth && inputValue && this.input) {
|
|
|
|
|
dropdownMenuColumnStyle.width = this.input.input.offsetWidth
|
|
|
|
|
}
|
2018-06-07 14:40:10 +08:00
|
|
|
|
// showSearch时,focus、blur在input上触发,反之在ref='picker'上触发
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const inputProps = {
|
|
|
|
|
props: {
|
|
|
|
|
...tempInputProps,
|
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
|
placeholder: value && value.length > 0 ? undefined : placeholder,
|
|
|
|
|
value: inputValue,
|
|
|
|
|
disabled: disabled,
|
|
|
|
|
readOnly: !showSearch,
|
|
|
|
|
autoComplete: 'off',
|
|
|
|
|
},
|
|
|
|
|
class: `${prefixCls}-input ${sizeCls}`,
|
|
|
|
|
ref: 'input',
|
|
|
|
|
on: {
|
2018-06-07 14:40:10 +08:00
|
|
|
|
focus: showSearch ? this.handleInputFocus : noop,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
click: showSearch ? this.handleInputClick : noop,
|
|
|
|
|
blur: showSearch ? this.handleInputBlur : noop,
|
|
|
|
|
keydown: this.handleKeyDown,
|
|
|
|
|
change: showSearch ? this.handleInputChange : noop,
|
|
|
|
|
},
|
2018-05-06 20:37:39 +08:00
|
|
|
|
attrs: getAttrs(this),
|
2018-03-03 19:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
const children = filterEmpty($slots.default)
|
2018-11-29 22:20:17 +08:00
|
|
|
|
const inputIcon = suffixIcon && (
|
2018-12-09 17:34:27 +08:00
|
|
|
|
isValidElement(suffixIcon)
|
2018-11-29 22:20:17 +08:00
|
|
|
|
? cloneElement(
|
2018-12-09 17:34:27 +08:00
|
|
|
|
suffixIcon,
|
2018-11-29 22:20:17 +08:00
|
|
|
|
{
|
|
|
|
|
class: {
|
|
|
|
|
[`${prefixCls}-picker-arrow`]: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
) : <span class={`${prefixCls}-picker-arrow`}>{suffixIcon}</span>) || (
|
|
|
|
|
<Icon type='down' class={arrowCls} />
|
|
|
|
|
)
|
|
|
|
|
|
2018-03-03 19:14:03 +08:00
|
|
|
|
const input = children.length ? children : (
|
|
|
|
|
<span
|
|
|
|
|
class={pickerCls}
|
2018-05-06 20:37:39 +08:00
|
|
|
|
style={getStyle(this)}
|
2018-06-07 14:40:10 +08:00
|
|
|
|
ref='picker'
|
2018-03-03 19:14:03 +08:00
|
|
|
|
>
|
2018-03-04 16:02:46 +08:00
|
|
|
|
{ showSearch ? <span class={`${prefixCls}-picker-label`}>
|
2018-03-03 19:14:03 +08:00
|
|
|
|
{this.getLabel()}
|
2018-03-04 16:02:46 +08:00
|
|
|
|
</span> : null}
|
2018-03-03 19:14:03 +08:00
|
|
|
|
<Input {...inputProps}/>
|
2018-03-04 16:02:46 +08:00
|
|
|
|
{ !showSearch ? <span class={`${prefixCls}-picker-label`}>
|
|
|
|
|
{this.getLabel()}
|
|
|
|
|
</span> : null}
|
2018-03-03 19:14:03 +08:00
|
|
|
|
{clearIcon}
|
2018-11-29 22:20:17 +08:00
|
|
|
|
{inputIcon}
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const expandIcon = (
|
|
|
|
|
<Icon type='right' />
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const loadingIcon = (
|
|
|
|
|
<span class={`${prefixCls}-menu-item-loading-icon`}>
|
|
|
|
|
<Icon type='redo' spin />
|
2018-03-03 19:14:03 +08:00
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
const cascaderProps = {
|
|
|
|
|
props: {
|
|
|
|
|
...props,
|
|
|
|
|
options: options,
|
|
|
|
|
value: value,
|
|
|
|
|
popupVisible: sPopupVisible,
|
|
|
|
|
dropdownMenuColumnStyle: dropdownMenuColumnStyle,
|
2018-11-29 22:20:17 +08:00
|
|
|
|
expandIcon,
|
|
|
|
|
loadingIcon,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
},
|
|
|
|
|
on: {
|
2018-06-07 14:40:10 +08:00
|
|
|
|
...$listeners,
|
2018-03-03 19:14:03 +08:00
|
|
|
|
popupVisibleChange: this.handlePopupVisibleChange,
|
|
|
|
|
change: this.handleChange,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<VcCascader {...cascaderProps}>
|
|
|
|
|
{input}
|
|
|
|
|
</VcCascader>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
|
/* istanbul ignore next */
|
|
|
|
|
Cascader.install = function (Vue) {
|
|
|
|
|
Vue.component(Cascader.name, Cascader)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Cascader
|
|
|
|
|
|