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

feat: wrap all components with withStyles HOC #749

Merged
merged 81 commits into from
Oct 1, 2019
Merged

Conversation

jbadan
Copy link
Contributor

@jbadan jbadan commented Sep 26, 2019

Description

The new withStyles higher order component wraps each component and dynamically requires the relevant styles, or disable the styles based on props.

Pass custom styling via require statement in customStyles prop.
Disable all styles using disableStyles prop

This pr introduces storybook! 📖 🎉

Functional components now with wrapped with forwardRefs

npm run start:playground or npm run storybook to start it

NOTE that you will need to refresh the page to see different style combos because the style will persist across pages.

To do:

  • docs site - markdown for code samples not working. This has to do with the forwarding refs
  • bug: Numbers in the calendar not showing up in storybook but are in docs site
  • update all tests that use .state() - not compatible with wrapped components 💀 😭
  • audit subcomponents to see if they need withStyles wrapper
  • finish remaining components
  • figure out why docs site in netlify is bringing in the customTestStyles 😆 - hot pink navigation anyone?
  • localizationEditor cascade disableCSS
  • prop description tables

@jbadan jbadan changed the title feat: WIP: wrap all components with withStyles HOC feat: wrap all components with withStyles HOC Oct 1, 2019
@jbadan jbadan requested a review from a team October 1, 2019 15:04

const ActionBar = ({ children, className, ...props }) => {
const ActionBar = React.forwardRef(({ children, className, customStyles, disableStyles, ...props }, ref) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can get rid of the customStyles in this prop pruning use case since this never gets passed here from withStyles. It should stay in the PropTypes definition, however, because consumers are unaware that the component is wrapped in an HOC.

@@ -3,6 +3,7 @@ import Popover from '../Popover/Popover';
import PropTypes from 'prop-types';
import Time from '../Time/Time';
import TimePickerItem from './_TimePickerItem';
import withStyles from '../utils/WithStyles/WithStyles';
Copy link
Contributor

Choose a reason for hiding this comment

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

Does TimePicker need the ref forwarding?

import classnames from 'classnames';
import FormItem from '../Forms/FormItem';
import FormLabel from '../Forms/FormLabel';
import PropTypes from 'prop-types';
import React from 'react';
import { TOGGLE_SIZES } from '../utils/constants';

import withStyles from '../utils/WithStyles/WithStyles';
class Toggle extends React.Component {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does Toggle need the ref forwarding?

import PropTypes from 'prop-types';
import Tree from './_Tree';
import TreeBranch from './_TreeBranch';
import TreeCol from './_TreeCol';
import TreeHead from './_TreeHead';
import TreeItem from './_TreeItem';
import TreeRow from './_TreeRow';
import withStyles from '../utils/WithStyles/WithStyles';
import React, { Component } from 'react';

class TreeView extends Component {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does TreeView need the ref forwarding?

Copy link
Contributor

@greg-a-smith greg-a-smith left a comment

Choose a reason for hiding this comment

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

Disregard my last 3 comments about ref forwarding -- I didn't notice they weren't functional components. All aboard!!! 🚢

@jbadan jbadan merged commit f4f08d7 into master Oct 1, 2019
@jbadan jbadan deleted the feat/withStyles branch October 1, 2019 17:23
};

ActionBar.Actions = ActionBarActions;
ActionBar.Back = ActionBarBack;
ActionBar.Header = ActionBarHeader;

export default ActionBar;
export { ActionBar as __ActionBar };
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of these __ renamed exports?

Copy link
Contributor

Choose a reason for hiding this comment

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

To have access to the "plain" component for the purposes of building the docs site (property tables and import statements).

<div {...props} className={actionBarClasses}>{children}</div>
<div {...props}
className={actionBarClasses}
ref={ref}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this ref (and similar others) added? Usually they're for focus management, but this is just a containing div

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reasoning was to go ahead and add them to every functional component being wrapped in withStyles as it will be a breaking change to add them going forward. See: https://reactjs.org/docs/forwarding-refs.html#note-for-component-library-maintainers

@@ -64,12 +65,13 @@ class Alert extends Component {
)}
<div className='fd-alert__text'>
{type && (
<Icon glyph={`mesage-${type}`} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this mesage a typo?

@@ -23,7 +23,7 @@ describe('<DatePicker />', () => {
});

test('open/close by calendar icon button', () => {
wrapper = mount(defaultDatePicker);
wrapper = mount(defaultDatePicker).children().children();
Copy link
Contributor

Choose a reason for hiding this comment

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

There are ~80 instances of this .children().children() pattern—can we write a single mount function with a comment for why we need to dive down 2 levels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely should be on the to do for the next iteration ✅

};

//HOCs do not automatically pass refs as they are not technically props
function forwardRef(props, ref) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function name makes it sound like an imperative side-effecty thing, when really this is a component. Is there a better name?

Also, can this just return the "with ref" version so the hoisted thing below is a little clearer? Maybe something like:

const componentWithRef = (props, ref) => React.forwardRef(<WithStyles {...props} forwardedRef={ref} />);

const componentWithStaticMethods = hoistNonReactStatic(componentWithRef, WrappedComponent);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can definitely try that out in the next iteration - I took this from the react docs to rather be safe than sorry.

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.

None yet

4 participants