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 => - `` - ) - .join( + div.style.cssText += `visibility: hidden!important;`; + div.innerHTML = + `
${th.innerHTML}
${thead.outerHTML}
` + + `${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`] = ` >
+