index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. field: true,
  4. relation: {
  5. name: 'checkbox-group',
  6. type: 'ancestor'
  7. },
  8. classes: ['icon-class', 'label-class'],
  9. props: {
  10. value: null,
  11. disabled: Boolean,
  12. useIconSlot: Boolean,
  13. checkedColor: String,
  14. labelPosition: String,
  15. labelDisabled: Boolean,
  16. shape: {
  17. type: String,
  18. value: 'round'
  19. }
  20. },
  21. methods: {
  22. emitChange: function emitChange(value) {
  23. var parent = this.getRelationNodes('../checkbox-group/index')[0];
  24. if (parent) {
  25. this.setParentValue(parent, value);
  26. } else {
  27. this.$emit('input', value);
  28. this.$emit('change', value);
  29. }
  30. },
  31. toggle: function toggle() {
  32. if (!this.data.disabled) {
  33. this.emitChange(!this.data.value);
  34. }
  35. },
  36. onClickLabel: function onClickLabel() {
  37. if (!this.data.disabled && !this.data.labelDisabled) {
  38. this.emitChange(!this.data.value);
  39. }
  40. },
  41. setParentValue: function setParentValue(parent, value) {
  42. var parentValue = parent.data.value.slice();
  43. var name = this.data.name;
  44. if (value) {
  45. if (parent.data.max && parentValue.length >= parent.data.max) {
  46. return;
  47. }
  48. /* istanbul ignore else */
  49. if (parentValue.indexOf(name) === -1) {
  50. parentValue.push(name);
  51. parent.$emit('input', parentValue);
  52. parent.$emit('change', parentValue);
  53. }
  54. } else {
  55. var index = parentValue.indexOf(name);
  56. /* istanbul ignore else */
  57. if (index !== -1) {
  58. parentValue.splice(index, 1);
  59. parent.$emit('input', parentValue);
  60. parent.$emit('change', parentValue);
  61. }
  62. }
  63. }
  64. }
  65. });