Skip to content

Commit 5e5ac47

Browse files
authored
Merge pull request #108 from NadavShaar/issue-#107
2 parents c71ff5b + be1d680 commit 5e5ac47

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const columns = [
112112
id: 3,
113113
field: 'last_visited',
114114
label: 'Last Visited',
115-
sort: ({a, b, isAscending}) => {
115+
sort: ({ a, b, isAscending }) => {
116116
let aa = a.split('/').reverse().join(),
117117
bb = b.split('/').reverse().join();
118118
return aa < bb ? isAscending ? -1 : 1 : (aa > bb ? isAscending ? 1 : -1 : 0);
@@ -122,7 +122,7 @@ const columns = [
122122
id: 4,
123123
field: 'test',
124124
label: 'Score',
125-
getValue: ({value, column}) => value.x + value.y
125+
getValue: ({ value }) => value.x + value.y
126126
}
127127
];
128128

@@ -275,7 +275,7 @@ Each column (except for '[checkbox](#checkbox-column)' column) has support for t
275275
className: '',
276276
pinned: false,
277277
width: '200px',
278-
getValue: ({value, column}) => value,
278+
getValue: ({ tableManager, value, column, rowData }) => value,
279279
setValue: ({ value, data, setRow, column }) => { setRow({ ...data, [column.field]: value}) },
280280
minResizeWidth: 70,
281281
maxResizeWidth: null,
@@ -353,7 +353,9 @@ Each row should have a unique identifier field, which by default is `id`, but it
353353
}
354354
```
355355

356-
**Note:** If a property value is not of type string, you'll have to use the `getValue` function on the column in order to extract the desired value.
356+
**Note:** If a property value is not of type string, or in cases you don't specify a field for the column, you'll have to use the `getValue` function on the column in order to extract the desired value.
357+
358+
**Signature**: getValue: ({ tableManager, value, column, rowData }) => string
357359

358360
**Example:**
359361

@@ -363,7 +365,7 @@ Let's say the field's value for a cell is an object:
363365

364366
Its `getValue` function for displaying the first and last name as a full name, would be:
365367

366-
`getValue: ({value, column}) => value.firstName + ' ' + value.lastName`
368+
`getValue: ({ value }) => value.firstName + ' ' + value.lastName`
367369

368370
The returned value will be used for searching, sorting etc...
369371

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nadavshaar/react-grid-table",
3-
"version": "1.1.0",
3+
"version": "1.1.2",
44
"description": "A modular table, based on a CSS grid layout, optimized for customization.",
55
"author": "Nadav Shaar",
66
"license": "MIT",

src/components/Cell.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22

3-
const Cell = ({ value, tableManager }) => {
3+
const Cell = ({ value, textValue, tableManager }) => {
44
const {
55
config: {
66
additionalProps: { cell: additionalProps = {} },
@@ -12,7 +12,7 @@ const Cell = ({ value, tableManager }) => {
1212
).trim();
1313

1414
return (
15-
<div {...additionalProps} className={classNames} title={value}>
15+
<div {...additionalProps} className={classNames} title={textValue}>
1616
{value}
1717
</div>
1818
);

src/components/CellContainer.jsx

+24-12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ const CellContainer = ({
6565
return classNames;
6666
};
6767

68+
const textValue = useMemo(
69+
() =>
70+
data &&
71+
column
72+
.getValue?.({
73+
tableManager,
74+
value: isEdit ? editRow[column.field] : data[column.field],
75+
column,
76+
rowData: data,
77+
})
78+
?.toString?.(),
79+
[column, data, editRow, isEdit, tableManager]
80+
);
81+
6882
const getValue = () => {
6983
let value;
7084

@@ -73,17 +87,7 @@ const CellContainer = ({
7387
value = isSelected;
7488
break;
7589
default:
76-
value =
77-
data &&
78-
column
79-
.getValue?.({
80-
tableManager,
81-
value: isEdit
82-
? editRow[column.field]
83-
: data[column.field],
84-
column,
85-
})
86-
?.toString?.();
90+
value = textValue;
8791
if (
8892
!isEdit &&
8993
highlightSearch &&
@@ -129,7 +133,15 @@ const CellContainer = ({
129133
let classNames = getClassNames();
130134
let value = getValue();
131135

132-
let cellProps = { tableManager, value, data, column, colIndex, rowIndex };
136+
let cellProps = {
137+
tableManager,
138+
value,
139+
textValue,
140+
data,
141+
column,
142+
colIndex,
143+
rowIndex,
144+
};
133145
const isFirstEditableCell = useMemo(
134146
() =>
135147
visibleColumns.findIndex(

src/hooks/useSearch.jsx

+19-11
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,40 @@ const useSearch = (props, tableManager) => {
3737

3838
searchApi.searchRows = useCallback(
3939
(rows) => {
40-
var cols = columns.reduce((cols, coldef) => {
41-
cols[coldef.field] = coldef;
42-
return cols;
43-
}, {});
44-
4540
if (searchApi.validSearchText) {
4641
rows = rows.filter((item) =>
4742
Object.keys(item).some((key) => {
48-
if (cols[key] && cols[key].searchable) {
49-
const value = cols[key].getValue({
43+
var cols = columns.filter(
44+
(column) =>
45+
column.searchable && column.field === key
46+
);
47+
48+
let isValid = false;
49+
50+
for (let index = 0; index < cols.length; index++) {
51+
const currentColumn = cols[index];
52+
const value = currentColumn.getValue({
53+
tableManager,
5054
value: item[key],
51-
column: cols[key],
55+
column: currentColumn,
56+
rowData: item,
5257
});
53-
return cols[key].search({
58+
isValid = currentColumn.search({
5459
value: value?.toString() || "",
5560
searchText: searchApi.validSearchText,
5661
});
62+
63+
if (isValid) break;
5764
}
58-
return false;
65+
66+
return isValid;
5967
})
6068
);
6169
}
6270

6371
return rows;
6472
},
65-
[columns, searchApi.validSearchText]
73+
[columns, searchApi.validSearchText, tableManager]
6674
);
6775

6876
return searchApi;

src/hooks/useSort.jsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ const useSort = (props, tableManager) => {
4141
rows = [...rows];
4242
rows.sort((a, b) => {
4343
const aVal = cols[sortApi.sort.colId].getValue({
44+
tableManager,
4445
value: a[cols[sortApi.sort.colId].field],
4546
column: cols[sortApi.sort.colId],
47+
rowData: a,
4648
});
4749
const bVal = cols[sortApi.sort.colId].getValue({
50+
tableManager,
4851
value: b[cols[sortApi.sort.colId].field],
4952
column: cols[sortApi.sort.colId],
53+
rowData: b,
5054
});
5155

5256
if (cols[sortApi.sort.colId].sortable === false) return 0;
@@ -60,7 +64,7 @@ const useSort = (props, tableManager) => {
6064

6165
return rows;
6266
},
63-
[sortApi.sort, columns]
67+
[sortApi.sort, columns, tableManager]
6468
);
6569

6670
sortApi.toggleSort = (colId) => {

0 commit comments

Comments
 (0)