index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. } 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. } else {
  67. const index = parentValue.indexOf(name);
  68. if (index !== -1) {
  69. parentValue.splice(index, 1);
  70. emit(parent, parentValue);
  71. }
  72. }
  73. },
  74. },
  75. });