index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  38. else {
  39. emit(this, value);
  40. }
  41. },
  42. toggle() {
  43. const { parentDisabled, disabled, value } = this.data;
  44. if (!disabled && !parentDisabled) {
  45. this.emitChange(!value);
  46. }
  47. },
  48. onClickLabel() {
  49. const { labelDisabled, parentDisabled, disabled, value } = this.data;
  50. if (!disabled && !labelDisabled && !parentDisabled) {
  51. this.emitChange(!value);
  52. }
  53. },
  54. setParentValue(parent, value) {
  55. const parentValue = parent.data.value.slice();
  56. const { name } = this.data;
  57. const { max } = parent.data;
  58. if (value) {
  59. if (max && parentValue.length >= max) {
  60. return;
  61. }
  62. if (parentValue.indexOf(name) === -1) {
  63. parentValue.push(name);
  64. emit(parent, parentValue);
  65. }
  66. }
  67. else {
  68. const index = parentValue.indexOf(name);
  69. if (index !== -1) {
  70. parentValue.splice(index, 1);
  71. emit(parent, parentValue);
  72. }
  73. }
  74. }
  75. }
  76. });