fix: remove internalKeys from Selected row and Triggered row (#12655)

This commit is contained in:
Tolulope Adetula 2022-04-12 11:01:03 +01:00 committed by GitHub
parent ea71e5844e
commit 89fcc414e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 26 deletions

View File

@ -25,15 +25,16 @@ export default {
} }
const filteredTableData = const filteredTableData =
props.filteredTableData || props.sanitizedTableData || []; props.filteredTableData || props.sanitizedTableData || [];
const internalKeysToOmit = ["__originalIndex__", "__primaryKey__"];
if (selectedRowIndex === -1) { if (selectedRowIndex === -1) {
const emptyRow = { ...filteredTableData[0] }; const emptyRow = { ...filteredTableData[0] };
Object.keys(emptyRow).forEach((key) => { Object.keys(emptyRow).forEach((key) => {
emptyRow[key] = ""; emptyRow[key] = "";
}); });
return emptyRow; return _.omit(emptyRow, internalKeysToOmit);
} }
const selectedRow = { ...filteredTableData[selectedRowIndex] }; const selectedRow = { ...filteredTableData[selectedRowIndex] };
return selectedRow; return _.omit(selectedRow, internalKeysToOmit);
}, },
// //
getTriggeredRow: (props, moment, _) => { getTriggeredRow: (props, moment, _) => {
@ -43,15 +44,16 @@ export default {
? -1 ? -1
: parseInt(props.triggeredRowIndex); : parseInt(props.triggeredRowIndex);
const tableData = props.sanitizedTableData || []; const tableData = props.sanitizedTableData || [];
const internalKeysToOmit = ["__originalIndex__", "__primaryKey__"];
if (triggeredRowIndex === -1) { if (triggeredRowIndex === -1) {
const emptyRow = { ...tableData[0] }; const emptyRow = { ...tableData[0] };
Object.keys(emptyRow).forEach((key) => { Object.keys(emptyRow).forEach((key) => {
emptyRow[key] = ""; emptyRow[key] = "";
}); });
return emptyRow; return _.omit(emptyRow, internalKeysToOmit);
} }
const triggeredRow = { ...tableData[triggeredRowIndex] }; const triggeredRow = { ...tableData[triggeredRowIndex] };
return triggeredRow; return _.omit(triggeredRow, internalKeysToOmit);
}, },
// //
getSelectedRows: (props, moment, _) => { getSelectedRows: (props, moment, _) => {
@ -61,8 +63,9 @@ export default {
const filteredTableData = const filteredTableData =
props.filteredTableData || props.sanitizedTableData || []; props.filteredTableData || props.sanitizedTableData || [];
const selectedRows = selectedRowIndices.map( const internalKeysToOmit = ["__originalIndex__", "__primaryKey__"];
(ind) => filteredTableData[ind], const selectedRows = selectedRowIndices.map((ind) =>
_.omit(filteredTableData[ind], internalKeysToOmit),
); );
return selectedRows; return selectedRows;
}, },

View File

@ -1251,7 +1251,7 @@ describe("Validates Derived Properties", () => {
}); });
describe("Validate getSelectedRow function", () => { describe("Validate getSelectedRow function", () => {
it("Multple row selection, with selected rows", () => { it("Multiple row selection, with selected rows", () => {
const { getSelectedRow } = derivedProperty; const { getSelectedRow } = derivedProperty;
const input = { const input = {
multiRowSelection: true, multiRowSelection: true,
@ -1267,12 +1267,11 @@ describe("Validate getSelectedRow function", () => {
id: 234, id: 234,
name: "Jane Doe", name: "Jane Doe",
extra: "Extra2", extra: "Extra2",
__originalIndex__: 2,
}); });
}); });
it("Multple row selection, with no selected rows", () => { it("Multiple row selection, with no selected rows", () => {
const { getSelectedRow } = derivedProperty; const { getSelectedRows } = derivedProperty;
const input = { const input = {
multiRowSelection: true, multiRowSelection: true,
selectedRowIndices: [], selectedRowIndices: [],
@ -1283,12 +1282,7 @@ describe("Validate getSelectedRow function", () => {
{ id: 123, name: "John Doe", extra: "Extra1", __originalIndex__: 1 }, { id: 123, name: "John Doe", extra: "Extra1", __originalIndex__: 1 },
], ],
}; };
expect(getSelectedRow(input, moment, _)).toStrictEqual({ expect(getSelectedRows(input, moment, _)).toStrictEqual([]);
id: "",
name: "",
extra: "",
__originalIndex__: "",
});
}); });
it("Single row selection, with selected row", () => { it("Single row selection, with selected row", () => {
@ -1307,14 +1301,13 @@ describe("Validate getSelectedRow function", () => {
id: 234, id: 234,
name: "Jane Doe", name: "Jane Doe",
extra: "Extra2", extra: "Extra2",
__originalIndex__: 2,
}); });
}); });
it("Single row selection, without selected row", () => { it("Single row selection, without selected row", () => {
const { getSelectedRow } = derivedProperty; const { getSelectedRow } = derivedProperty;
const input = { const input = {
multiRowSelection: true, multiRowSelection: false,
selectedRowIndices: [], selectedRowIndices: [],
selectedRowIndex: -1, selectedRowIndex: -1,
sanitizedTableData: [ sanitizedTableData: [
@ -1328,7 +1321,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
@ -1349,7 +1341,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
it("Single row selection, with indices undefined", () => { it("Single row selection, with indices undefined", () => {
@ -1369,7 +1360,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
it("Single row selection, with invalid indices", () => { it("Single row selection, with invalid indices", () => {
@ -1389,7 +1379,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
it("Single row selection, with invalid indices", () => { it("Single row selection, with invalid indices", () => {
@ -1409,7 +1398,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
it("Single row selection, with invalid indices", () => { it("Single row selection, with invalid indices", () => {
@ -1429,7 +1417,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
it("Single row selection, with invalid indices", () => { it("Single row selection, with invalid indices", () => {
@ -1449,7 +1436,6 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "",
}); });
}); });
it("Single row selection, with invalid indices", () => { it("Single row selection, with invalid indices", () => {
@ -1469,7 +1455,25 @@ describe("Validate getSelectedRow function", () => {
id: "", id: "",
name: "", name: "",
extra: "", extra: "",
__originalIndex__: "", });
});
});
describe("Validate getSelectedRow function", () => {
it("Trigger row selection", () => {
const { getTriggeredRow } = derivedProperty;
const input = {
triggeredRowIndex: 0,
sanitizedTableData: [
{ id: 1234, name: "Jim Doe", extra: "", __originalIndex__: 0 },
{ id: 234, name: "Jane Doe", extra: "Extra2", __originalIndex__: 2 },
{ id: 123, name: "John Doe", extra: "Extra1", __originalIndex__: 1 },
],
};
expect(getTriggeredRow(input, moment, _)).toStrictEqual({
id: 1234,
name: "Jim Doe",
extra: "",
}); });
}); });
}); });