index.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { useParent } from '../common/relation';
  2. import { VantComponent } from '../common/component';
  3. function emit(target, value) {
  4. target.$emit('input', value);
  5. target.$emit('change', value);
  6. }
  7. VantComponent({
  8. field: true,
  9. relation: useParent('checkbox-group'),
  10. classes: ['icon-class', 'label-class'],
  11. props: {
  12. value: Boolean,
  13. disabled: Boolean,
  14. useIconSlot: Boolean,
  15. checkedColor: String,
  16. labelPosition: {
  17. type: String,
  18. value: 'right',
  19. },
  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. direction: 'vertical',
  33. },
  34. methods: {
  35. emitChange(value) {
  36. if (this.parent) {
  37. this.setParentValue(this.parent, value);
  38. }
  39. else {
  40. emit(this, value);
  41. }
  42. },
  43. toggle() {
  44. const { parentDisabled, disabled, value } = this.data;
  45. if (!disabled && !parentDisabled) {
  46. this.emitChange(!value);
  47. }
  48. },
  49. onClickLabel() {
  50. const { labelDisabled, parentDisabled, disabled, value } = this.data;
  51. if (!disabled && !labelDisabled && !parentDisabled) {
  52. this.emitChange(!value);
  53. }
  54. },
  55. setParentValue(parent, value) {
  56. const parentValue = parent.data.value.slice();
  57. const { name } = this.data;
  58. const { max } = parent.data;
  59. if (value) {
  60. if (max && parentValue.length >= max) {
  61. return;
  62. }
  63. if (parentValue.indexOf(name) === -1) {
  64. parentValue.push(name);
  65. emit(parent, parentValue);
  66. }
  67. }
  68. else {
  69. const index = parentValue.indexOf(name);
  70. if (index !== -1) {
  71. parentValue.splice(index, 1);
  72. emit(parent, parentValue);
  73. }
  74. }
  75. },
  76. },
  77. });