-
Notifications
You must be signed in to change notification settings - Fork 376
refactor: Use the same mechanism for autogenerated component id #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
PatternFly-React preview: https://830-pr-patternfly-react-patternfly.surge.sh |
Pull Request Test Coverage Report for Build 2866
💛 - Coveralls |
| }; | ||
|
|
||
| class Progress extends Component { | ||
| constructor(props) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great PR! But we do need to initialize the unique id in the constructors. Or at least have some other way of assigning unique id to the component. Props get evaluated before the component instances are created so if you have multiple of the same components on the page they will all have the same id otherwise.
| Math.random() | ||
| .toString(36) | ||
| .slice(2), | ||
| id: getUniqueId(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not work. Default props are only instantiated once on when this variable , defaultProps is created. The id will need to be setup in a "constructor" or better as a class property
class Something extends React.Component {
this.id = this.props.id || getUniqueId();
}| return input[0].toUpperCase() + input.substring(1); | ||
| } | ||
|
|
||
| export function getUniqueId() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be nice to allow a prefix for this so that we can make the id's somewhat useful
getUniqueId(prefix = '') {
return (
`${prefix}-` + new Date().getTime() +
Math.random()
.toString(36)
.slice(2)
);
}this would allow us to do something like getUniqueId('pf-switch') we could even add the pf- by default and have the preson just pass the component name to it.
c4fe12a to
84dc4b0
Compare
|
@dmiller9911 and @jschuler done |
packages/patternfly-4/react-core/src/components/Dropdown/Dropdown.js
Outdated
Show resolved
Hide resolved
03b2e06 to
1b3ef68
Compare
tlabaj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Could you just merge the merge conflicts. Thanks!
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.
98b92bb
1b3ef68 to
98b92bb
Compare
tlabaj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGtM
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 in the defaultProps object. Add also some tests for util
file.