Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 chrome 低版本列设置宽度无效的问题 #9026

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/amis-core/src/utils/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const chromeVersion = (function getChromeVersion() {
const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
})();
1 change: 1 addition & 0 deletions packages/amis-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './api';
export * from './attachmentAdpator';
export * from './autobind';
export * from './browser';
export * from './ColorScale';
export * from './columnsSplit';
export * from './dataMapping';
Expand Down
46 changes: 45 additions & 1 deletion packages/amis/src/renderers/Table/ColGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import type {IColumn, ITableStore} from 'amis-core';
import {chromeVersion, type IColumn, type ITableStore} from 'amis-core';
import {observer} from 'mobx-react';

export function ColGroup({
Expand Down Expand Up @@ -33,6 +33,50 @@ export function ColGroup({
};
}, []);

// 解决 chrome 91 以下版本的设置 colgroup>col 的 width 属性无效的问题
// 低版本同时设置 thead>th
// The problem is min-width CSS property.
// Before Chrome 91, min-width was ignored on COL elements. 91 no longer ignores it.
if (typeof chromeVersion === 'number' && chromeVersion < 91) {
React.useEffect(() => {
if (domRef.current) {
const ths = [].slice.call(
domRef.current.parentElement!.querySelectorAll(
':scope > thead > tr > th[data-index]'
)
);
ths.forEach((th: HTMLTableCellElement) => {
const index = parseInt(th.getAttribute('data-index')!, 10);
const column = store.columns[index];

let style = '';
let width: any = -1;

if (store.columnWidthReady && column.width) {
width = column.width;
} else if (column.pristine.width) {
width = column.pristine.width;
}

if (width === -1) {
return;
}
style += `width: ${
// 有可能是百分比
typeof width === 'number' ? `${width}px` : width
};`;

if (store.tableLayout === 'auto') {
style += `min-width: ${
typeof width === 'number' ? `${width}px` : width
};`;
}
th.style.cssText = style;
});
}
}, columns.map(column => column.width).concat(store.columnWidthReady as any));
}

return (
<colgroup ref={domRef}>
{columns.map(column => {
Expand Down
Loading