common.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { h, computed } from 'vue';
  2. import type { ExtractPropTypes, SetupContext, RenderFunction } from 'vue';
  3. import { createComponent } from '@/packages/utils/create';
  4. import { pxCheck } from '@/packages/utils/pxCheck';
  5. import { useProvide } from '@/packages/utils/useRelation/useProvide';
  6. const { componentName } = createComponent('grid');
  7. export const GRID_KEY = Symbol('grid');
  8. export type GridDirection = 'horizontal' | 'vertical';
  9. export const gridProps = {
  10. // 列数
  11. columnNum: {
  12. type: [Number, String],
  13. default: 4
  14. },
  15. // 是否显示边框
  16. border: {
  17. type: Boolean,
  18. default: true
  19. },
  20. // 格子之间间隔距离
  21. gutter: {
  22. type: [Number, String],
  23. default: 0
  24. },
  25. // 是否内容居中
  26. center: {
  27. type: Boolean,
  28. default: true
  29. },
  30. // 是否固定正方形
  31. square: {
  32. type: Boolean,
  33. default: false
  34. },
  35. // 内容与文字翻转
  36. reverse: {
  37. type: Boolean,
  38. default: false
  39. },
  40. // 内容排列方向
  41. direction: {
  42. type: String as import('vue').PropType<GridDirection>
  43. },
  44. // 是否开启点击反馈
  45. clickable: {
  46. type: Boolean,
  47. default: false
  48. }
  49. };
  50. export type GridProps = ExtractPropTypes<typeof gridProps>;
  51. export const component = {
  52. props: gridProps,
  53. setup(props: GridProps, { slots }: SetupContext): RenderFunction {
  54. useProvide(GRID_KEY, `${componentName}-item`)({ props });
  55. const rootClass = computed(() => {
  56. const prefixCls = componentName;
  57. return {
  58. [prefixCls]: true,
  59. [`${prefixCls}--border`]: props.border && !props.gutter
  60. };
  61. });
  62. const rootStyle = computed(() => {
  63. const style: import('vue').CSSProperties = {};
  64. if (props.gutter) {
  65. style.paddingLeft = pxCheck(props.gutter);
  66. }
  67. return style;
  68. });
  69. return () => {
  70. return h(
  71. 'view',
  72. {
  73. class: rootClass.value,
  74. style: rootStyle.value
  75. },
  76. slots.default?.()
  77. );
  78. };
  79. }
  80. };