index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { use } from '../utils';
  2. import { CheckboxMixin } from '../mixins/checkbox';
  3. var _use = use('checkbox'),
  4. sfc = _use[0],
  5. bem = _use[1];
  6. export default sfc({
  7. mixins: [CheckboxMixin('van-checkbox-group', bem)],
  8. computed: {
  9. checked: {
  10. get: function get() {
  11. return this.parent ? this.parent.value.indexOf(this.name) !== -1 : this.value;
  12. },
  13. set: function set(val) {
  14. if (this.parent) {
  15. this.setParentValue(val);
  16. } else {
  17. this.$emit('input', val);
  18. }
  19. }
  20. }
  21. },
  22. watch: {
  23. value: function value(val) {
  24. this.$emit('change', val);
  25. }
  26. },
  27. methods: {
  28. toggle: function toggle() {
  29. var _this = this;
  30. var checked = !this.checked; // When toggle method is called multiple times at the same time,
  31. // only the last call is valid.
  32. // This is a hack for usage inside Cell.
  33. clearTimeout(this.toggleTask);
  34. this.toggleTask = setTimeout(function () {
  35. _this.checked = checked;
  36. });
  37. },
  38. onClickIcon: function onClickIcon() {
  39. if (!this.isDisabled) {
  40. this.toggle();
  41. }
  42. },
  43. onClickLabel: function onClickLabel() {
  44. if (!this.isDisabled && !this.labelDisabled) {
  45. this.toggle();
  46. }
  47. },
  48. setParentValue: function setParentValue(val) {
  49. var parent = this.parent;
  50. var value = parent.value.slice();
  51. if (val) {
  52. if (parent.max && value.length >= parent.max) {
  53. return;
  54. }
  55. /* istanbul ignore else */
  56. if (value.indexOf(this.name) === -1) {
  57. value.push(this.name);
  58. parent.$emit('input', value);
  59. }
  60. } else {
  61. var index = value.indexOf(this.name);
  62. /* istanbul ignore else */
  63. if (index !== -1) {
  64. value.splice(index, 1);
  65. parent.$emit('input', value);
  66. }
  67. }
  68. }
  69. }
  70. });