diff --git a/packages/amis-core/src/store/table.ts b/packages/amis-core/src/store/table.ts
index 10978b5b8..cb37de97a 100644
--- a/packages/amis-core/src/store/table.ts
+++ b/packages/amis-core/src/store/table.ts
@@ -938,23 +938,29 @@ export const TableStore = iRendererStore
});
const originColumns = self.columns.concat();
- columns = columns.map((item, index) => ({
- ...item,
- id: guid(),
- index,
- width: originColumns[index]?.width || 0,
- minWidth: originColumns[index]?.minWidth || 0,
- rawIndex: index - PARTITION_INDEX,
- type: item.type || 'plain',
- pristine: item.pristine || item,
- toggled: item.toggled !== false,
- breakpoint: item.breakpoint,
- isPrimary: index === PARTITION_INDEX,
- /** 提前映射变量,方便后续view中使用 */
- label: isPureVariable(item.label)
- ? resolveVariableAndFilter(item.label, self.data)
- : item.label
- }));
+ columns = columns.map((item, index) => {
+ const origin = item.id
+ ? originColumns.find(column => column.id === item.id)
+ : originColumns[index];
+
+ return {
+ ...item,
+ id: guid(),
+ index,
+ width: origin?.width || 0,
+ minWidth: origin?.minWidth || 0,
+ rawIndex: index - PARTITION_INDEX,
+ type: item.type || 'plain',
+ pristine: item.pristine || item,
+ toggled: item.toggled !== false,
+ breakpoint: item.breakpoint,
+ isPrimary: index === PARTITION_INDEX,
+ /** 提前映射变量,方便后续view中使用 */
+ label: isPureVariable(item.label)
+ ? resolveVariableAndFilter(item.label, self.data)
+ : item.label
+ };
+ });
self.columns.replace(columns as any);
self.useFixedLayout = self.columns.some(
@@ -968,91 +974,63 @@ export const TableStore = iRendererStore
if (!table) {
return;
}
- // 自动将 table-layout: auto 改成 fixed
- const ths = (
- [].slice.call(
- table.querySelectorAll(':scope>thead>tr>th[data-index]')
- ) as HTMLTableCellElement[]
- ).filter((th, index, arr) => {
- return (
- arr.findIndex(
- item =>
- item.getAttribute('data-index') === th.getAttribute('data-index')
- ) === index
- );
- });
+ const tableWidth = table.parentElement!.offsetWidth;
+ const thead = table.querySelector(':scope>thead')!;
const tbodyTr = table.querySelector(':scope>tbody>tr:first-child');
-
const div = document.createElement('div');
div.className = 'amis-scope'; // jssdk 里面 css 会在这一层
- div.style.cssText = `position:absolute;top:0;left:0;pointer-events:none;visibility: hidden;`;
- div.innerHTML = `
${ths
- .map(
- th =>
- `${th.innerHTML} | `
- )
- .join(
+ div.style.cssText += `visibility: hidden!important;`;
+ div.innerHTML =
+ `` +
+ `${ths
- .map(th => {
- const index = parseInt(th.getAttribute('data-index')!, 10);
- const column = self.columns[index];
+ )}">${thead.outerHTML}${
+ tbodyTr ? `
${tbodyTr.outerHTML}` : ''
+ }
`;
+ const ths1: Array = [].slice.call(
+ div.querySelectorAll(':scope>table:first-child>thead>tr>th[data-index]')
+ );
+ const ths2: Array = [].slice.call(
+ div.querySelectorAll(':scope>table:last-child>thead>tr>th[data-index]')
+ );
- return `${th.innerHTML} | `;
- })
- .join('')}
${
- tbodyTr ? `${tbodyTr.outerHTML}` : ''
- }
`;
+ ths1.forEach(th => {
+ th.style.cssText += 'width: 0';
+ });
+ ths2.forEach(th => {
+ const index = parseInt(th.getAttribute('data-index')!, 10);
+ const column = self.columns[index];
+
+ th.style.cssText += `${
+ typeof column.pristine.width === 'number'
+ ? `width: ${column.pristine.width}px;`
+ : column.pristine.width
+ ? `width: ${column.pristine.width};`
+ : ''
+ }`;
+ });
document.body.appendChild(div);
const minWidths: {
[propName: string]: number;
} = {};
- [].slice
- .call(
- div.querySelectorAll(
- ':scope>table:first-child>thead>tr>th[data-index]'
- )
- )
- .forEach((th: HTMLTableCellElement) => {
- minWidths[th.getAttribute('data-index')!] = th.clientWidth;
- });
+ ths1.forEach((th: HTMLTableCellElement) => {
+ minWidths[th.getAttribute('data-index')!] = th.clientWidth;
+ });
- [].slice
- .call(
- div.querySelectorAll(
- ':scope>table:last-child>thead>tr>th[data-index]'
- )
- )
- .forEach((col: HTMLElement) => {
- const index = parseInt(col.getAttribute('data-index')!, 10);
- const column = self.columns[index];
- column.setWidth(
- Math.max(
- typeof column.pristine.width === 'number'
- ? column.pristine.width
- : col.clientWidth - 2,
- minWidths[index]
- ),
+ ths2.forEach((col: HTMLElement) => {
+ const index = parseInt(col.getAttribute('data-index')!, 10);
+ const column = self.columns[index];
+ column.setWidth(
+ Math.max(
+ typeof column.pristine.width === 'number'
+ ? column.pristine.width
+ : col.clientWidth - 2,
minWidths[index]
- );
- });
+ ),
+ minWidths[index]
+ );
+ });
document.body.removeChild(div);
}
diff --git a/packages/amis-editor-core/src/component/factory.tsx b/packages/amis-editor-core/src/component/factory.tsx
index 50dbdaf6c..b0e2b6741 100644
--- a/packages/amis-editor-core/src/component/factory.tsx
+++ b/packages/amis-editor-core/src/component/factory.tsx
@@ -153,9 +153,11 @@ export function makeWrapper(
ref = ref.getWrappedInstance();
}
- this.editorNode &&
- isAlive(this.editorNode) &&
+ if (this.editorNode && isAlive(this.editorNode)) {
this.editorNode.setComponent(ref);
+
+ info.plugin?.componentRef?.(this.editorNode, ref);
+ }
}
/**
diff --git a/packages/amis-editor-core/src/plugin.ts b/packages/amis-editor-core/src/plugin.ts
index 9f30b6897..79cb94dd3 100644
--- a/packages/amis-editor-core/src/plugin.ts
+++ b/packages/amis-editor-core/src/plugin.ts
@@ -940,6 +940,13 @@ export interface PluginInterface
) => Schema | void;
dispose?: () => void;
+
+ /**
+ * 组件 ref 回调,mount 和 unmount 的时候都会调用
+ * @param ref
+ * @returns
+ */
+ componentRef?: (node: EditorNodeType, ref: any) => void;
}
export interface RendererPluginEvent {
diff --git a/packages/amis-editor/src/plugin/Table.tsx b/packages/amis-editor/src/plugin/Table.tsx
index 4c495bedc..12754a096 100644
--- a/packages/amis-editor/src/plugin/Table.tsx
+++ b/packages/amis-editor/src/plugin/Table.tsx
@@ -32,6 +32,7 @@ import {
schemaToArray,
resolveArrayDatasource
} from '../util';
+import {reaction} from 'mobx';
export class TablePlugin extends BasePlugin {
static id = 'TablePlugin';
@@ -942,6 +943,26 @@ export class TablePlugin extends BasePlugin {
}
});
}
+
+ unWatchWidthChange: {[propName: string]: () => void} = {};
+ componentRef(node: EditorNodeType, ref: any) {
+ if (ref) {
+ const store = ref.props.store;
+ this.unWatchWidthChange[node.id] = reaction(
+ () =>
+ store.columns.map((column: any) => column.pristine.width).join(','),
+ () => {
+ ref.updateTableInfoLazy(() => {
+ this.manager.store.highlightNodes.forEach(node =>
+ node.calculateHighlightBox()
+ );
+ });
+ }
+ );
+ } else {
+ this.unWatchWidthChange[node.id]?.();
+ }
+ }
}
registerEditorPlugin(TablePlugin);
diff --git a/packages/amis-editor/src/tpl/common.tsx b/packages/amis-editor/src/tpl/common.tsx
index eb5674423..3ab2c3b21 100644
--- a/packages/amis-editor/src/tpl/common.tsx
+++ b/packages/amis-editor/src/tpl/common.tsx
@@ -79,27 +79,15 @@ setSchemaTpl('formItemName', {
// validateOnChange: false
});
-setSchemaTpl('formItemExtraName', {
- className: 'mb-3',
- type: 'fieldset',
- body: [
- getSchemaTpl('formItemName', {
- required: true,
- label: '额外字段',
- name: 'extraName',
- visibleOn: 'typeof this.extraName === "string"'
- }),
-
- {
- type: 'switch',
- label: tipedLabel('存成两个字段', '开启后将选中范围分别存成两个字段'),
- name: 'extraName',
- pipeIn: (value: any) => typeof value === 'string',
- pipeOut: (value: any) => (value ? '' : undefined),
- inputClassName: 'is-inline'
- }
- ]
-});
+setSchemaTpl(
+ 'formItemExtraName',
+ getSchemaTpl('formItemName', {
+ required: false,
+ label: '结尾字段名',
+ name: 'extraName',
+ description: '配置了结尾字段名,该组件将开始和结尾存成两个字段'
+ })
+);
setSchemaTpl(
'formItemMode',
diff --git a/packages/amis-ui/scss/_mixins.scss b/packages/amis-ui/scss/_mixins.scss
index 1be6de704..ab7bdd5b8 100644
--- a/packages/amis-ui/scss/_mixins.scss
+++ b/packages/amis-ui/scss/_mixins.scss
@@ -203,7 +203,8 @@
&:hover:active {
@content;
}
- &.active {
+ &.active,
+ &.is-active {
@content;
}
}
diff --git a/packages/amis/__tests__/event-action/renderers/__snapshots__/crud.test.tsx.snap b/packages/amis/__tests__/event-action/renderers/__snapshots__/crud.test.tsx.snap
index e52d990aa..541f772e5 100644
--- a/packages/amis/__tests__/event-action/renderers/__snapshots__/crud.test.tsx.snap
+++ b/packages/amis/__tests__/event-action/renderers/__snapshots__/crud.test.tsx.snap
@@ -25,6 +25,7 @@ exports[`doAction:crud reload 1`] = `
+
@@ -294,6 +334,7 @@ exports[`doAction:crud reload with data1 1`] = `
+
@@ -563,6 +643,7 @@ exports[`doAction:crud reload with data2 1`] = `
+
diff --git a/packages/amis/__tests__/renderers/Form/__snapshots__/inputTable.test.tsx.snap b/packages/amis/__tests__/renderers/Form/__snapshots__/inputTable.test.tsx.snap
index 3e664707e..3c3e0c22f 100644
--- a/packages/amis/__tests__/renderers/Form/__snapshots__/inputTable.test.tsx.snap
+++ b/packages/amis/__tests__/renderers/Form/__snapshots__/inputTable.test.tsx.snap
@@ -224,6 +224,7 @@ exports[`Renderer:input table 1`] = `
>
+
@@ -550,6 +590,7 @@ exports[`Renderer:input table add 1`] = `
>
+
@@ -882,6 +962,7 @@ exports[`Renderer:input-table cell selects delete 1`] = `
+
@@ -1263,6 +1383,7 @@ exports[`Renderer:input-table with combo column 1`] = `
>
+
diff --git a/packages/amis/__tests__/renderers/__snapshots__/CRUD.test.tsx.snap b/packages/amis/__tests__/renderers/__snapshots__/CRUD.test.tsx.snap
index 7c0911445..5bcbec564 100644
--- a/packages/amis/__tests__/renderers/__snapshots__/CRUD.test.tsx.snap
+++ b/packages/amis/__tests__/renderers/__snapshots__/CRUD.test.tsx.snap
@@ -17,6 +17,7 @@ exports[`1. Renderer:crud basic interval headerToolbar footerToolbar 1`] = `
>
+
@@ -2108,6 +2148,7 @@ exports[`6. Renderer:crud source & alwaysShowPagination 1`] = `
>
+
@@ -2626,6 +2706,7 @@ exports[`13. enderer: crud keepItemSelectionOnPageChange & maxKeepItemSelectionL
>
+
diff --git a/packages/amis/__tests__/renderers/__snapshots__/Pagination.test.tsx.snap b/packages/amis/__tests__/renderers/__snapshots__/Pagination.test.tsx.snap
index a28931759..60900d830 100644
--- a/packages/amis/__tests__/renderers/__snapshots__/Pagination.test.tsx.snap
+++ b/packages/amis/__tests__/renderers/__snapshots__/Pagination.test.tsx.snap
@@ -7,6 +7,7 @@ exports[`Renderer:Pagination 1`] = `
>
+
diff --git a/packages/amis/__tests__/renderers/__snapshots__/Table.test.tsx.snap b/packages/amis/__tests__/renderers/__snapshots__/Table.test.tsx.snap
index 4d3e6bfce..cbca46da7 100644
--- a/packages/amis/__tests__/renderers/__snapshots__/Table.test.tsx.snap
+++ b/packages/amis/__tests__/renderers/__snapshots__/Table.test.tsx.snap
@@ -1,7 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Renderer:table 1`] = `
-
+
@@ -325,11 +327,52 @@ exports[`Renderer:table 1`] = `
+
`;
exports[`Renderer:table align 1`] = `
-
+
@@ -676,6 +719,45 @@ exports[`Renderer:table align 1`] = `
+
`;
@@ -696,6 +778,7 @@ exports[`Renderer:table children 1`] = `
>
+
@@ -1261,7 +1383,9 @@ exports[`Renderer:table children 1`] = `
`;
exports[`Renderer:table classNameExpr 1`] = `
-
+
@@ -1585,11 +1709,52 @@ exports[`Renderer:table classNameExpr 1`] = `
+
`;
exports[`Renderer:table column head style className 1`] = `
-
+
@@ -1924,6 +2089,45 @@ exports[`Renderer:table column head style className 1`] = `
+
`;
@@ -1944,6 +2148,7 @@ exports[`Renderer:table combine Renderer:table combineNum only 1`] = `
>
+
@@ -2481,6 +2725,7 @@ exports[`Renderer:table combine Renderer:table combineNum with fromIndex 1`] = `
>
+
@@ -3088,6 +3372,7 @@ exports[`Renderer:table groupName-default 1`] = `
>
+
@@ -3829,6 +4153,7 @@ exports[`Renderer:table groupName-middleNoGroupName 1`] = `
>
+
@@ -4574,6 +4938,7 @@ exports[`Renderer:table groupName-startNoGroupName 1`] = `
>
+
@@ -5307,6 +5711,7 @@ exports[`Renderer:table groupName-withTpl 1`] = `
>
+
@@ -6032,7 +6476,9 @@ exports[`Renderer:table groupName-withTpl 1`] = `
`;
exports[`Renderer:table isHead fixed 1`] = `
-
+
@@ -6367,11 +6813,52 @@ exports[`Renderer:table isHead fixed 1`] = `
+
`;
exports[`Renderer:table list 1`] = `
-
+
@@ -7559,5 +8046,44 @@ exports[`Renderer:table list 1`] = `
+
`;
diff --git a/packages/amis/src/renderers/Table/index.tsx b/packages/amis/src/renderers/Table/index.tsx
index ac21fd28c..f315d7fb8 100644
--- a/packages/amis/src/renderers/Table/index.tsx
+++ b/packages/amis/src/renderers/Table/index.tsx
@@ -65,6 +65,7 @@ import {isMobile} from 'amis-core';
import isPlainObject from 'lodash/isPlainObject';
import omit from 'lodash/omit';
import ColGroup from './ColGroup';
+import debounce from 'lodash/debounce';
/**
* 表格列,不指定类型时默认为文本类型。
@@ -513,6 +514,10 @@ export default class Table extends React.Component {
subForms: any = {};
timer: ReturnType;
toDispose: Array<() => void> = [];
+ updateTableInfoLazy = debounce(this.updateTableInfo.bind(this), 250, {
+ trailing: true,
+ leading: false
+ });
constructor(props: TableProps, context: IScopedContext) {
super(props);
@@ -524,6 +529,7 @@ export default class Table extends React.Component {
this.tableRef = this.tableRef.bind(this);
this.affixedTableRef = this.affixedTableRef.bind(this);
this.updateTableInfo = this.updateTableInfo.bind(this);
+ this.updateTableInfoRef = this.updateTableInfoRef.bind(this);
this.handleAction = this.handleAction.bind(this);
this.handleCheck = this.handleCheck.bind(this);
this.handleCheckAll = this.handleCheckAll.bind(this);
@@ -661,6 +667,9 @@ export default class Table extends React.Component {
this.updateAutoFillHeight();
}
+ this.toDispose.push(
+ resizeSensor(currentNode.parentElement!, this.updateTableInfoLazy)
+ );
const {store, autoGenerateFilter, onSearchableFromInit} = this.props;
// autoGenerateFilter 开启后
@@ -845,6 +854,7 @@ export default class Table extends React.Component {
this.toDispose.forEach(fn => fn());
this.toDispose = [];
+ this.updateTableInfoLazy.cancel();
formItem && isAlive(formItem) && formItem.setSubStore(null);
clearTimeout(this.timer);
@@ -1169,11 +1179,19 @@ export default class Table extends React.Component {
return store.selectedRows.map(item => item.data);
}
- updateTableInfo(ref: any) {
- if (!ref) {
+ updateTableInfo(callback?: () => void) {
+ if (this.resizeLine) {
return;
}
this.props.store.syncTableWidth();
+ callback && setTimeout(callback, 20);
+ }
+
+ updateTableInfoRef(ref: any) {
+ if (!ref) {
+ return;
+ }
+ this.updateTableInfo();
}
// 当表格滚动是,需要让 affixHeader 部分的表格也滚动
@@ -2850,7 +2868,9 @@ export default class Table extends React.Component {
{
// 利用这个将 table-layout: auto 转成 table-layout: fixed
- store.columnWidthReady ? null :
+ store.columnWidthReady ? null : (
+
+ )
}