Skip to content

Commit a3ddb73

Browse files
committed
feat: use single unique id generator
1 parent 2851630 commit a3ddb73

File tree

17 files changed

+96
-89
lines changed

17 files changed

+96
-89
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/components/base/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@react-ck/icon": "^4.3.2",
3939
"@react-ck/keyboard-controls": "^1.1.0",
4040
"@react-ck/layers": "^1.4.15",
41+
"@react-ck/manager": "^1.3.26",
4142
"@react-ck/react-utils": "^2.0.4",
4243
"@react-ck/scss-utils": "^1.1.13",
4344
"@react-ck/snackbar-provider": "^1.0.7",

packages/components/base/src/dropdown/specs/index.unit.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { act } from "react";
22
import { Dropdown } from "../Dropdown";
33
import { render, screen } from "@testing-library/react";
44
import "@testing-library/jest-dom";
5-
// eslint-disable-next-line workspaces/no-relative-imports, workspaces/require-dependency -- this is a test
5+
// eslint-disable-next-line workspaces/no-relative-imports -- this is a test
66
import { Manager } from "../../../../../providers/manager/src";
77

88
describe("unit Dropdown", () => {

packages/components/base/src/form-field/FormField.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import styles from "./styles/index.module.scss";
33
import classNames from "classnames";
44
import { Text } from "../text";
55
import { FormFieldContext, type FormFieldContextProps } from "./context";
6+
import { useManagerContext } from "@react-ck/manager";
67

78
/**
89
* Props interface for the FormField component.
@@ -79,9 +80,9 @@ export const FormField = ({
7980
reserveSpace,
8081
...otherProps
8182
}: Readonly<FormFieldProps>): React.ReactElement => {
82-
const generatedId = useMemo(() => `ff-${Math.random()}-${Date.now()}`, []);
83+
const { generateUniqueId } = useManagerContext();
8384

84-
const computedId = useMemo(() => id ?? generatedId, [id, generatedId]);
85+
const computedId = useMemo(() => id ?? generateUniqueId(), [id, generateUniqueId]);
8586

8687
const context = useMemo<FormFieldContextProps>(
8788
() => ({

packages/components/base/src/form-field/specs/__snapshots__/index.snapshot.test.tsx.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
exports[`snapshot FormField renders correctly 1`] = `
44
<DocumentFragment>
55
<div
6-
class="root skin_default variation_default"
6+
style="--react-ck-color-highlight-primary-light: hsl(185deg 50% 95%); --react-ck-color-highlight-primary: hsl(185deg 50% 55%); --react-ck-color-highlight-primary-dark: hsl(185deg 50% 35%); --react-ck-color-highlight-alt: #0052cc; --react-ck-color-highlight-alt-dark: #003e99; --react-ck-color-neutral-light-100: #fff; --react-ck-color-neutral-light-200: hsl(230deg 40% 76% / 10%); --react-ck-color-neutral-light-300: hsl(230deg 40% 76% / 20%); --react-ck-color-neutral-light-400: hsl(230deg 30% 46% / 20%); --react-ck-color-neutral-light-500: hsl(230deg 10% 2% / 25%); --react-ck-color-neutral-light-600: hsl(230deg 5% 1% / 35%); --react-ck-color-neutral-light-200-solid: #F9FAFB; --react-ck-color-neutral-light-300-solid: hsl(230deg 40% 95%); --react-ck-color-neutral-light-400-solid: hsl(230deg 30% 90%); --react-ck-color-neutral-light-500-solid: hsl(230deg 10% 80%); --react-ck-color-neutral-light-600-solid: hsl(230deg 5% 70%); --react-ck-color-neutral-dark-100: hsl(230deg 5% 60%); --react-ck-color-neutral-dark-200: hsl(230deg 5% 50%); --react-ck-color-neutral-dark-300: hsl(230deg 5% 40%); --react-ck-color-neutral-dark-400: hsl(230deg 15% 25%); --react-ck-color-neutral-dark-500: hsl(230deg 25% 15%); --react-ck-color-status-positive-light: #E1FCF0; --react-ck-color-status-positive: #00D891; --react-ck-color-status-positive-dark: #276749; --react-ck-color-status-average-light: #FFEDD5; --react-ck-color-status-average: #F59E0B; --react-ck-color-status-average-dark: #9b5c2c; --react-ck-color-status-negative-light: #FFF0F0; --react-ck-color-status-negative: #EF4444; --react-ck-color-status-negative-dark: #b71010; --react-ck-color-status-info-light: #BFDBFE; --react-ck-color-status-info: #3B82F6; --react-ck-color-status-info-dark: #0950c3; --react-ck-color-overlay-light: rgba(255,255,255, .4); --react-ck-color-overlay-dark: rgba(0,0,0,.2); --react-ck-color-focus: #2684FFB2; --react-ck-color-misc-1: #F28B82; --react-ck-color-misc-2: #FF8EA2; --react-ck-color-misc-3: #7EC9FF; --react-ck-color-misc-4: #BABAFD; --react-ck-color-misc-5: #90d585; --react-ck-font-family: system-ui, sans-serif; --react-ck-font-base-size: 14px; --react-ck-spacing-base: 8px; --react-ck-spacing-border: 1px;"
77
>
88
<div
9-
class="main_content"
9+
class="root skin_default variation_default"
1010
>
1111
<div
12-
class="input_wrapper"
12+
class="main_content"
1313
>
14-
FormField
14+
<div
15+
class="input_wrapper"
16+
>
17+
FormField
18+
</div>
1519
</div>
1620
</div>
1721
</div>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React from "react";
22
import { FormField } from "../index";
33
import { render } from "@testing-library/react";
4+
import { Manager } from "@react-ck/manager";
45

56
describe("snapshot FormField", () => {
67
it("renders correctly", () => {
7-
const tree = render(<FormField>FormField</FormField>).asFragment();
8+
const tree = render(
9+
<Manager>
10+
<FormField>FormField</FormField>
11+
</Manager>,
12+
).asFragment();
813
expect(tree).toMatchSnapshot();
914
});
1015
});

packages/components/base/src/form-field/specs/index.unit.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import React from "react";
22
import { FormField } from "../index";
33
import { render, screen } from "@testing-library/react";
44
import "@testing-library/jest-dom";
5+
import { Manager } from "@react-ck/manager";
56

67
describe("unit FormField", () => {
78
it("renders correctly", async () => {
89
const content = "FormField";
9-
render(<FormField>{content}</FormField>);
10+
render(
11+
<Manager>
12+
<FormField>{content}</FormField>
13+
</Manager>,
14+
);
1015
const find = await screen.findByText(content);
1116
expect(find).toBeInTheDocument();
1217
});

packages/components/base/src/select/specs/index.unit.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Select } from "../index";
33
import { render, screen, fireEvent } from "@testing-library/react";
44
import "@testing-library/jest-dom";
55
import { mockResizeObserver } from "./mocks";
6-
// eslint-disable-next-line workspaces/require-dependency -- testing purposes only
6+
77
import { Manager } from "@react-ck/manager/src";
88

99
/**

packages/components/base/src/skeleton/SkeletonTable.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React from "react";
1+
import React, { useMemo } from "react";
22
import classNames from "classnames";
33
import { Skeleton } from "./Skeleton";
44
import { Table, type TableProps } from "../table";
55
import { Card } from "../card";
6+
import { useManagerContext } from "@react-ck/manager";
67

78
/**
89
* Props for configuring the SkeletonTable component
@@ -23,20 +24,31 @@ export const SkeletonTable = ({
2324
columns = 4,
2425
className,
2526
...otherProps
26-
}: Readonly<SkeletonTableProps>): React.ReactElement => (
27-
<Card>
28-
<Table className={classNames(className)} {...otherProps}>
27+
}: Readonly<SkeletonTableProps>): React.ReactElement => {
28+
const { generateUniqueId } = useManagerContext();
29+
30+
const content = useMemo(
31+
() => (
2932
<Table.TBody>
3033
{Array.from({ length: rows }).map(() => (
31-
<Table.Tr key={`skeleton-row-${crypto.randomUUID()}`}>
34+
<Table.Tr key={`skeleton-row-${generateUniqueId()}`}>
3235
{Array.from({ length: columns }).map(() => (
33-
<Table.Td key={`skeleton-cell-${crypto.randomUUID()}`}>
36+
<Table.Td key={`skeleton-cell-${generateUniqueId()}`}>
3437
<Skeleton variation="text" />
3538
</Table.Td>
3639
))}
3740
</Table.Tr>
3841
))}
3942
</Table.TBody>
40-
</Table>
41-
</Card>
42-
);
43+
),
44+
[columns, generateUniqueId, rows],
45+
);
46+
47+
return (
48+
<Card>
49+
<Table className={classNames(className)} {...otherProps}>
50+
{content}
51+
</Table>
52+
</Card>
53+
);
54+
};

packages/components/base/src/snackbar/Snackbar.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useCallback, useMemo, useRef, useState } from "react";
2-
import { generateId } from "./utils";
32
import { Layer } from "@react-ck/layers";
43
import { SnackbarItem } from "./SnackbarItem";
54
import styles from "./styles/index.module.scss";
@@ -11,6 +10,7 @@ import {
1110
type Item,
1211
type AddOptions,
1312
} from "@react-ck/snackbar-provider";
13+
import { useManagerContext } from "@react-ck/manager";
1414

1515
const durationMap = new Map<NonNullable<AddOptions["duration"]>, number>([
1616
["short", 3000],
@@ -38,9 +38,11 @@ export const Snackbar = ({
3838
}: Readonly<SnackbarProps>): React.ReactElement => {
3939
const timeoutMap = useRef<Record<string, ReturnType<typeof setTimeout>>>({});
4040

41+
const { generateUniqueId } = useManagerContext();
42+
4143
const [items, setItems] = useState<Item[]>(
4244
initialItems?.map((elementCreator) => {
43-
const id = generateId();
45+
const id = generateUniqueId();
4446
const element = elementCreator(id);
4547

4648
return {
@@ -64,7 +66,7 @@ export const Snackbar = ({
6466

6567
const add = useCallback<SnackbarContextProps["add"]>(
6668
(elementCreator, options) => {
67-
const id = generateId();
69+
const id = generateUniqueId();
6870
const element = elementCreator(id);
6971

7072
setItems((v) => [
@@ -84,7 +86,7 @@ export const Snackbar = ({
8486

8587
return id;
8688
},
87-
[remove],
89+
[remove, generateUniqueId],
8890
);
8991

9092
const contextValue = useMemo(

0 commit comments

Comments
 (0)