-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: made GUID and number id composables non-reliant on useId
- Loading branch information
Simon Milfred
committed
Jun 10, 2024
1 parent
888f1a7
commit bcfbe06
Showing
3 changed files
with
23 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
/* Create a GUID */ | ||
function useGUID(key = useId()) { | ||
return useState('_guid-' + key, () => { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( | ||
/[xy]/g, | ||
function (c) { | ||
const r = (Math.random() * 16) | 0, | ||
v = c == 'x' ? r : (r & 0x3) | 0x8; | ||
return v.toString(16); | ||
} | ||
); | ||
}).value; | ||
/* Make a GUID */ | ||
function useGUID(key) { | ||
return key ? useState('_guid-' + key, makeGUID).value : makeGUID(); | ||
} | ||
|
||
export default useGUID; | ||
|
||
function makeGUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( | ||
/[xy]/g, | ||
function (c) { | ||
const r = (Math.random() * 16) | 0, | ||
v = c == 'x' ? r : (r & 0x3) | 0x8; | ||
return v.toString(16); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
/* Create a number ID (unlike useId() which generates an id usable by the DOM) */ | ||
function useNumberId(key = useId()) { | ||
function useNumberId(key) { | ||
const counter = useState(() => 0); | ||
return useState('_num-id-' + key, () => counter.value++).value; | ||
return key ? useState('_num-id-' + key, nextNumber).value : nextNumber(); | ||
|
||
function nextNumber() { | ||
return counter.value++; | ||
} | ||
} | ||
export default useNumberId; |