From 46d39d38aa57ae4477d075d73cfc410516886c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Tue, 3 Jun 2025 17:08:39 +0800 Subject: [PATCH] refactor: use rc-util useId --- src/Select.tsx | 2 +- src/hooks/useId.ts | 32 ------------- tests/__snapshots__/Combobox.test.tsx.snap | 12 ++--- tests/__snapshots__/Multiple.test.tsx.snap | 24 +++++----- tests/__snapshots__/Select.test.tsx.snap | 56 +++++++++++----------- tests/__snapshots__/Tags.test.tsx.snap | 40 ++++++++-------- tests/__snapshots__/ssr.test.tsx.snap | 2 +- 7 files changed, 68 insertions(+), 100 deletions(-) delete mode 100644 src/hooks/useId.ts diff --git a/src/Select.tsx b/src/Select.tsx index 2d337a672..8bad7c365 100644 --- a/src/Select.tsx +++ b/src/Select.tsx @@ -49,7 +49,7 @@ import SelectContext from './SelectContext'; import type { SelectContextProps } from './SelectContext'; import useCache from './hooks/useCache'; import useFilterOptions from './hooks/useFilterOptions'; -import useId from './hooks/useId'; +import useId from '@rc-component/util/lib/hooks/useId'; import useOptions from './hooks/useOptions'; import useRefFunc from './hooks/useRefFunc'; import type { FlattenOptionData } from './interface'; diff --git a/src/hooks/useId.ts b/src/hooks/useId.ts deleted file mode 100644 index b06775c34..000000000 --- a/src/hooks/useId.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as React from 'react'; -import canUseDom from '@rc-component/util/lib/Dom/canUseDom'; - -let uuid = 0; - -/** Is client side and not jsdom */ -export const isBrowserClient = process.env.NODE_ENV !== 'test' && canUseDom(); - -/** Get unique id for accessibility usage */ -export function getUUID(): number | string { - let retId: string | number; - - // Test never reach - /* istanbul ignore if */ - if (isBrowserClient) { - retId = uuid; - uuid += 1; - } else { - retId = 'TEST_OR_SSR'; - } - - return retId; -} - -export default function useId(id?: string) { - // Inner id for accessibility usage. Only work in client side - const [innerId, setInnerId] = React.useState(); - React.useEffect(() => { - setInnerId(`rc_select_${getUUID()}`); - }, []); - return id || innerId; -} diff --git a/tests/__snapshots__/Combobox.test.tsx.snap b/tests/__snapshots__/Combobox.test.tsx.snap index 8efa34f80..173283c6a 100644 --- a/tests/__snapshots__/Combobox.test.tsx.snap +++ b/tests/__snapshots__/Combobox.test.tsx.snap @@ -15,13 +15,13 @@ exports[`Select.Combobox renders controlled correctly 1`] = ` >
1 @@ -22,7 +22,7 @@ exports[`Select.Basic does not filter when filterOption value is false 1`] = `
2 @@ -99,7 +99,7 @@ exports[`Select.Basic filterOption could be true as described in default value 1
Not Found @@ -123,13 +123,13 @@ exports[`Select.Basic no search 1`] = ` >
1 @@ -560,7 +560,7 @@ exports[`Select.Basic should contain falsy children 1`] = `
2 @@ -644,14 +644,14 @@ exports[`Select.Basic should render custom dropdown correctly 1`] = ` CUSTOM NODE
1 @@ -659,7 +659,7 @@ exports[`Select.Basic should render custom dropdown correctly 1`] = `
2 diff --git a/tests/__snapshots__/Tags.test.tsx.snap b/tests/__snapshots__/Tags.test.tsx.snap index 89c74d8a8..477783d55 100644 --- a/tests/__snapshots__/Tags.test.tsx.snap +++ b/tests/__snapshots__/Tags.test.tsx.snap @@ -77,15 +77,15 @@ exports[`Select.Tags OptGroup renders correctly 1`] = ` style="width: 0px;" >
"`; +exports[`Select.SSR should work 1`] = `"
"`;