-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 5815-warning-inner-fill-progress-indicator
- Loading branch information
Showing
6 changed files
with
92 additions
and
2 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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright IBM Corp. 2020 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { render, cleanup } from '@carbon/test-utils/react'; | ||
import React from 'react'; | ||
import { useId } from '../useId'; | ||
|
||
describe('useId', () => { | ||
afterEach(cleanup); | ||
|
||
it('should generate a unique id that is stable across renders', () => { | ||
function Test() { | ||
const id = useId('test'); | ||
return <span id={id}>test</span>; | ||
} | ||
|
||
const container = document.createElement('div'); | ||
const getTestId = () => container.firstChild.getAttribute('id'); | ||
|
||
render(<Test />, { container }); | ||
|
||
const id = getTestId(); | ||
expect(getTestId()).toBeDefined(); | ||
|
||
render(<Test />, { container }); | ||
expect(id).toBe(getTestId()); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { useRef } from 'react'; | ||
import setupGetInstanceId from '../tools/setupGetInstanceId'; | ||
|
||
const getId = setupGetInstanceId(); | ||
|
||
/** | ||
* Generate a unique ID with an optional prefix prepended to it | ||
* @param {string} [prefix] | ||
* @returns {string} | ||
*/ | ||
export function useId(prefix = 'id') { | ||
const ref = useRef(`${prefix}-${getId()}`); | ||
return ref.current; | ||
} |