-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathuseId.ts
More file actions
32 lines (26 loc) · 787 Bytes
/
useId.ts
File metadata and controls
32 lines (26 loc) · 787 Bytes
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
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<string>();
React.useEffect(() => {
setInnerId(`rc_select_${getUUID()}`);
}, []);
return id || innerId;
}