-
Notifications
You must be signed in to change notification settings - Fork 200
feat: force useId #741
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
feat: force useId #741
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,23 +40,23 @@ const useOriginId = getUseId(); | |
|
|
||
| export default useOriginId | ||
| ? // Use React `useId` | ||
| function useId(id?: string) { | ||
| function useId(id?: string, forceUseId?: boolean) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
建议修复(跨文件)--- a/src/Dom/focus.ts
+++ b/src/Dom/focus.ts
@@
- const id = useId();
+ const id = useId(undefined, true);Also applies to: 59-59 🤖 Prompt for AI Agents |
||
| const reactId = useOriginId(); | ||
|
|
||
| // Developer passed id is single source of truth | ||
| if (id) { | ||
| return id; | ||
| } | ||
|
|
||
| // Test env always return mock id | ||
| if (process.env.NODE_ENV === 'test') { | ||
| // By default, test env always return mock id, but also allow force use id for test case which need to test id logic. | ||
| if (!forceUseId && process.env.NODE_ENV === 'test') { | ||
|
|
||
| return 'test-id'; | ||
|
|
||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new logic is not covered by tests. Please add tests to verify that |
||
|
|
||
| return reactId; | ||
| } | ||
| : // Use compatible of `useId` | ||
| function useCompatId(id?: string) { | ||
| function useCompatId(id?: string, forceUseId?: boolean) { | ||
| // Inner id for accessibility usage. Only work in client side | ||
|
|
||
| const [innerId, setInnerId] = React.useState<string>('ssr-id'); | ||
|
|
||
|
|
@@ -72,8 +72,8 @@ export default useOriginId | |
| return id; | ||
| } | ||
|
|
||
| // Test env always return mock id | ||
| if (process.env.NODE_ENV === 'test') { | ||
| // By default, test env always return mock id, but also allow force use id for test case which need to test id logic. | ||
| if (!forceUseId && process.env.NODE_ENV === 'test') { | ||
|
|
||
| return 'test-id'; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter name
forceUseIdcan be a bit confusing. Its purpose is to bypass the mock ID in test environments, but the name might suggest it's forcing the use of theuseIdhook itself. A more descriptive name likeforceActualIdorskipMockwould improve clarity. This change should be applied consistently to bothuseIdanduseCompatIdand their usages.