From fcb3f8c5f0fb5985a073e7955eb79b962bdd5d7b Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Mon, 12 Feb 2018 18:10:51 +0800 Subject: [PATCH] add select demo --- components/_util/props-util.js | 25 ++++- components/_util/vnode.js | 2 +- components/vc-select/OptGroup.vue | 4 + components/vc-select/Option.vue | 1 + components/vc-select/Select.vue | 94 +++++++++++-------- components/vc-select/demo/mul-tag-suggest.vue | 56 +++++++++++ .../vc-select/demo/multiple-readonly.vue | 58 ++++++++++++ components/vc-select/demo/multiple.vue | 74 +++++++++++++++ components/vc-select/demo/optgroup.vue | 43 +++++++++ .../vc-select/demo/optionFilterProp.vue | 31 ++++++ components/vc-select/demo/optionLabelProp.vue | 30 ++++++ components/vc-select/util.js | 16 +++- 12 files changed, 389 insertions(+), 45 deletions(-) create mode 100644 components/vc-select/demo/mul-tag-suggest.vue create mode 100644 components/vc-select/demo/multiple-readonly.vue create mode 100644 components/vc-select/demo/multiple.vue create mode 100644 components/vc-select/demo/optgroup.vue create mode 100644 components/vc-select/demo/optionFilterProp.vue create mode 100644 components/vc-select/demo/optionLabelProp.vue diff --git a/components/_util/props-util.js b/components/_util/props-util.js index ff3d48d40..ed40d579a 100644 --- a/components/_util/props-util.js +++ b/components/_util/props-util.js @@ -22,7 +22,7 @@ const getSlotOptions = (ele) => { if (ele.$vnode) { componentOptions = ele.$vnode.componentOptions } - return componentOptions.Ctor.options + return componentOptions ? componentOptions.Ctor.options || {} : {} } const getOptionProps = (instance) => { const { $options = {}, $props = {}} = instance @@ -43,8 +43,17 @@ const getPropsData = (ele) => { if (ele.$vnode) { componentOptions = ele.$vnode.componentOptions } - return componentOptions && componentOptions.propsData + return componentOptions ? componentOptions.propsData || {} : {} } + +const getAttrs = (ele) => { + let data = ele.data + if (ele.$vnode) { + data = ele.$vnode.data + } + return data ? data.attrs || {} : {} +} + const getKey = (ele) => { let key = ele.key if (ele.$vnode) { @@ -52,5 +61,15 @@ const getKey = (ele) => { } return key } -export { hasProp, filterProps, getOptionProps, getComponentFromProp, getSlotOptions, slotHasProp, getPropsData, getKey } +export { + hasProp, + filterProps, + getOptionProps, + getComponentFromProp, + getSlotOptions, + slotHasProp, + getPropsData, + getKey, + getAttrs, +} export default hasProp diff --git a/components/_util/vnode.js b/components/_util/vnode.js index c3619a14f..95b3de55f 100644 --- a/components/_util/vnode.js +++ b/components/_util/vnode.js @@ -128,7 +128,7 @@ export function getPropsData (ele) { return ele.componentOptions && ele.componentOptions.propsData } export function getValueByProp (ele, prop) { - return ele.componentOptions && ele.componentOptions.propsData[prop] + return ele.componentOptions && ele.componentOptions.propsData && ele.componentOptions.propsData[prop] } export function getEvents (child) { diff --git a/components/vc-select/OptGroup.vue b/components/vc-select/OptGroup.vue index 3d7d08eea..ca44b96a6 100644 --- a/components/vc-select/OptGroup.vue +++ b/components/vc-select/OptGroup.vue @@ -1,5 +1,9 @@ diff --git a/components/vc-select/Option.vue b/components/vc-select/Option.vue index d6f4d899a..c5ac2df0c 100644 --- a/components/vc-select/Option.vue +++ b/components/vc-select/Option.vue @@ -8,6 +8,7 @@ export default { PropTypes.number, ]), disabled: PropTypes.bool, + title: PropTypes.string, }, isSelectOption: true, } diff --git a/components/vc-select/Select.vue b/components/vc-select/Select.vue index be6f76c33..5f12dd53a 100644 --- a/components/vc-select/Select.vue +++ b/components/vc-select/Select.vue @@ -113,25 +113,32 @@ export default { }) }, watch: { - '$props': { - handler: function (nextProps) { - if (hasProp(this, 'value')) { - const { combobox, $slots } = this - let value = toArray(this.value) - value = this.addLabelToValue(value) - value = this.addTitleToValue($slots.default, value) - this.setState({ - sValue: value, - }) - if (combobox) { - this.setState({ - inputValue: value.length - ? this.getLabelFromProps(value[0].key) - : '', - }) - } - } - }, + // '$props': { + // handler: function (nextProps) { + // if (hasProp(this, 'value')) { + // console.log('nextProps', nextProps) + // const { combobox, $slots } = this + // let value = toArray(this.value) + // value = this.addLabelToValue(value) + // value = this.addTitleToValue($slots.default, value) + // this.setState({ + // sValue: value, + // }) + // if (combobox) { + // this.setState({ + // inputValue: value.length + // ? this.getLabelFromProps(value[0].key) + // : '', + // }) + // } + // } + // }, + // }, + value (val) { + this.updateState() + }, + combobox () { + this.updateState() }, }, updated () { @@ -148,10 +155,6 @@ export default { } }) }, - beforeUpdate () { - // console.log('beforeUpdate') - // this.adjustOpenState() - }, beforeDestroy () { this.clearFocusTime() this.clearBlurTime() @@ -163,6 +166,22 @@ export default { } }, methods: { + updateState () { + const { combobox, $slots } = this + let value = toArray(this.value) + value = this.addLabelToValue(value) + value = this.addTitleToValue($slots.default, value) + this.setState({ + sValue: value, + }) + if (combobox) { + this.setState({ + inputValue: value.length + ? this.getLabelFromProps(value[0].key) + : '', + }) + } + }, onInputChange (event) { const { tokenSeparators } = this const val = event.target.value @@ -437,7 +456,7 @@ export default { }, onChoiceAnimationLeave () { - this.$refs.selectTriggerRef.triggerRef.forcePopupAlign() + this.$refs.selectTriggerRef.$refs.triggerRef.forcePopupAlign() }, getOptionsFromChildren (value, children = [], options = []) { let values = value @@ -535,6 +554,7 @@ export default { }, getLabelFromOption (child) { + console.log(child, this.optionLabelProp) return getPropValue(child, this.optionLabelProp) }, @@ -635,14 +655,12 @@ export default { if (options.length) { const firstOption = findFirstMenuItem(options) if (firstOption) { - console.log('pre', this.sValue) sValue = [ { key: firstOption.key, label: this.getLabelFromOption(firstOption), }, ] - console.log('new', this.sValue, sValue) this.fireChange(sValue) } } @@ -833,6 +851,8 @@ export default { this.clearFocusTime() } this.focusTimer = setTimeout(() => { + this._focused = true + this.updateFocusClassName() this.__emit('focus') }, 10) }, @@ -911,7 +931,7 @@ export default { return } if (getSlotOptions(child).isSelectOptGroup) { - nextValues = this.addTitleToValue(child.$slots.default, nextValues) + nextValues = this.addTitleToValue(child.componentOptions.children, nextValues) } else { const value = getValuePropValue(child) const valueIndex = keys.indexOf(value) @@ -961,7 +981,7 @@ export default { this.__emit('select', labelInValue ? value : value.key, this.getSingleOptionByValueKey(value.key)) }, fireChange (value) { - if (hasProp(this, 'value')) { + if (!hasProp(this, 'value')) { this.setState({ sValue: value, }) @@ -975,7 +995,7 @@ export default { isChildDisabled (key) { return this.$slots.default.some(child => { const childValue = getValuePropValue(child) - return childValue === key && getValue(child, 'title') + return childValue === key && getValue(child, 'disabled') }) }, @@ -1304,6 +1324,7 @@ export default { } if (isMultipleOrTags(props)) { selectedValueNodes = limitedCountValue.map(singleValue => { + console.log('singleValue', singleValue) let content = singleValue.label const title = singleValue.title || content if ( @@ -1353,29 +1374,25 @@ export default { if (isMultipleOrTags(props) && choiceTransitionName) { const transitionProps = getTransitionProps(choiceTransitionName, { tag: 'ul', - // beforeEnter: this.onChoiceAnimationLeave, + afterLeave: this.onChoiceAnimationLeave, }) innerNode = ( {selectedValueNodes} ) } else { innerNode = ( -