index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* eslint-disable object-shorthand */
  2. import { use } from '../utils';
  3. import { FindParentMixin } from '../mixins/find-parent';
  4. var _use = use('tab'),
  5. sfc = _use[0],
  6. bem = _use[1];
  7. export default sfc({
  8. mixins: [FindParentMixin],
  9. props: {
  10. title: String,
  11. disabled: Boolean
  12. },
  13. data: function data() {
  14. return {
  15. inited: false
  16. };
  17. },
  18. computed: {
  19. index: function index() {
  20. return this.parent.tabs.indexOf(this);
  21. },
  22. selected: function selected() {
  23. return this.index === this.parent.curActive;
  24. }
  25. },
  26. watch: {
  27. 'parent.curActive': function parentCurActive() {
  28. this.inited = this.inited || this.selected;
  29. },
  30. title: function title() {
  31. this.parent.setLine();
  32. }
  33. },
  34. created: function created() {
  35. this.findParent('van-tabs');
  36. },
  37. mounted: function mounted() {
  38. var tabs = this.parent.tabs;
  39. var index = this.parent.slots().indexOf(this.$vnode);
  40. tabs.splice(index === -1 ? tabs.length : index, 0, this);
  41. if (this.slots('title')) {
  42. this.parent.renderTitle(this.$refs.title, this.index);
  43. }
  44. },
  45. beforeDestroy: function beforeDestroy() {
  46. this.parent.tabs.splice(this.index, 1);
  47. },
  48. render: function render(h) {
  49. var slots = this.slots;
  50. var shouldRender = this.inited || !this.parent.lazyRender;
  51. return h("div", {
  52. "directives": [{
  53. name: "show",
  54. value: this.selected || this.parent.animated
  55. }],
  56. "class": bem('pane')
  57. }, [shouldRender ? slots() : h(), slots('title') && h("div", {
  58. "ref": "title"
  59. }, [slots('title')])]);
  60. }
  61. });