index.js 1.49 KB
Newer Older
李嘉林 committed
1
import { VantComponent } from '../common/component';
程默 committed
2
import { setContentAnimate } from './animate';
李嘉林 committed
3
VantComponent({
程默 committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  classes: ['title-class', 'content-class'],
  relation: {
    name: 'collapse',
    type: 'ancestor',
    current: 'collapse-item',
  },
  props: {
    name: null,
    title: null,
    value: null,
    icon: String,
    label: String,
    disabled: Boolean,
    clickable: Boolean,
    border: {
      type: Boolean,
      value: true,
李嘉林 committed
21
    },
程默 committed
22 23 24
    isLink: {
      type: Boolean,
      value: true,
李嘉林 committed
25
    },
程默 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  },
  data: {
    expanded: false,
  },
  mounted() {
    this.updateExpanded();
    this.mounted = true;
  },
  methods: {
    updateExpanded() {
      if (!this.parent) {
        return;
      }
      const { value, accordion } = this.parent.data;
      const { children = [] } = this.parent;
      const { name } = this.data;
      const index = children.indexOf(this);
      const currentName = name == null ? index : name;
      const expanded = accordion
        ? value === currentName
        : (value || []).some((name) => name === currentName);
      if (expanded !== this.data.expanded) {
        setContentAnimate(this, expanded, this.mounted);
      }
      this.setData({ index, expanded });
李嘉林 committed
51
    },
程默 committed
52 53 54 55 56 57 58 59
    onClick() {
      if (this.data.disabled) {
        return;
      }
      const { name, expanded } = this.data;
      const index = this.parent.children.indexOf(this);
      const currentName = name == null ? index : name;
      this.parent.switch(currentName, !expanded);
李嘉林 committed
60
    },
程默 committed
61
  },
李嘉林 committed
62
});