Skip to content

Commit

Permalink
fix(slash): fix console warning when using Infos component when id is…
Browse files Browse the repository at this point in the history
… not provided

Ref: #648
  • Loading branch information
pplancq committed Jan 14, 2025
1 parent aba40e4 commit d5f9658
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions slash/react/src/Layout/Header/Infos/Infos.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Fragment, ReactNode } from "react";
import "@axa-fr/design-system-slash-css/dist/Layout/Header/Infos/Infos.scss";
import { getComponentClassName } from "../../../utilities";
import { generateId } from "../../../utilities/helpers/generateId";

import "@axa-fr/design-system-slash-css/dist/Layout/Header/Infos/Infos.scss";

const defaultClassName = "af-contrat";

Expand Down Expand Up @@ -28,7 +30,7 @@ const Infos = ({ infos, className, classModifier }: InfosProps) => {
<i className="glyphicon glyphicon-info-sign" />
<dl className={`${defaultClassName}__list`}>
{infos.map((info) => (
<Fragment key={`info-${info.id}`}>
<Fragment key={`info-${generateId(info)}`}>
<dt className={`${defaultClassName}__word`}>{info.word}</dt>
<dd className={`${defaultClassName}__def`}>{info.definition}</dd>
</Fragment>
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions slash/react/src/utilities/helpers/__tests__/generateId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { generateId } from "../generateId";

describe("generateKey", () => {
it("should generate a unique id for each object", () => {
const obj1 = {};
const obj2 = {};
const key1 = generateId(obj1);
const key2 = generateId(obj2);

expect(key1).toBe("id-1");
expect(key2).toBe("id-2");
});

it("should return the same id for the same object", () => {
const obj = {};
const key1 = generateId(obj);
const key2 = generateId(obj);

expect(key1).toBe(key2);
});
});
14 changes: 14 additions & 0 deletions slash/react/src/utilities/helpers/generateId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let counter = 1;

const map = new WeakMap<object, number>();

export const generateId = (item: object): string => {
if (!map.has(item)) {
map.set(item, counter);
counter += 1;

return generateId(item);
}

return `id-${map.get(item)}`;
};

0 comments on commit d5f9658

Please sign in to comment.