index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { link } from '../mixins/link';
  2. import { VantComponent } from '../common/component';
  3. import { addUnit } from '../common/utils';
  4. VantComponent({
  5. relation: {
  6. name: 'grid',
  7. type: 'ancestor',
  8. linked(parent) {
  9. this.parent = parent;
  10. }
  11. },
  12. mixins: [link],
  13. props: {
  14. icon: String,
  15. dot: Boolean,
  16. info: null,
  17. text: String,
  18. useSlot: Boolean
  19. },
  20. mounted() {
  21. this.updateStyle();
  22. },
  23. methods: {
  24. updateStyle() {
  25. if (!this.parent) {
  26. return;
  27. }
  28. const { data, children } = this.parent;
  29. const { columnNum, border, square, gutter, clickable, center } = data;
  30. const width = `${100 / columnNum}%`;
  31. const styleWrapper = [];
  32. styleWrapper.push(`width: ${width}`);
  33. if (square) {
  34. styleWrapper.push(`padding-top: ${width}`);
  35. }
  36. if (gutter) {
  37. const gutterValue = addUnit(gutter);
  38. styleWrapper.push(`padding-right: ${gutterValue}`);
  39. const index = children.indexOf(this);
  40. if (index >= columnNum) {
  41. styleWrapper.push(`margin-top: ${gutterValue}`);
  42. }
  43. }
  44. let contentStyle = '';
  45. if (square && gutter) {
  46. const gutterValue = addUnit(gutter);
  47. contentStyle = `
  48. right: ${gutterValue};
  49. bottom: ${gutterValue};
  50. height: auto;
  51. `;
  52. }
  53. this.setData({
  54. style: styleWrapper.join('; '),
  55. contentStyle,
  56. center,
  57. border,
  58. square,
  59. gutter,
  60. clickable
  61. });
  62. },
  63. onClick() {
  64. this.$emit('click');
  65. this.jumpLink();
  66. }
  67. }
  68. });