forked from patternfly/patternfly-react
-
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.
refactor: Use the same mechanism for autogenerated component id (patt…
…ernfly#830) To make the code more consistent, I added a method getUniqueId that is used by all components that need a unique id. I put this id recovery in the defaultProps object. Add also some tests for util file.
- Loading branch information
1 parent
c7072c8
commit b8bc4b1
Showing
6 changed files
with
63 additions
and
47 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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export function capitalize(input) { | ||
return input[0].toUpperCase() + input.substring(1); | ||
} | ||
|
||
export function getUniqueId(prefix = 'pf') { | ||
const uid = | ||
new Date().getTime() + | ||
Math.random() | ||
.toString(36) | ||
.slice(2); | ||
return `${prefix}-${uid}`; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/patternfly-4/react-core/src/internal/util.test.js
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,14 @@ | ||
import { capitalize, getUniqueId } from './util'; | ||
|
||
test('capitalize', () => { | ||
expect(capitalize('foo')).toBe('Foo'); | ||
}); | ||
|
||
test('getUniqueId', () => { | ||
expect(getUniqueId()).not.toBe(getUniqueId()); | ||
}); | ||
|
||
test('getUniqueId prefixed', () => { | ||
expect(getUniqueId().substring(0, 3)).toBe('pf-'); | ||
expect(getUniqueId('pf-switch').substring(0, 10)).toBe('pf-switch-'); | ||
}); |