Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: table/list keyAttribute error if not unique (fixes SFKUI-6936) #284

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions etc/vue.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7798,6 +7798,7 @@ callbackBeforeItemDelete(item: ListItem): void;
escapeNewlines(value: string): string;
updateActiveRowFromVModel(): void;
setActiveRow(row: ListItem | undefined): void;
confirmUniqueKey(): void;
}, ComponentOptions, ComponentOptionsMixin, ("change" | "click" | "select" | "collapse" | "update:modelValue" | "expand" | "unselect" | "update:active")[], "change" | "click" | "select" | "collapse" | "update:modelValue" | "expand" | "unselect" | "update:active", PublicProps, Readonly<ExtractPropTypes< {
rows: {
type: PropType<ListArray>;
Expand Down
14 changes: 14 additions & 0 deletions packages/vue/src/components/FDataTable/FDataTable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ it("should call provided sort method when clicking columnheader that is registra
`);
});

it("should throw error if `keyAttribute` is not unique", async () => {
expect.assertions(1);
expect(() => {
mount(FDataTable, {
props: {
rows: [{ id: "a" }, { id: "b" }, { id: "b" }],
keyAttribute: "id",
},
});
}).toThrowErrorMatchingInlineSnapshot(
`"Expected each table row to have a unique key attribute but encountered duplicate of "b" in row index 2."`,
);
});

describe("html-validate", () => {
it("should require non-empty key-attribute attribute", () => {
expect.assertions(2);
Expand Down
20 changes: 20 additions & 0 deletions packages/vue/src/components/FDataTable/FDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ export default defineComponent({
return this.scroll !== TableScroll.NONE ? 0 : undefined;
},
},
watch: {
rows: {
deep: true,
immediate: true,
handler: function () {
const seenKeys: Record<string, boolean> = {};
for (const row of this.rows) {
const rowKey = String(row[this.keyAttribute]);
if (seenKeys[rowKey]) {
const index = this.rows.indexOf(row);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Väldigt sällan jag rekommenderar .forEach() men här känns som en ok fall:

this.rows.forEach((row, index) => {

});

Så slipper vi scanna tabellen två gånger för att hitta index (en gång för for-slingar, en gång för indexOf)

Alternativt

for (const index in this.rows) {
    const row = this.rows[index];
}

throw new Error(
`Expected each table row to have a unique key attribute but encountered duplicate of "${rowKey}" in row index ${index}.`,
);
}

seenKeys[rowKey] = true;
}
},
},
},
mounted() {
this.registerCallbackOnSort(this.callbackOnSort);
this.registerCallbackOnMount(this.callbackSortableColumns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,37 @@ it("should call provided sort method when clicking columnheader that is registra
`);
});

describe("`keyAttribute` prop", () => {
it("should throw error if not unique in row", async () => {
expect.assertions(1);
expect(() => {
mount(FInteractiveTable, {
props: {
rows: [{ id: "a" }, { id: "b" }, { id: "b" }],
keyAttribute: "id",
},
});
}).toThrowErrorMatchingInlineSnapshot(
`"Expected each row to have a unique key attribute but encountered duplicate of "b" in row index 2."`,
);
});

it("should throw error if not unique in expandable row", async () => {
expect.assertions(1);
expect(() => {
mount(FInteractiveTable, {
props: {
rows: [{ id: "a" }, { id: "b", more: [{ id: "a" }] }],
keyAttribute: "id",
expandableAttribute: "more",
},
});
}).toThrowErrorMatchingInlineSnapshot(
`"Expected each row to have a unique key attribute but encountered duplicate of "a" in expandable row index 0 of row index 1."`,
);
});
});

describe("Expandable rows", () => {
it("Should handle slot content in expanded rows", () => {
expect.assertions(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ export default defineComponent({
return includeItem(row, this.rows, this.keyAttribute);
});
}

this.confirmUniqueKey();
},
},
active: {
Expand Down Expand Up @@ -616,6 +618,49 @@ export default defineComponent({
*/
this.$emit("update:active", this.activeRow);
},
confirmUniqueKey(): void {
const seenKeys: Record<string, boolean> = {};

for (const row of this.rows) {
const rowKey = String(row[this.keyAttribute]);
if (seenKeys[rowKey]) {
const index = this.rows.indexOf(row);
const msg =
`Expected each row to have a unique key attribute but ` +
`encountered duplicate of "${rowKey}" in row index ${index}.`;

throw new Error(msg);
}

seenKeys[rowKey] = true;
}

if (!this.isExpandableTable) {
return;
}

for (const row of this.rows) {
const expandableRows = this.expandableRows(row);
if (expandableRows === undefined) {
continue;
}

for (const expandableRow of expandableRows) {
const rowKey = String(expandableRow[this.keyAttribute]);
if (seenKeys[rowKey]) {
const index = this.rows.indexOf(row);
const expandableIndex = expandableRows.indexOf(expandableRow);
const msg =
`Expected each row to have a unique key attribute but encountered duplicate` +
` of "${rowKey}" in expandable row index ${expandableIndex} of row index ${index}.`;

throw new Error(msg);
}

seenKeys[rowKey] = true;
}
}
},
},
});
</script>
16 changes: 16 additions & 0 deletions packages/vue/src/components/FList/FList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,22 @@ describe("screenreader slot", () => {
});
});

describe("`keyAttribute` prop", () => {
it("should throw error if `keyAttribute` is not unique", async () => {
expect.assertions(1);
expect(() => {
mount(FList, {
props: {
items: [{ id: "a" }, { id: "b" }, { id: "b" }],
keyAttribute: "id",
},
});
}).toThrowErrorMatchingInlineSnapshot(
`"Expected each list item to have a unique key attribute but encountered duplicate of "b" in item index 2."`,
);
});
});

describe("html-validate", () => {
it("should require non-empty key-attribute attribute", () => {
expect.assertions(2);
Expand Down
13 changes: 13 additions & 0 deletions packages/vue/src/components/FList/FList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ export default defineComponent({
immediate: true,
handler: function () {
this.updateSelectedItemsFromVModel();

const seenKeys: Record<string, boolean> = {};
for (const item of this.items) {
const rowKey = String(item[this.keyAttribute]);
if (seenKeys[rowKey]) {
const index = this.items.indexOf(item);
throw new Error(
`Expected each list item to have a unique key attribute but encountered duplicate of "${rowKey}" in item index ${index}.`,
);
}

seenKeys[rowKey] = true;
}
},
},
modelValue: {
Expand Down
Loading