-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathuseId.ts
More file actions
82 lines (65 loc) · 2.38 KB
/
useId.ts
File metadata and controls
82 lines (65 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as React from 'react';
function getUseId() {
// We need fully clone React function here to avoid webpack warning React 17 do not export `useId`
const fullClone = {
...React,
};
return fullClone.useId;
}
let uuid = 0;
/** @private Note only worked in develop env. Not work in production. */
export function resetUuid() {
if (process.env.NODE_ENV !== 'production') {
uuid = 0;
}
}
/**
* 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: React.Key): string {
// React.Key can be string | number, convert to string first
const keyStr = String(key);
// Valid id characters: letters, digits, hyphen, underscore, colon, period
// Replace all invalid characters (including spaces) with hyphens to preserve length
const sanitizedKey = keyStr.replace(/[^a-zA-Z0-9_.:-]/g, '-');
return `${prefix}-${sanitizedKey}`;
}
const useOriginId = getUseId();
export default useOriginId
? // Use React `useId`
function useId(id?: string, disableTestMock?: boolean) {
const reactId = useOriginId();
// Developer passed id is single source of truth
if (id) {
return id;
}
// By default, test env always return mock id, but also allow force use id for test case which need to test id logic.
if (!disableTestMock && process.env.NODE_ENV === 'test') {
return 'test-id';
}
return reactId;
}
: // Use compatible of `useId`
function useCompatId(id?: string, disableTestMock?: boolean) {
// Inner id for accessibility usage. Only work in client side
const [innerId, setInnerId] = React.useState<string>('ssr-id');
React.useEffect(() => {
const nextId = uuid;
uuid += 1;
setInnerId(`rc_unique_${nextId}`);
}, []);
// Developer passed id is single source of truth
if (id) {
return id;
}
// By default, test env always return mock id, but also allow force use id for test case which need to test id logic.
if (!disableTestMock && process.env.NODE_ENV === 'test') {
return 'test-id';
}
// Return react native id or inner id
return innerId;
};