index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { VantComponent } from '../common/component';
  2. import { useParent } from '../common/relation';
  3. VantComponent({
  4. props: {
  5. info: null,
  6. name: null,
  7. icon: String,
  8. dot: Boolean,
  9. url: {
  10. type: String,
  11. value: '',
  12. },
  13. linkType: {
  14. type: String,
  15. value: 'redirectTo',
  16. },
  17. iconPrefix: {
  18. type: String,
  19. value: 'van-icon',
  20. },
  21. },
  22. relation: useParent('tabbar'),
  23. data: {
  24. active: false,
  25. activeColor: '',
  26. inactiveColor: '',
  27. },
  28. methods: {
  29. onClick() {
  30. const { parent } = this;
  31. if (parent) {
  32. const index = parent.children.indexOf(this);
  33. const active = this.data.name || index;
  34. if (active !== this.data.active) {
  35. parent.$emit('change', active);
  36. }
  37. }
  38. const { url, linkType } = this.data;
  39. if (url && wx[linkType]) {
  40. return wx[linkType]({ url });
  41. }
  42. this.$emit('click');
  43. },
  44. updateFromParent() {
  45. const { parent } = this;
  46. if (!parent) {
  47. return;
  48. }
  49. const index = parent.children.indexOf(this);
  50. const parentData = parent.data;
  51. const { data } = this;
  52. const active = (data.name || index) === parentData.active;
  53. const patch = {};
  54. if (active !== data.active) {
  55. patch.active = active;
  56. }
  57. if (parentData.activeColor !== data.activeColor) {
  58. patch.activeColor = parentData.activeColor;
  59. }
  60. if (parentData.inactiveColor !== data.inactiveColor) {
  61. patch.inactiveColor = parentData.inactiveColor;
  62. }
  63. if (Object.keys(patch).length > 0) {
  64. this.setData(patch);
  65. }
  66. },
  67. },
  68. });