Completely updated React, fixed #11, (hopefully)

This commit is contained in:
2018-03-04 19:11:49 -05:00
parent 6e0afd6e2a
commit 34e5f5139a
13674 changed files with 333464 additions and 473223 deletions

View File

@@ -1,23 +1,20 @@
// @flow weak
import React from 'react';
import PropTypes from 'prop-types';
import warning from 'warning';
import type { HigherOrderComponent } from 'react-flow-types';
import hoistNonReactStatics from 'hoist-non-react-statics';
import wrapDisplayName from 'recompose/wrapDisplayName';
import getDisplayName from 'recompose/getDisplayName';
import wrapDisplayName from 'recompose/wrapDisplayName';
import contextTypes from 'react-jss/lib/contextTypes';
import { create } from 'jss';
import preset from 'jss-preset-default';
import * as ns from 'react-jss/lib/ns';
import jssPreset from './jssPreset';
import createMuiTheme from './createMuiTheme';
import themeListener from './themeListener';
import createGenerateClassName from './createGenerateClassName';
import getStylesCreator from './getStylesCreator';
// New JSS instance.
const jss = create(preset());
const jss = create(jssPreset());
// Use a singleton or the provided one by the context.
const generateClassName = createGenerateClassName();
@@ -49,53 +46,16 @@ function getDefaultTheme() {
return defaultTheme;
}
type Options = {
flip?: boolean,
withTheme?: boolean,
name?: string,
// Problem: https://github.com/brigand/babel-plugin-flow-react-proptypes/issues/127
// import type { StyleSheetFactoryOptions } from 'jss/lib/types';
// ...StyleSheetFactoryOptions,
// and the fact that we currently cannot import/spread types with
// https://github.com/brigand/babel-plugin-flow-react-proptypes/issues/106
media?: string,
meta?: string,
index?: number,
link?: boolean,
element?: HTMLStyleElement,
generateClassName?: Function, // generateClassName - use generic to stop the bleeding.
};
export type RequiredProps = {
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* Use that property to pass a ref callback to the decorated component.
*/
innerRef?: Function,
};
// Note, theme is conditionally injected, but flow is static analysis so we need to include it.
export type InjectedProps = { classes: Object, theme: Object };
// Link a style sheet with a component.
// It does not modify the component passed to it;
// instead, it returns a new component, with a `classes` property.
const withStyles = (
stylesOrCreator: Object,
options?: Options = {},
): HigherOrderComponent<RequiredProps, InjectedProps> => (Component: any): any => {
const { withTheme = false, flip, name, ...styleSheetOptions } = options;
const withStyles = (stylesOrCreator, options = {}) => Component => {
const { withTheme = false, flip = null, name, ...styleSheetOptions } = options;
const stylesCreator = getStylesCreator(stylesOrCreator);
const listenToTheme = stylesCreator.themingEnabled || withTheme || typeof name === 'string';
if (stylesCreator.options.index === undefined) {
indexCounter += 1;
stylesCreator.options.index = indexCounter;
}
indexCounter += 1;
stylesCreator.options.index = indexCounter;
warning(
indexCounter < 0,
@@ -105,26 +65,13 @@ const withStyles = (
].join(' '),
);
class Style extends React.Component<RequiredProps> {
static contextTypes = {
muiThemeProviderOptions: PropTypes.object,
...contextTypes,
...(listenToTheme ? themeListener.contextTypes : {}),
};
// Exposed for tests purposes
static options: ?Options;
// Exposed for test purposes.
static Naked = Component;
constructor(props, context: Object) {
class WithStyles extends React.Component {
constructor(props, context) {
super(props, context);
const { muiThemeProviderOptions } = this.context;
this.jss = this.context[ns.jss] || jss;
const { muiThemeProviderOptions } = this.context;
if (muiThemeProviderOptions) {
if (muiThemeProviderOptions.sheetsManager) {
this.sheetsManager = muiThemeProviderOptions.sheetsManager;
@@ -141,7 +88,7 @@ const withStyles = (
generateClassName,
...this.context[ns.sheetOptions],
};
// We use || as it's lazy evaluated.
// We use || as the function call is lazy evaluated.
this.theme = listenToTheme ? themeListener.initial(context) || getDefaultTheme() : noopTheme;
}
@@ -188,7 +135,7 @@ const withStyles = (
}
}
attach(theme: Object) {
attach(theme) {
if (this.disableStylesGeneration) {
return;
}
@@ -213,14 +160,15 @@ const withStyles = (
if (sheetManagerTheme.refs === 0) {
const styles = stylesCreatorSaved.create(theme, name);
let meta;
let meta = name;
if (process.env.NODE_ENV !== 'production') {
meta = name || getDisplayName(Component);
if (process.env.NODE_ENV !== 'production' && !meta) {
meta = getDisplayName(Component);
}
const sheet = this.jss.createStyleSheet(styles, {
meta,
classNamePrefix: meta,
flip: typeof flip === 'boolean' ? flip : theme.direction === 'rtl',
link: false,
...this.sheetOptions,
@@ -241,7 +189,7 @@ const withStyles = (
sheetManagerTheme.refs += 1;
}
detach(theme: Object) {
detach(theme) {
if (this.disableStylesGeneration) {
return;
}
@@ -262,14 +210,13 @@ const withStyles = (
}
}
unsubscribeId = null;
jss = null;
sheetsManager = sheetsManager;
disableStylesGeneration = false;
jss = null;
sheetOptions = null;
sheetsManager = sheetsManager;
stylesCreatorSaved = null;
theme = null;
sheetOptions = null;
theme = null;
unsubscribeId = null;
render() {
const { classes: classesProp, innerRef, ...other } = this.props;
@@ -332,16 +279,36 @@ const withStyles = (
}
}
hoistNonReactStatics(Style, Component);
WithStyles.propTypes = {
/**
* Useful to extend the style applied to components.
*/
classes: PropTypes.object,
/**
* Use that property to pass a ref callback to the decorated component.
*/
innerRef: PropTypes.func,
};
// Higher specificity
Style.options = options;
WithStyles.contextTypes = {
muiThemeProviderOptions: PropTypes.object,
...contextTypes,
...(listenToTheme ? themeListener.contextTypes : {}),
};
if (process.env.NODE_ENV !== 'production') {
Style.displayName = wrapDisplayName(Component, 'withStyles');
WithStyles.displayName = wrapDisplayName(Component, 'WithStyles');
}
return Style;
hoistNonReactStatics(WithStyles, Component);
if (process.env.NODE_ENV !== 'production') {
// Exposed for test purposes.
WithStyles.Naked = Component;
WithStyles.options = options;
}
return WithStyles;
};
export default withStyles;