index.js 1.9 KB

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