styles-helper.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { inject } from 'vue';
  2. import { getFixedColumnOffset, ensurePosition, getFixedColumnsClass } from '../util.mjs';
  3. import { TABLE_INJECTION_KEY } from '../tokens.mjs';
  4. import { useNamespace } from '../../../../hooks/use-namespace/index.mjs';
  5. import { isFunction, isString, isArray, isObject } from '@vue/shared';
  6. function useStyles(props) {
  7. const parent = inject(TABLE_INJECTION_KEY);
  8. const ns = useNamespace("table");
  9. const getRowStyle = (row, rowIndex) => {
  10. const rowStyle = parent == null ? void 0 : parent.props.rowStyle;
  11. if (isFunction(rowStyle)) {
  12. return rowStyle.call(null, {
  13. row,
  14. rowIndex
  15. });
  16. }
  17. return rowStyle || null;
  18. };
  19. const getRowClass = (row, rowIndex) => {
  20. const classes = [ns.e("row")];
  21. if ((parent == null ? void 0 : parent.props.highlightCurrentRow) && row === props.store.states.currentRow.value) {
  22. classes.push("current-row");
  23. }
  24. if (props.stripe && rowIndex % 2 === 1) {
  25. classes.push(ns.em("row", "striped"));
  26. }
  27. const rowClassName = parent == null ? void 0 : parent.props.rowClassName;
  28. if (isString(rowClassName)) {
  29. classes.push(rowClassName);
  30. } else if (isFunction(rowClassName)) {
  31. classes.push(rowClassName.call(null, {
  32. row,
  33. rowIndex
  34. }));
  35. }
  36. return classes;
  37. };
  38. const getCellStyle = (rowIndex, columnIndex, row, column) => {
  39. const cellStyle = parent == null ? void 0 : parent.props.cellStyle;
  40. let cellStyles = cellStyle != null ? cellStyle : {};
  41. if (isFunction(cellStyle)) {
  42. cellStyles = cellStyle.call(null, {
  43. rowIndex,
  44. columnIndex,
  45. row,
  46. column
  47. });
  48. }
  49. const fixedStyle = getFixedColumnOffset(columnIndex, props == null ? void 0 : props.fixed, props.store);
  50. ensurePosition(fixedStyle, "left");
  51. ensurePosition(fixedStyle, "right");
  52. return Object.assign({}, cellStyles, fixedStyle);
  53. };
  54. const getCellClass = (rowIndex, columnIndex, row, column, offset) => {
  55. const fixedClasses = getFixedColumnsClass(ns.b(), columnIndex, props == null ? void 0 : props.fixed, props.store, void 0, offset);
  56. const classes = [column.id, column.align, column.className, ...fixedClasses];
  57. const cellClassName = parent == null ? void 0 : parent.props.cellClassName;
  58. if (isString(cellClassName)) {
  59. classes.push(cellClassName);
  60. } else if (isFunction(cellClassName)) {
  61. classes.push(cellClassName.call(null, {
  62. rowIndex,
  63. columnIndex,
  64. row,
  65. column
  66. }));
  67. }
  68. classes.push(ns.e("cell"));
  69. return classes.filter((className) => Boolean(className)).join(" ");
  70. };
  71. const getSpan = (row, column, rowIndex, columnIndex) => {
  72. let rowspan = 1;
  73. let colspan = 1;
  74. const fn = parent == null ? void 0 : parent.props.spanMethod;
  75. if (isFunction(fn)) {
  76. const result = fn({
  77. row,
  78. column,
  79. rowIndex,
  80. columnIndex
  81. });
  82. if (isArray(result)) {
  83. rowspan = result[0];
  84. colspan = result[1];
  85. } else if (isObject(result)) {
  86. rowspan = result.rowspan;
  87. colspan = result.colspan;
  88. }
  89. }
  90. return { rowspan, colspan };
  91. };
  92. const getColspanRealWidth = (columns, colspan, index) => {
  93. if (colspan < 1) {
  94. return columns[index].realWidth;
  95. }
  96. const widthArr = columns.map(({ realWidth, width }) => realWidth || width).slice(index, index + colspan);
  97. return Number(widthArr.reduce((acc, width) => Number(acc) + Number(width), -1));
  98. };
  99. return {
  100. getRowStyle,
  101. getRowClass,
  102. getCellStyle,
  103. getCellClass,
  104. getSpan,
  105. getColspanRealWidth
  106. };
  107. }
  108. export { useStyles as default };
  109. //# sourceMappingURL=styles-helper.mjs.map