index.js 2.0 KB

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