diff --git a/packages/checkbox-group/index.js b/packages/checkbox-group/index.js index 87254796c..4957d2c56 100644 --- a/packages/checkbox-group/index.js +++ b/packages/checkbox-group/index.js @@ -1,8 +1,11 @@ import { use } from '../utils'; +import { ParentMixin } from '../mixins/relation'; const [sfc, bem] = use('checkbox-group'); export default sfc({ + mixins: [ParentMixin('vanCheckbox')], + props: { max: Number, value: Array, diff --git a/packages/checkbox/index.js b/packages/checkbox/index.js index e01f188cf..8eaad0f1d 100644 --- a/packages/checkbox/index.js +++ b/packages/checkbox/index.js @@ -4,7 +4,7 @@ import { CheckboxMixin } from '../mixins/checkbox'; const [sfc, bem] = use('checkbox'); export default sfc({ - mixins: [CheckboxMixin('van-checkbox-group', bem)], + mixins: [CheckboxMixin('vanCheckbox', bem)], computed: { checked: { diff --git a/packages/coupon-list/test/__snapshots__/index.spec.js.snap b/packages/coupon-list/test/__snapshots__/index.spec.js.snap index 527ff96c0..021657bce 100644 --- a/packages/coupon-list/test/__snapshots__/index.spec.js.snap +++ b/packages/coupon-list/test/__snapshots__/index.spec.js.snap @@ -28,13 +28,13 @@ exports[`render coupon list 1`] = `
-
+
可使用优惠券 (6)
不可使用优惠券 (2)
-
+
diff --git a/packages/mixins/checkbox.js b/packages/mixins/checkbox.js index 6d8ea11a6..a9412c67f 100644 --- a/packages/mixins/checkbox.js +++ b/packages/mixins/checkbox.js @@ -2,10 +2,10 @@ * Common part of Checkbox & Radio */ import Icon from '../icon'; -import { FindParentMixin } from './find-parent'; +import { ChildrenMixin } from './relation'; export const CheckboxMixin = (parent, bem) => ({ - mixins: [FindParentMixin], + mixins: [ChildrenMixin(parent)], props: { name: null, @@ -20,10 +20,6 @@ export const CheckboxMixin = (parent, bem) => ({ } }, - created() { - this.findParent(parent); - }, - computed: { isDisabled() { return (this.parent && this.parent.disabled) || this.disabled; diff --git a/packages/mixins/find-parent.js b/packages/mixins/find-parent.js deleted file mode 100644 index d61e4460e..000000000 --- a/packages/mixins/find-parent.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * find parent component by name - */ - -export const FindParentMixin = { - data() { - return { - parent: null - }; - }, - - methods: { - findParent(name) { - let parent = this.$parent; - while (parent) { - if (parent.$options.name === name) { - this.parent = parent; - break; - } - parent = parent.$parent; - } - } - } -}; diff --git a/packages/mixins/relation.js b/packages/mixins/relation.js index 87146d793..1f100daec 100644 --- a/packages/mixins/relation.js +++ b/packages/mixins/relation.js @@ -1,6 +1,10 @@ export function ChildrenMixin(parent) { return { - inject: [parent], + inject: { + [parent]: { + default: null + } + }, computed: { parent() { @@ -18,11 +22,17 @@ export function ChildrenMixin(parent) { }, beforeDestroy() { - this.parent.children = this.parent.children.filter(item => item !== this); + if (this.parent) { + this.parent.children = this.parent.children.filter(item => item !== this); + } }, methods: { bindRelation() { + if (!this.parent) { + return; + } + const { children } = this.parent; if (children.indexOf(this) === -1) { diff --git a/packages/radio-group/index.js b/packages/radio-group/index.js index c0d5fbee9..abc16a1bc 100644 --- a/packages/radio-group/index.js +++ b/packages/radio-group/index.js @@ -1,8 +1,11 @@ import { use } from '../utils'; +import { ParentMixin } from '../mixins/relation'; const [sfc, bem] = use('radio-group'); export default sfc({ + mixins: [ParentMixin('vanRadio')], + props: { value: null, disabled: Boolean diff --git a/packages/radio/index.js b/packages/radio/index.js index 8e3127f9f..52f02d5fd 100644 --- a/packages/radio/index.js +++ b/packages/radio/index.js @@ -4,7 +4,7 @@ import { CheckboxMixin } from '../mixins/checkbox'; const [sfc, bem] = use('radio'); export default sfc({ - mixins: [CheckboxMixin('van-radio-group', bem)], + mixins: [CheckboxMixin('vanRadio', bem)], computed: { currentValue: {