index.js 1.7 KB

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