Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kne/info-page",
"version": "0.1.26",
"version": "0.1.27",
"description": "一般用在复杂的详情展示页面,InfoPage提供了一个标准的展示信息的格式",
"syntax": {
"esmodules": true
Expand Down
76 changes: 8 additions & 68 deletions src/TableView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Checkbox, Col, Empty, Row } from 'antd';
import { CheckOutlined } from '@ant-design/icons';
import classnames from 'classnames';
import get from 'lodash/get';
import formatView from '../formatView';
import computeColumnsValue, { computeDisplay, computeColumnsDisplay } from '../computeColumnsValue';
import { isEmpty } from '@kne/is-empty';
import style from './style.module.scss';

Expand All @@ -21,7 +21,7 @@ const TableView = p => {
},
p
);
const { className, dataSource, columns, rowKey, rowSelection, valueIsEmpty, emptyIsPlaceholder, placeholder, empty, onRowSelect, render, ...others } = props;
const { className, dataSource, columns, rowKey, rowSelection, valueIsEmpty, emptyIsPlaceholder, placeholder, empty, onRowSelect, render, context, sticky, ...others } = props;
const dataSourceMapRef = useRef(new Map());
const defaultSpan = useMemo(() => {
const assignedSpan = columns.reduce((a, b) => {
Expand All @@ -32,15 +32,16 @@ const TableView = p => {
return Math.round(Math.max(24 - assignedSpan, 0) / undistributedColCount);
}, [columns]);

const header = <Header {...props} defaultSpan={defaultSpan} colsSize={colsSize} setColsSize={setColsSize} />;
const header = <Header {...props} sticky={sticky} defaultSpan={defaultSpan} colsSize={colsSize} setColsSize={setColsSize} />;

const renderBody = dataSource => {
const renderBody = (dataSource, context) => {
const getId = item => get(item, typeof rowKey === 'function' ? rowKey(item) : rowKey);
return dataSource && dataSource.length > 0 ? (
dataSource.map(item => {
const id = getId(item);
dataSourceMapRef.current.set(id, item);
const isChecked = rowSelection?.selectedRowKeys && rowSelection.selectedRowKeys.indexOf(id) > -1;
const columnsValue = computeColumnsValue({ columns, emptyIsPlaceholder, valueIsEmpty, removeEmpty: false, dataSource: item, placeholder, context });
return (
<Row
wrap={false}
Expand Down Expand Up @@ -88,50 +89,8 @@ const TableView = p => {
)}
<Col flex={1}>
<Row className={classnames('info-page-table-row-content')} wrap={false}>
{columns.map(column => {
{columnsValue.map(column => {
const { name, span } = column;
const colItem = (item => {
const itemValue =
typeof column.getValueOf === 'function'
? column.getValueOf(item, {
dataSource,
columns,
column,
target: item
})
: get(item, column.name);

const displayValue = (value => {
if (typeof column.format === 'function') {
return column.format(value, {
dataSource,
columns,
column,
target: item
});
}
if (typeof column.format === 'string') {
const formatValue = formatView(value, column.format, {
dataSource,
columns,
column,
target: item
});
if (formatValue) {
return formatValue;
}
}
return value;
})(itemValue);

const itemIsEmpty = (column.valueIsEmpty || valueIsEmpty)(itemValue);

if (!(column.hasOwnProperty('emptyIsPlaceholder') ? column.emptyIsPlaceholder : emptyIsPlaceholder) && itemIsEmpty) {
return null;
}
return Object.assign({}, column, { isEmpty: itemIsEmpty, value: displayValue });
})(item);

return (
<Col
key={name}
Expand All @@ -143,26 +102,7 @@ const TableView = p => {
}}
className={classnames(style['col'], 'info-page-table-col')}
>
<span className={style['col-content']}>
{colItem.isEmpty
? typeof colItem.renderPlaceholder === 'function'
? colItem.renderPlaceholder({
column,
dataSource,
columns,
placeholder,
target: item
})
: colItem.placeholder || placeholder
: typeof colItem.render === 'function'
? colItem.render(colItem.value, {
column,
columns,
dataSource,
target: item
})
: colItem.value}
</span>
<span className={style['col-content']}>{computeDisplay({ column, placeholder, dataSource: item, context })}</span>
</Col>
);
})}
Expand All @@ -182,7 +122,7 @@ const TableView = p => {
return (
<div {...others} className={classnames(style['table'], 'info-page-table', className)}>
{header}
<div className={classnames('info-page-table-body')}>{renderBody(dataSource)}</div>
<div className={classnames('info-page-table-body')}>{renderBody(dataSource, context)}</div>
</div>
);
};
Expand Down
31 changes: 17 additions & 14 deletions src/computeColumnsValue.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import get from 'lodash/get';
import formatView from './formatView';

const computeColumnsValue = ({ columns, emptyIsPlaceholder, valueIsEmpty, dataSource }) => {
return columns
.map(item => {
const itemValue = typeof item.getValueOf === 'function' ? item.getValueOf(dataSource, { column: item }) : get(dataSource, item.name);
const computeColumnsValue = ({ columns, emptyIsPlaceholder, valueIsEmpty, removeEmpty = true, dataSource, context }) => {
return (output => (removeEmpty ? output.filter(item => !!item) : output))(
columns.map(item => {
const itemValue = typeof item.getValueOf === 'function' ? item.getValueOf(dataSource, { column: item, context }) : get(dataSource, item.name);
const displayValue = (value => {
if (typeof item.format === 'function') {
return item.format(value, { dataSource, column: item });
return item.format(value, { dataSource, column: item, context });
}
if (typeof item.format === 'string') {
const formatValue = formatView(value, item.format, { dataSource, column: item });
const formatValue = formatView(value, item.format, { dataSource, column: item, context });
if (formatValue) {
return formatValue;
}
Expand All @@ -25,7 +25,8 @@ const computeColumnsValue = ({ columns, emptyIsPlaceholder, valueIsEmpty, dataSo
(typeof item.display === 'function' &&
item.display(itemValue, {
dataSource,
column: item
column: item,
context
}) === false)
) {
return null;
Expand All @@ -37,29 +38,31 @@ const computeColumnsValue = ({ columns, emptyIsPlaceholder, valueIsEmpty, dataSo

return Object.assign({}, item, { isEmpty: itemIsEmpty, value: displayValue });
})
.filter(item => !!item);
);
};

export const computeDisplay = ({ column, dataSource, placeholder }) => {
export const computeDisplay = ({ column, dataSource, placeholder, context }) => {
return column.isEmpty
? typeof column.renderPlaceholder === 'function'
? column.renderPlaceholder({
column: column,
dataSource,
placeholder
placeholder,
context
})
: column.placeholder || placeholder
: typeof column.render === 'function'
? column.render(column.value, {
column,
dataSource
dataSource,
context
})
: column.value;
};

export const computeColumnsDisplay = ({ columns, emptyIsPlaceholder, valueIsEmpty, dataSource, placeholder }) => {
return computeColumnsValue({ columns, emptyIsPlaceholder, valueIsEmpty, dataSource }).map(column => {
return computeDisplay({ column, placeholder, dataSource });
export const computeColumnsDisplay = ({ columns, emptyIsPlaceholder, valueIsEmpty, removeEmpty, dataSource, placeholder, context }) => {
return computeColumnsValue({ columns, emptyIsPlaceholder, valueIsEmpty, removeEmpty, dataSource, context }).map(column => {
return computeDisplay({ column, placeholder, dataSource, context });
});
};

Expand Down