-
Notifications
You must be signed in to change notification settings - Fork 199
feat: support getId #724
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: support getId #724
Changes from 1 commit
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,21 @@ export function resetUuid() { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Generate a valid HTML id from prefix and key. | ||||||||||||||||||||||||||||||
| * Sanitizes the key by replacing invalid characters with hyphens. | ||||||||||||||||||||||||||||||
| * @param prefix - The prefix for the id | ||||||||||||||||||||||||||||||
| * @param key - The key from React element, may contain spaces or invalid characters | ||||||||||||||||||||||||||||||
| * @returns A valid HTML id string | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| export function getId(prefix: string, key: string): string { | ||||||||||||||||||||||||||||||
| // Valid id characters: letters, digits, hyphen, underscore, colon, period | ||||||||||||||||||||||||||||||
| // Replace all invalid characters (including spaces) with hyphens to preserve length | ||||||||||||||||||||||||||||||
| const sanitizedKey = key.replace(/[^a-zA-Z0-9-_:.]/g, '-'); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return `${prefix}-${sanitizedKey}`; | ||||||||||||||||||||||||||||||
|
Comment on lines
+28
to
+33
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. 🧩 Analysis chain🏁 Script executed: # First, let's examine the actual file
cat -n src/hooks/useId.tsRepository: react-component/util Length of output: 2688 🏁 Script executed: # Search for how getId is being called to understand usage patterns
rg "getId" --type ts --type tsx -B 2 -A 2Repository: react-component/util Length of output: 91 🌐 Web query:
💡 Result: In React 18’s TypeScript types (
And the JSX
Citations:
🏁 Script executed: # Search for getId function calls to understand usage patterns
rg "getId\(" --type ts -B 2 -A 2Repository: react-component/util Length of output: 1160 🏁 Script executed: # Also check if this is exported/used elsewhere
rg "from.*useId|import.*getId" --type ts --type tsxRepository: react-component/util Length of output: 91 🏁 Script executed: # Check if there are any tests or examples showing how getId should be used
fd -e test.ts -e spec.ts -e example.ts | head -20Repository: react-component/util Length of output: 392 React 元素 key 类型与入参类型不匹配,存在运行时风险 JSDoc 注释声称该函数处理"React 元素的 key",但 🧩 修复建议-export function getId(prefix: string, key: string): string {
+export function getId(prefix: string, key: React.Key): string {
+ const normalizedKey = key == null ? '' : String(key);
// Valid id characters: letters, digits, hyphen, underscore, colon, period
// Replace all invalid characters (including spaces) with hyphens to preserve length
- const sanitizedKey = key.replace(/[^a-zA-Z0-9_.:-]/g, '-');
+ const sanitizedKey = normalizedKey.replace(/[^a-zA-Z0-9_.:-]/g, '-');
return `${prefix}-${sanitizedKey}`;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const useOriginId = getUseId(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export default useOriginId | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.