diff --git a/packages/checkbox/index.js b/packages/checkbox/index.js
index ba3570abe..994eb4c59 100644
--- a/packages/checkbox/index.js
+++ b/packages/checkbox/index.js
@@ -1,24 +1,15 @@
import { use } from '../utils';
-import Icon from '../icon';
-import findParent from '../mixins/find-parent';
+import CheckboxMixin from '../mixins/checkbox';
const [sfc, bem] = use('checkbox');
export default sfc({
- mixins: [findParent],
-
- props: {
- name: null,
- value: null,
- disabled: Boolean,
- checkedColor: String,
- labelPosition: String,
- labelDisabled: Boolean,
- shape: {
- type: String,
- default: 'round'
- }
- },
+ mixins: [
+ CheckboxMixin({
+ bem,
+ parent: 'van-checkbox-group'
+ })
+ ],
computed: {
checked: {
@@ -33,20 +24,6 @@ export default sfc({
this.$emit('input', val);
}
}
- },
-
- isDisabled() {
- return (this.parent && this.parent.disabled) || this.disabled;
- },
-
- iconStyle() {
- const { checkedColor } = this;
- if (checkedColor && this.checked && !this.isDisabled) {
- return {
- borderColor: checkedColor,
- backgroundColor: checkedColor
- };
- }
}
},
@@ -56,14 +33,20 @@ export default sfc({
}
},
- created() {
- this.findParent('van-checkbox-group');
- },
-
methods: {
- toggle(target) {
- if (!this.isDisabled && !(target === 'label' && this.labelDisabled)) {
- this.checked = !this.checked;
+ toggle() {
+ this.checked = !this.checked;
+ },
+
+ onClickIcon() {
+ if (!this.isDisabled) {
+ this.toggle();
+ }
+ },
+
+ onClickLabel() {
+ if (!this.isDisabled && !this.labelDisabled) {
+ this.toggle();
}
},
@@ -91,36 +74,5 @@ export default sfc({
}
}
}
- },
-
- render(h) {
- const CheckIcon = this.$scopedSlots.icon ? (
- this.$scopedSlots.icon({ checked: this.checked })
- ) : (
-
- );
-
- const Label = this.$slots.default && (
- {
- this.toggle('label');
- }}
- >
- {this.$slots.default}
-
- );
-
- return (
-
-
- {CheckIcon}
-
- {Label}
-
- );
}
});
diff --git a/packages/mixins/checkbox.js b/packages/mixins/checkbox.js
new file mode 100644
index 000000000..e3cb4b9d7
--- /dev/null
+++ b/packages/mixins/checkbox.js
@@ -0,0 +1,76 @@
+/**
+ * Common part of Checkbox & Radio
+ */
+import Icon from '../icon';
+import findParent from './find-parent';
+
+export default ({ parent, bem }) => ({
+ mixins: [findParent],
+
+ props: {
+ name: null,
+ value: null,
+ disabled: Boolean,
+ checkedColor: String,
+ labelPosition: String,
+ labelDisabled: Boolean,
+ shape: {
+ type: String,
+ default: 'round'
+ }
+ },
+
+ created() {
+ this.findParent(parent);
+ },
+
+ computed: {
+ isDisabled() {
+ return (this.parent && this.parent.disabled) || this.disabled;
+ },
+
+ iconStyle() {
+ const { checkedColor } = this;
+ if (checkedColor && this.checked && !this.isDisabled) {
+ return {
+ borderColor: checkedColor,
+ backgroundColor: checkedColor
+ };
+ }
+ }
+ },
+
+ render(h) {
+ const CheckIcon = this.$scopedSlots.icon ? (
+ this.$scopedSlots.icon({ checked: this.checked })
+ ) : (
+
+ );
+
+ const Label = this.$slots.default && (
+
+ {this.$slots.default}
+
+ );
+
+ return (
+ {
+ this.$emit('click');
+ }}
+ >
+
+ {CheckIcon}
+
+ {Label}
+
+ );
+ }
+});
diff --git a/packages/radio/index.js b/packages/radio/index.js
index 3e4f9cf85..8552690fc 100644
--- a/packages/radio/index.js
+++ b/packages/radio/index.js
@@ -1,24 +1,15 @@
import { use } from '../utils';
-import Icon from '../icon';
-import findParent from '../mixins/find-parent';
+import CheckboxMixin from '../mixins/checkbox';
const [sfc, bem] = use('radio');
export default sfc({
- mixins: [findParent],
-
- props: {
- name: null,
- value: null,
- disabled: Boolean,
- checkedColor: String,
- labelPosition: String,
- labelDisabled: Boolean,
- shape: {
- type: String,
- default: 'round'
- }
- },
+ mixins: [
+ CheckboxMixin({
+ bem,
+ parent: 'van-radio-group'
+ })
+ ],
computed: {
currentValue: {
@@ -31,25 +22,11 @@ export default sfc({
}
},
- isDisabled() {
- return this.parent ? this.parent.disabled || this.disabled : this.disabled;
- },
-
- iconStyle() {
- const { checkedColor } = this;
- if (checkedColor && this.currentValue === this.name && !this.isDisabled) {
- return {
- borderColor: checkedColor,
- backgroundColor: checkedColor
- };
- }
+ checked() {
+ return this.currentValue === this.name;
}
},
- created() {
- this.findParent('van-radio-group');
- },
-
methods: {
onClickIcon() {
if (!this.isDisabled) {
@@ -62,40 +39,5 @@ export default sfc({
this.currentValue = this.name;
}
}
- },
-
- render(h) {
- const checked = this.currentValue === this.name;
- const CheckIcon = this.$scopedSlots.icon ? (
- this.$scopedSlots.icon({ checked })
- ) : (
-
- );
-
- const Label = this.$slots.default && (
-
- {this.$slots.default}
-
- );
-
- return (
- {
- this.$emit('click');
- }}
- >
-
- {CheckIcon}
-
- {Label}
-
- );
}
});