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

fluxible-addons-react: replaced commonjs imports by es imports #664

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions packages/fluxible-addons-react/src/FluxibleComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
import { Component, cloneElement } from 'react';
import { func, object, node } from 'prop-types';

const React = require('react');
const PropTypes = require('prop-types');

class FluxibleComponent extends React.Component {
class FluxibleComponent extends Component {
getChildContext() {
return {
getStore: this.props.context.getStore,
Expand All @@ -16,20 +14,20 @@ class FluxibleComponent extends React.Component {
}

render() {
return React.cloneElement(this.props.children, {
return cloneElement(this.props.children, {
context: this.props.context
});
}
}

FluxibleComponent.propTypes = {
children: PropTypes.node.isRequired,
context: PropTypes.object.isRequired
children: node.isRequired,
context: object.isRequired
};

FluxibleComponent.childContextTypes = {
executeAction: PropTypes.func.isRequired,
getStore: PropTypes.func.isRequired
executeAction: func.isRequired,
getStore: func.isRequired
};

module.exports = FluxibleComponent;
export default FluxibleComponent;
5 changes: 2 additions & 3 deletions packages/fluxible-addons-react/src/batchedUpdatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Copyright 2014, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
const { unstable_batchedUpdates } = require('react-dom');
import { unstable_batchedUpdates } from 'react-dom';

function createBatchedUpdatePlugin() {
/**
Expand All @@ -25,4 +24,4 @@ function createBatchedUpdatePlugin() {
};
}

module.exports = createBatchedUpdatePlugin;
export default createBatchedUpdatePlugin;
18 changes: 8 additions & 10 deletions packages/fluxible-addons-react/src/connectToStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';

const React = require('react');
const PropTypes = require('prop-types');
const hoistNonReactStatics = require('hoist-non-react-statics');
import { Component as ReactComponent, createRef, createElement } from 'react';
import { func } from 'prop-types';
import hoistNonReactStatics from 'hoist-non-react-statics';

/**
* Registers change listeners and retrieves state from stores using the `getStateFromStores`
Expand All @@ -32,14 +30,14 @@ const hoistNonReactStatics = require('hoist-non-react-statics');
* @returns {React.Component} or {Function} if using decorator pattern
*/
function connectToStores(Component, stores, getStateFromStores, customContextTypes) {
class StoreConnector extends React.Component {
class StoreConnector extends ReactComponent {
constructor(props, context) {
super(props, context);
this._isMounted = false;
this._onStoreChange = this._onStoreChange.bind(this);
this.getStateFromStores = this.getStateFromStores.bind(this);
this.state = this.getStateFromStores();
this.wrappedElementRef = React.createRef();
this.wrappedElementRef = createRef();
}

getStateFromStores(props) {
Expand Down Expand Up @@ -71,14 +69,14 @@ function connectToStores(Component, stores, getStateFromStores, customContextTyp
const props = (Component.prototype && Component.prototype.isReactComponent)
? {ref: this.wrappedElementRef}
: null;
return React.createElement(Component, {...this.props, ...this.state, ...props});
return createElement(Component, {...this.props, ...this.state, ...props});
}
}

StoreConnector.displayName = `storeConnector(${Component.displayName || Component.name || 'Component'})`;

StoreConnector.contextTypes = {
getStore: PropTypes.func.isRequired,
getStore: func.isRequired,
...customContextTypes
},

Expand All @@ -89,4 +87,4 @@ function connectToStores(Component, stores, getStateFromStores, customContextTyp
return StoreConnector;
}

module.exports = connectToStores;
export default connectToStores;
14 changes: 6 additions & 8 deletions packages/fluxible-addons-react/src/createElementWithContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';

const React = require('react');
const FluxibleComponent = require('./FluxibleComponent');
import { Component, createElement } from 'react';
import FluxibleComponent from './FluxibleComponent';

/**
* Creates an instance of the app level component with given props and a proper component
Expand All @@ -20,17 +18,17 @@ function createElementWithContext(fluxibleContext, props) {
throw new Error('A top-level component was not passed to the Fluxible constructor.');
}
if (Component.displayName && Component.displayName.includes('contextProvider')) {
return React.createElement(
return createElement(
Component,
{context: fluxibleContext.getComponentContext(), ...props},
);
}
const componentInstance = React.createElement(Component, props);
return React.createElement(
const componentInstance = createElement(Component, props);
return createElement(
FluxibleComponent,
{context: fluxibleContext.getComponentContext()},
componentInstance
);
}

module.exports = createElementWithContext;
export default createElementWithContext;
14 changes: 5 additions & 9 deletions packages/fluxible-addons-react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';

module.exports = {
batchedUpdatePlugin: require('./batchedUpdatePlugin'),
connectToStores: require('./connectToStores'),
createElementWithContext: require('./createElementWithContext'),
FluxibleComponent: require('./FluxibleComponent'),
provideContext: require('./provideContext')
};
export { default as FluxibleComponent } from './FluxibleComponent';
export { default as batchedUpdatePlugin } from './batchedUpdatePlugin';
export { default as connectToStores } from './connectToStores';
export { default as createElementWithContext } from './createElementWithContext';
export { default as provideContext } from './provideContext';
22 changes: 10 additions & 12 deletions packages/fluxible-addons-react/src/provideContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';

const React = require('react');
const PropTypes = require('prop-types');
const hoistNonReactStatics = require('hoist-non-react-statics');
import { Component as ReactComponent, createRef, createElement } from 'react';
import { func, object } from 'prop-types';
import hoistNonReactStatics from 'hoist-non-react-statics';

/**
* Provides context prop to all children as React context
Expand All @@ -22,10 +20,10 @@ const hoistNonReactStatics = require('hoist-non-react-statics');
* @returns {React.Component} or {Function} if using decorator pattern
*/
function provideContext(Component, customContextTypes) {
class ContextProvider extends React.Component {
class ContextProvider extends ReactComponent {
constructor(props) {
super(props);
this.wrappedElementRef = React.createRef();
this.wrappedElementRef = createRef();
}

getChildContext() {
Expand All @@ -45,18 +43,18 @@ function provideContext(Component, customContextTypes) {
const props = (Component.prototype && Component.prototype.isReactComponent)
? {ref: this.wrappedElementRef}
: null;
return React.createElement(Component, {...this.props, ...props});
return createElement(Component, {...this.props, ...props});
}
}

ContextProvider.childContextTypes = {
executeAction: PropTypes.func.isRequired,
getStore: PropTypes.func.isRequired,
executeAction: func.isRequired,
getStore: func.isRequired,
...customContextTypes
};

ContextProvider.propTypes = {
context: PropTypes.object.isRequired
context: object.isRequired
};

ContextProvider.displayName = `contextProvider(${Component.displayName || Component.name || 'Component'})`;
Expand All @@ -66,4 +64,4 @@ function provideContext(Component, customContextTypes) {
return ContextProvider;
}

module.exports = provideContext;
export default provideContext;