index.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(value) {
  23. const parent = this.getRelationNodes('../checkbox-group/index')[0];
  24. if (parent) {
  25. this.setParentValue(parent, value);
  26. }
  27. else {
  28. this.$emit('input', value);
  29. this.$emit('change', value);
  30. }
  31. },
  32. toggle() {
  33. if (!this.data.disabled) {
  34. this.emitChange(!this.data.value);
  35. }
  36. },
  37. onClickLabel() {
  38. if (!this.data.disabled && !this.data.labelDisabled) {
  39. this.emitChange(!this.data.value);
  40. }
  41. },
  42. setParentValue(parent, value) {
  43. const parentValue = parent.data.value.slice();
  44. const { name } = this.data;
  45. if (value) {
  46. if (parent.data.max && parentValue.length >= parent.data.max) {
  47. return;
  48. }
  49. /* istanbul ignore else */
  50. if (parentValue.indexOf(name) === -1) {
  51. parentValue.push(name);
  52. parent.$emit('input', parentValue);
  53. parent.$emit('change', parentValue);
  54. }
  55. }
  56. else {
  57. const index = parentValue.indexOf(name);
  58. /* istanbul ignore else */
  59. if (index !== -1) {
  60. parentValue.splice(index, 1);
  61. parent.$emit('input', parentValue);
  62. parent.$emit('change', parentValue);
  63. }
  64. }
  65. }
  66. }
  67. });