MCPcopy Create free account
hub / github.com/ElemeFE/element-react / caculateWidth

Method caculateWidth

src/table/TableLayout.jsx:106–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

104
105 // horizontal direction layout
106 caculateWidth(): Object {
107 const { store: { columns, fixedColumns, rightFixedColumns }, fit } = this.props;
108 const { gutterWidth } = this.state;
109 const bodyMinWidth = columns.reduce((pre, col) => pre + (col.width || col.minWidth), 0);
110
111 let bodyWidth = this.table.el.clientWidth;
112 let scrollX;
113 let fixedWidth;
114 let rightFixedWidth;
115
116 // mutate props (TableStore's state[columns])
117 const flexColumns = columns.filter(column => typeof column.width !== 'number');
118 if (flexColumns.length && fit) {
119 if (bodyMinWidth < bodyWidth - gutterWidth) { // no scroll bar
120 scrollX = false;
121
122 const totalFlexWidth = bodyWidth - gutterWidth - bodyMinWidth;
123 if (flexColumns.length === 1) {
124 flexColumns[0].realWidth = flexColumns[0].minWidth + totalFlexWidth;
125 } else {
126 const allColumnsWidth = flexColumns.reduce((pre, col) => pre + col.minWidth, 0);
127 const flexWidthPerPixel = totalFlexWidth / allColumnsWidth;
128
129 let widthWithoutFirst = 0;
130
131 flexColumns.forEach((column, index) => {
132 if (index === 0) return;
133 const flexWidth = Math.floor(column.minWidth * flexWidthPerPixel);
134 widthWithoutFirst += flexWidth;
135 column.realWidth = column.minWidth + flexWidth;
136 });
137
138 flexColumns[0].realWidth = flexColumns[0].minWidth + totalFlexWidth - widthWithoutFirst;
139 }
140 } else { // have horizontal scroll bar
141 scrollX = true;
142 flexColumns.forEach((column) => {
143 column.realWidth = column.minWidth;
144 });
145 }
146
147 bodyWidth = Math.max(bodyMinWidth, bodyWidth);
148 } else {
149 scrollX = bodyMinWidth > bodyWidth;
150 bodyWidth = bodyMinWidth;
151 }
152
153 if (fixedColumns.length) {
154 fixedWidth = fixedColumns.reduce((pre, col) => pre + col.realWidth, 0);
155 }
156
157 if (rightFixedColumns.length) {
158 rightFixedWidth = rightFixedColumns.reduce((pre, col) => pre + col.realWidth, 0);
159 }
160
161 return {
162 scrollX,
163 bodyWidth,

Callers 1

scheduleLayoutMethod · 0.95

Calls 2

maxMethod · 0.80
filterMethod · 0.45

Tested by

no test coverage detected