Skip to content

Commit 6728a40

Browse files
authored
Merge pull request #502 from kne-union/release
Release
2 parents 8a102cd + 1188b33 commit 6728a40

File tree

4 files changed

+67
-23
lines changed

4 files changed

+67
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kne-components/components-core",
3-
"version": "0.4.14",
3+
"version": "0.4.15",
44
"files": [
55
"build"
66
],

src/components/Table/Table.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import useColumnTypeProps from "@components/Table/useColumnTypeProps";
1010
import useGroupHeader from "./useGroupHeader";
1111
import useSort from "./useSort";
1212
import useRefCallback from "@kne/use-ref-callback";
13+
import useSelectedRow from "./useSelectedRow";
14+
import TablePage from './TablePage';
1315

1416
const Table = ({
1517
columns,
@@ -132,4 +134,7 @@ const Table = ({
132134
</div>);
133135
};
134136

137+
Table.useSelectedRow = useSelectedRow;
138+
Table.TablePage = TablePage;
139+
135140
export default Table;

src/components/Table/TablePage.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,23 @@ const TablePageInner = withFetch(({
7777
/>
7878
</>),
7979
current: get(requestParams, [pagination.paramsType, pagination.currentName], 1),
80-
pageSize: pagination.pageSize,
81-
onChange: pagination.onChange ? pagination.onChange : (page, size) => {
82-
if (page !== get(requestParams, [pagination.paramsType, pagination.currentName], 1)) {
83-
(pagination.requestType === "refresh" ? refresh : reload)({
84-
[pagination.paramsType]: {
85-
[pagination.currentName]: page, [pagination.pageSizeName]: size,
86-
},
87-
});
88-
} else {
89-
pagination.onShowSizeChange && pagination.onShowSizeChange(page, size);
90-
}
80+
pageSize: get(requestParams, [pagination.paramsType, pagination.pageSizeName], 20),
81+
onChange: (page, size) => {
82+
(() => {
83+
if (typeof pagination.onChange === 'function') {
84+
pagination.onChange(page, size);
85+
return;
86+
}
87+
if (page !== get(requestParams, [pagination.paramsType, pagination.currentName], 1)) {
88+
(pagination.requestType === "refresh" ? refresh : reload)({
89+
[pagination.paramsType]: {
90+
[pagination.currentName]: page, [pagination.pageSizeName]: size,
91+
},
92+
});
93+
} else {
94+
pagination.onShowSizeChange && pagination.onShowSizeChange(page, size);
95+
}
96+
})();
9197
getScrollEl().scrollTop = 0;
9298
},
9399
size: pagination.size,
@@ -98,7 +104,7 @@ const TablePageInner = withFetch(({
98104
};
99105

100106
return (<IntlProvider importMessages={importMessages} moduleName="Table">
101-
<FeaturesColumnsConfig id={featureId} columns={columns}>
107+
<FeaturesColumnsConfig id={featureId} columns={typeof columns === 'function' ? columns(data) : columns}>
102108
{({columns}) => (<Table
103109
{...Object.assign({}, props, tableProps)}
104110
sticky={sticky}
@@ -111,15 +117,7 @@ const TablePageInner = withFetch(({
111117
}}
112118
summary={typeof summary === "function" ? (...args) => {
113119
return summary(Object.assign({}, {
114-
data,
115-
fetchProps,
116-
requestParams,
117-
refresh,
118-
reload,
119-
loadMore,
120-
send,
121-
dataFormat,
122-
pagination,
120+
data, fetchProps, requestParams, refresh, reload, loadMore, send, dataFormat, pagination,
123121
}, ...args));
124122
} : null}
125123
/>)}
@@ -136,7 +134,6 @@ const TablePage = forwardRef(({pagination, ...props}, ref) => {
136134
requestType: "reload",
137135
currentName: "currentPage",
138136
pageSizeName: "perPage",
139-
pageSize: 20, //size: "small",
140137
}, pagination);
141138
const pageSizeKey = `${(props.name || "common").toUpperCase()}_TABLE_PAGE_SIZE`;
142139
const [pageSize, setPageSize] = useState(localStorage.getItem(pageSizeKey) || pagination.pageSize);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {useState} from 'react';
2+
import uniq from "lodash/uniq";
3+
4+
const useSelectedRow = (options) => {
5+
const {rowKey} = Object.assign({}, {rowKey: 'id'}, options);
6+
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
7+
8+
const getRowId = (item) => typeof rowKey === 'function' ? rowKey(item) : item[rowKey];
9+
10+
return {
11+
type: 'checkbox', selectedRowKeys, onSelectAll: (type, selected, items) => {
12+
const ids = items.map(getRowId);
13+
if (type) {
14+
setSelectedRowKeys(value => {
15+
return uniq([...value, ...ids]);
16+
});
17+
} else {
18+
setSelectedRowKeys(value => {
19+
return value.filter(item => {
20+
return ids.indexOf(item) === -1;
21+
});
22+
});
23+
}
24+
}, onSelect: (item, type) => {
25+
if (type) {
26+
setSelectedRowKeys(value => {
27+
const newValue = value.slice(0);
28+
newValue.push(getRowId(item));
29+
return newValue;
30+
});
31+
} else {
32+
setSelectedRowKeys(value => {
33+
const newValue = value.slice(0);
34+
newValue.splice(newValue.indexOf(getRowId(item)), 1);
35+
return newValue;
36+
});
37+
}
38+
}, setSelectedRowKeys
39+
};
40+
};
41+
42+
export default useSelectedRow;

0 commit comments

Comments
 (0)