Skip to content
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

Conversation

joshblack
Copy link
Contributor

Update ContentSwitcher and Switch 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 consolidates ContentSwitcher and Switch under the same folder, making sure to add in aliases to the new paths.

Changelog

New

Changed

  • ContentSwitcher has been updated to use hooks
  • Switch has been updated to reflect changes in ContentSwitcher.
  • Deprecate name prop on Switch as we can now use index instead
  • Fix bug in deprecate prop type helper
  • Update test suites for ContentSwitcher and Switch

Removed

@joshblack joshblack requested a review from a team July 8, 2019 05:21
@ghost ghost requested review from jnm2377 and lovemecomputer and removed request for a team July 8, 2019 05:21
@netlify
Copy link

netlify bot commented Jul 8, 2019

Deploy preview for the-carbon-components ready!

Built with commit 2276790

https://deploy-preview-3302--the-carbon-components.netlify.com

@netlify
Copy link

netlify bot commented Jul 8, 2019

Deploy preview for carbon-components-react ready!

Built with commit 2276790

https://deploy-preview-3302--carbon-components-react.netlify.com

@netlify
Copy link

netlify bot commented Jul 8, 2019

Deploy preview for carbon-elements ready!

Built with commit 2276790

https://deploy-preview-3302--carbon-elements.netlify.com

@netlify
Copy link

netlify bot commented Jul 8, 2019

Deploy preview for the-carbon-components ready!

Built with commit 6667a27

https://deploy-preview-3302--the-carbon-components.netlify.com

@netlify
Copy link

netlify bot commented Jul 8, 2019

Deploy preview for carbon-elements ready!

Built with commit 6667a27

https://deploy-preview-3302--carbon-elements.netlify.com

@netlify
Copy link

netlify bot commented Jul 8, 2019

Deploy preview for carbon-components-react ready!

Built with commit 6667a27

https://deploy-preview-3302--carbon-components-react.netlify.com

Copy link
Contributor

@lovemecomputer lovemecomputer left a 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';
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

@jnm2377 jnm2377 left a 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.

@joshblack
Copy link
Contributor Author

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!

@joshblack
Copy link
Contributor Author

@asudoh @dakahn I think I found a solid heuristic for managing focus. Curious what you two think!

@joshblack joshblack requested a review from dakahn July 12, 2019 02:52
@joshblack joshblack requested a review from asudoh July 12, 2019 02:52
if (controlledSelectedIndex !== prevControlledIndex) {
setSelectedIndex(controlledSelectedIndex);
setPrevControlledIndex(controlledSelectedIndex);
setShouldFocus(false);
Copy link
Contributor

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?

Copy link
Contributor Author

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:

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you view the storybook, you'll notice that on first render we have no focus inside the ContentSwitcher alongside when selectedIndex is changed through the knob 👍

demo

Copy link
Contributor

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.

Copy link
Contributor

@dakahn dakahn left a 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 🧠 👍🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants