-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
refactor(react): update ContentSwitcher and Switch to hooks #3302
refactor(react): update ContentSwitcher and Switch to hooks #3302
Conversation
…refactor/update-content-switch-to-hooks
…refactor/update-content-switch-to-hooks
Deploy preview for the-carbon-components ready! Built with commit 2276790 https://deploy-preview-3302--the-carbon-components.netlify.com |
Deploy preview for carbon-components-react ready! Built with commit 2276790 https://deploy-preview-3302--carbon-components-react.netlify.com |
Deploy preview for carbon-elements ready! Built with commit 2276790 |
Deploy preview for the-carbon-components ready! Built with commit 6667a27 https://deploy-preview-3302--the-carbon-components.netlify.com |
Deploy preview for carbon-elements ready! Built with commit 6667a27 |
Deploy preview for carbon-components-react ready! Built with commit 6667a27 https://deploy-preview-3302--carbon-components-react.netlify.com |
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.
just a couple notes on naming/content, otherwise seems cool!
import { settings } from 'carbon-components'; | ||
import cx from 'classnames'; |
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.
why abbreviate to cx
? i typically prefer more readable/explicit names
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.
cx
comes from the "class set" convention from earlier versions of React 👍 Can definitely map it over now if that convention is outdated
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.
in this case i usually see it imported as classnames
around the codebase
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.
@lovemecomputer it's definitely a mix across the codebase depending on the author, we can totally consolidate though. cx
to me is helpful because it's shorter and has been a convention since early versions of React but I understand if that is no longer a relevant reference.
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 to me. I don't have any preference between cx
/classname
, that seems like a different issue if we want all of those to align.
Note to reviewers: we have a slight bug with the controlled prop where if it changes we are accidentally focusing the switch at the new index 🤦♂ will update accordingly! |
if (controlledSelectedIndex !== prevControlledIndex) { | ||
setSelectedIndex(controlledSelectedIndex); | ||
setPrevControlledIndex(controlledSelectedIndex); | ||
setShouldFocus(false); |
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.
Does the rendering code after this line stop focusing or not?
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.
Here's a quick isolated demo that I think demonstrates what's going on here: https://codesandbox.io/s/jolly-antonelli-uevex
Associated demo:
All this is to say that when we enqueue this state transition the effect is never re-run with the truth-y value in the effect. Instead, React runs the effect with the false-y value.
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.
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.
Cool, makes sense now, thank you for clarifying.
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.
I like this. Hooks are me-friendly 🧠 👍🏽
Update
ContentSwitcher
andSwitch
components to use hooks. This includes some general clean-ups as these codepaths haven't been updated in a while, namely around the test suite and props. This update also consolidatesContentSwitcher
andSwitch
under the same folder, making sure to add in aliases to the new paths.Changelog
New
Changed
ContentSwitcher
has been updated to use hooksSwitch
has been updated to reflect changes inContentSwitcher
.name
prop onSwitch
as we can now useindex
insteaddeprecate
prop type helperContentSwitcher
andSwitch
Removed