[Improvement] optimize find parent mixin (#781)

This commit is contained in:
neverland 2018-03-26 21:20:00 +08:00 committed by GitHub
parent dadf733d71
commit 5497eef5ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 82 additions and 105 deletions

View File

@ -1,11 +1,11 @@
<template> <template>
<transition name="van-actionsheet-float"> <transition name="van-slide-bottom">
<div class="van-actionsheet" :class="{ 'van-actionsheet--withtitle': title }" v-show="value"> <div class="van-actionsheet" :class="{ 'van-actionsheet--withtitle': title }" v-show="value">
<div class="van-actionsheet__header van-hairline--top-bottom" v-if="title"> <div class="van-actionsheet__header van-hairline--top-bottom" v-if="title">
<div v-text="title" /> <div v-text="title" />
<icon name="close" @click.stop="$emit('input', false)" /> <icon name="close" @click="$emit('input', false)" />
</div> </div>
<ul v-if="!title" class="van-actionsheet__list van-hairline--bottom"> <ul v-else class="van-hairline--bottom">
<li <li
v-for="(item, index) in actions" v-for="(item, index) in actions"
:key="index" :key="index"
@ -24,7 +24,7 @@
v-if="cancelText" v-if="cancelText"
v-text="cancelText" v-text="cancelText"
class="van-actionsheet__item van-actionsheet__cancel van-hairline--top" class="van-actionsheet__item van-actionsheet__cancel van-hairline--top"
@click.stop="$emit('input', false)" @click="$emit('input', false)"
/> />
<div v-else class="van-actionsheet__content"> <div v-else class="van-actionsheet__content">
<slot /> <slot />

View File

@ -40,44 +40,33 @@ export default create({
} }
}, },
watch: {
value(val) {
this.$emit('change', val);
}
},
data() {
this.findParentByName('van-checkbox-group');
return {};
},
computed: { computed: {
currentValue: { currentValue: {
get() { get() {
return this.parentGroup return this.parent
? this.parentGroup.value.indexOf(this.name) !== -1 ? this.parent.value.indexOf(this.name) !== -1
: this.value; : this.value;
}, },
set(val) { set(val) {
const { parentGroup } = this; const { parent } = this;
if (parentGroup) { if (parent) {
const parentValue = this.parentGroup.value.slice(); const parentValue = this.parent.value.slice();
if (val) { if (val) {
if (parentGroup.max && parentValue.length >= parentGroup.max) { if (parent.max && parentValue.length >= parent.max) {
return; return;
} }
/* istanbul ignore else */ /* istanbul ignore else */
if (parentValue.indexOf(this.name) === -1) { if (parentValue.indexOf(this.name) === -1) {
parentValue.push(this.name); parentValue.push(this.name);
parentGroup.$emit('input', parentValue); parent.$emit('input', parentValue);
} }
} else { } else {
const index = parentValue.indexOf(this.name); const index = parentValue.indexOf(this.name);
/* istanbul ignore else */ /* istanbul ignore else */
if (index !== -1) { if (index !== -1) {
parentValue.splice(index, 1); parentValue.splice(index, 1);
parentGroup.$emit('input', parentValue); parent.$emit('input', parentValue);
} }
} }
} else { } else {
@ -96,10 +85,20 @@ export default create({
}, },
isDisabled() { isDisabled() {
return (this.parentGroup && this.parentGroup.disabled) || this.disabled; return (this.parent && this.parent.disabled) || this.disabled;
} }
}, },
watch: {
value(val) {
this.$emit('change', val);
}
},
created() {
this.findParent('van-checkbox-group');
},
methods: { methods: {
onClick(target) { onClick(target) {
if (!this.isDisabled && !(target === 'label' && this.labelDisabled)) { if (!this.isDisabled && !(target === 'label' && this.labelDisabled)) {

View File

@ -32,7 +32,7 @@ export default create({
computed: { computed: {
items() { items() {
return this.parentGroup.items; return this.parent.items;
}, },
index() { index() {
@ -44,15 +44,15 @@ export default create({
}, },
expanded() { expanded() {
const { activeNames } = this.parentGroup; const { value } = this.parent;
return this.parentGroup.accordion return this.parent.accordion
? activeNames === this.currentName ? value === this.currentName
: activeNames.some(name => name === this.currentName); : value.some(name => name === this.currentName);
} }
}, },
created() { created() {
this.findParentByName('van-collapse'); this.findParent('van-collapse');
this.items.push(this); this.items.push(this);
}, },
@ -62,9 +62,9 @@ export default create({
methods: { methods: {
onClick() { onClick() {
const { parentGroup } = this; const { parent } = this;
const name = parentGroup.accordion && this.currentName === parentGroup.activeNames ? '' : this.currentName; const name = parent.accordion && this.currentName === parent.value ? '' : this.currentName;
this.parentGroup.switch(name, !this.expanded); this.parent.switch(name, !this.expanded);
} }
} }
}); });

View File

@ -10,13 +10,9 @@ import create from '../utils/create';
export default create({ export default create({
name: 'collapse', name: 'collapse',
model: {
prop: 'activeNames'
},
props: { props: {
accordion: Boolean, accordion: Boolean,
activeNames: [String, Number, Array] value: [String, Number, Array]
}, },
data() { data() {
@ -27,11 +23,10 @@ export default create({
methods: { methods: {
switch(name, expanded) { switch(name, expanded) {
const { activeNames } = this;
if (!this.accordion) { if (!this.accordion) {
name = expanded name = expanded
? activeNames.concat(name) ? this.value.concat(name)
: activeNames.filter(activeName => activeName !== name); : this.value.filter(activeName => activeName !== name);
} }
this.$emit('change', name); this.$emit('change', name);
this.$emit('input', name); this.$emit('input', name);

View File

@ -19,8 +19,8 @@
/> />
</cell-group> </cell-group>
<div class="van-contact-edit__buttons"> <div class="van-contact-edit__buttons">
<van-button block :loading="isSaving" @click="onSaveContact" type="primary">{{ $t('save') }}</van-button> <van-button block :loading="isSaving" @click="onSave" type="primary">{{ $t('save') }}</van-button>
<van-button block :loading="isDeleting" @click="onDeleteContact" v-if="isEdit">{{ $t('delete') }}</van-button> <van-button block :loading="isDeleting" @click="onDelete" v-if="isEdit">{{ $t('delete') }}</van-button>
</div> </div>
</div> </div>
</template> </template>
@ -90,13 +90,8 @@ export default create({
} }
}, },
onSaveContact() { onSave() {
const items = [ const isValid = ['name', 'tel'].every(item => {
'name',
'tel'
];
const isValid = items.every(item => {
const msg = this.getErrorMessageByKey(item); const msg = this.getErrorMessageByKey(item);
if (msg) { if (msg) {
this.errorInfo[item] = true; this.errorInfo[item] = true;
@ -110,11 +105,8 @@ export default create({
} }
}, },
onDeleteContact() { onDelete() {
if (this.isDeleting) { if (!this.isDeleting) {
return;
}
Dialog.confirm({ Dialog.confirm({
message: this.$t('confirmDelete') message: this.$t('confirmDelete')
}).then(() => { }).then(() => {
@ -122,5 +114,6 @@ export default create({
}); });
} }
} }
}
}); });
</script> </script>

View File

@ -146,8 +146,10 @@ export default create({
} }
} }
if (height) {
el.style.height = height + 'px'; el.style.height = height + 'px';
} }
} }
}
}); });
</script> </script>

View File

@ -3,20 +3,22 @@
*/ */
export default { export default {
data() {
return {
parent: null
};
},
methods: { methods: {
findParentByName(name) { findParent(name) {
if (!this.parentGroup) {
let parent = this.$parent; let parent = this.$parent;
while (parent) { while (parent) {
if (parent.$options.name === name) { if (parent.$options.name === name) {
this.parentGroup = parent; this.parent = parent;
break; break;
} }
parent = parent.$parent; parent = parent.$parent;
} }
} }
return this.parentGroup;
}
} }
}; };

View File

@ -36,40 +36,33 @@ export default create({
}, },
computed: { computed: {
isGroup() {
return !!this.findParentByName('van-radio-group');
},
currentValue: { currentValue: {
get() { get() {
return this.isGroup && this.parentGroup return this.parent ? this.parent.value : this.value;
? this.parentGroup.value
: this.value;
}, },
set(val) { set(val) {
if (this.isGroup && this.parentGroup) { (this.parent || this).$emit('input', val);
this.parentGroup.$emit('input', val);
} else {
this.$emit('input', val);
}
} }
}, },
isDisabled() { isDisabled() {
return this.isGroup && this.parentGroup return this.parent
? this.parentGroup.disabled || this.disabled ? this.parent.disabled || this.disabled
: this.disabled; : this.disabled;
} }
}, },
created() {
this.findParent('van-radio-group');
},
methods: { methods: {
onClickLabel() { onClickLabel() {
if (this.isDisabled) { if (!this.isDisabled) {
return;
}
this.currentValue = this.name; this.currentValue = this.name;
} }
} }
}
}); });
</script> </script>

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="van-tab__pane" :class="{ 'van-tab__pane--select': index === parentGroup.curActive }"> <div class="van-tab__pane" :class="{ 'van-tab__pane--select': index === parent.curActive }">
<slot /> <slot />
</div> </div>
</template> </template>
@ -20,17 +20,17 @@ export default create({
computed: { computed: {
index() { index() {
return this.parentGroup.tabs.indexOf(this); return this.parent.tabs.indexOf(this);
} }
}, },
created() { created() {
this.findParentByName('van-tabs'); this.findParent('van-tabs');
this.parentGroup.tabs.push(this); this.parent.tabs.push(this);
}, },
destroyed() { destroyed() {
this.parentGroup.tabs.splice(this.index, 1); this.parent.tabs.splice(this.index, 1);
} }
}); });
</script> </script>

View File

@ -6,11 +6,10 @@
right: 0; right: 0;
bottom: 0; bottom: 0;
color: $text-color; color: $text-color;
transition: .2s ease-out;
background-color: $background-color;
max-height: 90%; max-height: 90%;
overflow-y: auto; overflow-y: auto;
-webkit-overflow-scrolling: touch; -webkit-overflow-scrolling: touch;
background-color: $background-color;
&--withtitle { &--withtitle {
background-color: $white; background-color: $white;
@ -56,9 +55,4 @@
line-height: inherit; line-height: inherit;
} }
} }
&-float-enter,
&-float-leave-active {
transform: translate3d(0, 100%, 0);
}
} }

View File

@ -14,7 +14,6 @@ describe('ActionSheet', () => {
}); });
expect(wrapper.hasClass('van-actionsheet')).to.be.true; expect(wrapper.hasClass('van-actionsheet')).to.be.true;
expect(wrapper.contains('.van-actionsheet__list')).to.be.true;
expect(wrapper.instance().actions.length).to.equal(0); expect(wrapper.instance().actions.length).to.equal(0);
expect(wrapper.instance().overlay).to.be.true; expect(wrapper.instance().overlay).to.be.true;
expect(wrapper.instance().closeOnClickOverlay).to.be.true; expect(wrapper.instance().closeOnClickOverlay).to.be.true;

View File

@ -229,7 +229,7 @@ describe('ContactEdit', () => {
const deleteButton = wrapper.find('.van-button')[1]; const deleteButton = wrapper.find('.van-button')[1];
deleteButton.trigger('click'); deleteButton.trigger('click');
wrapper.vm.onDeleteContact(); wrapper.vm.onDelete();
setTimeout(() => { setTimeout(() => {
wrapper.vm.isDeleting = false; wrapper.vm.isDeleting = false;

View File

@ -69,6 +69,7 @@ describe('Field', () => {
it('create a autosize textarea field', (done) => { it('create a autosize textarea field', (done) => {
wrapper = mount(Field, { wrapper = mount(Field, {
attachToDocument: true,
propsData: { propsData: {
type: 'textarea', type: 'textarea',
autosize: {} autosize: {}
@ -88,7 +89,6 @@ describe('Field', () => {
textareaElement.value = longText; textareaElement.value = longText;
textarea.trigger('input'); textarea.trigger('input');
wrapper.update();
setTimeout(() => { setTimeout(() => {
expect(wrapper.find('.van-field__control')[0].element.value).to.equal(longText); expect(wrapper.find('.van-field__control')[0].element.value).to.equal(longText);
expect(textareaElement.style.height).to.equal((textareaElement.scrollHeight - textAreaDiff) + 'px'); expect(textareaElement.style.height).to.equal((textareaElement.scrollHeight - textAreaDiff) + 'px');