index.js 2.0 KB

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