index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. current: 'grid-item',
  9. },
  10. classes: ['content-class', 'icon-class', 'text-class'],
  11. mixins: [link],
  12. props: {
  13. icon: String,
  14. dot: Boolean,
  15. info: null,
  16. text: String,
  17. useSlot: Boolean
  18. },
  19. data: {
  20. viewStyle: '',
  21. },
  22. mounted() {
  23. this.updateStyle();
  24. },
  25. methods: {
  26. updateStyle() {
  27. if (!this.parent) {
  28. return;
  29. }
  30. const { data, children } = this.parent;
  31. const { columnNum, border, square, gutter, clickable, center } = data;
  32. const width = `${100 / columnNum}%`;
  33. const styleWrapper = [];
  34. styleWrapper.push(`width: ${width}`);
  35. if (square) {
  36. styleWrapper.push(`padding-top: ${width}`);
  37. }
  38. if (gutter) {
  39. const gutterValue = addUnit(gutter);
  40. styleWrapper.push(`padding-right: ${gutterValue}`);
  41. const index = children.indexOf(this);
  42. if (index >= columnNum) {
  43. styleWrapper.push(`margin-top: ${gutterValue}`);
  44. }
  45. }
  46. let contentStyle = '';
  47. if (square && gutter) {
  48. const gutterValue = addUnit(gutter);
  49. contentStyle = `
  50. right: ${gutterValue};
  51. bottom: ${gutterValue};
  52. height: auto;
  53. `;
  54. }
  55. this.setData({
  56. viewStyle: styleWrapper.join('; '),
  57. contentStyle,
  58. center,
  59. border,
  60. square,
  61. gutter,
  62. clickable
  63. });
  64. },
  65. onClick() {
  66. this.$emit('click');
  67. this.jumpLink();
  68. }
  69. }
  70. });