Skip to content

Commit

Permalink
Move ReactDOMFactories into separate package (#8356)
Browse files Browse the repository at this point in the history
- Update examples to no longer use React.DOM
- Add package and documentation entries for react-addons-dom-factories
- Update dom-factories readme
- Set up proxy to intercept React.DOM usage
- Update ReactDOM children tests to use createElement
- Add more specific warning assertion for React DOM factories
- Do not use expectDev in ReactDOMFactories tests
  • Loading branch information
nhunzaker authored and flarnie committed May 9, 2017
1 parent 850231c commit 0001036
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/docs/addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The add-ons below are considered legacy and their use is discouraged.
- [`PureRenderMixin`](pure-render-mixin.html). Use [`React.PureComponent`](/react/docs/react-api.html#react.purecomponent) instead.
- [`shallowCompare`](shallow-compare.html), a helper function that performs a shallow comparison for props and state in a component to decide if a component should update.
- [`update`](update.html). Use [`kolodny/immutability-helper`](https://github.com/kolodny/immutability-helper) instead.
- [`ReactDOMFactories`](dom-factories.html), pre-configured DOM factories to make React easier to use without JSX.

### Deprecated Add-ons

Expand Down
20 changes: 20 additions & 0 deletions packages/react-dom-factories/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `react-addons-dom-factories`

> Note:
> `ReactDOMFactories` is a legacy add-on. Consider using
> `React.createFactory` or JSX instead.
Prior to version 16.0.0, React maintained a whitelist of
pre-configured DOM factories. These predefined factories have been
moved to the `react-addons-dom-factories` library.

## Example

```javascript
import ReactDOM from 'react-dom';
import DOM from 'react-addons-dom-factories'; // ES6

const greeting = DOM.div({ className: 'greeting' }, DOM.p(null, 'Hello, world!'));

ReactDOM.render(greeting, document.getElementById('app'))
```
3 changes: 3 additions & 0 deletions packages/react-dom-factories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./lib/ReactDOMFactories');
25 changes: 25 additions & 0 deletions packages/react-dom-factories/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "react-dom-factories",
"version": "16.0.0-alpha",
"description": "React package for DOM factory methods.",
"main": "index.js",
"repository": "facebook/react",
"keywords": [
"react"
],
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/facebook/react/issues"
},
"homepage": "https://facebook.github.io/react/",
"peerDependencies": {
"react": "^16.0.0-alpha"
},
"files": [
"LICENSE",
"PATENTS",
"README.md",
"index.js",
"lib/"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ if (__DEV__) {

/**
* Creates a mapping from supported HTML tags to `ReactDOMComponent` classes.
* This is also accessible via `React.DOM`.
*
* @public
*/
Expand Down
39 changes: 39 additions & 0 deletions src/addons/__tests__/ReactDOMFactories-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

var React = require('React');
var {div} = require('ReactDOMFactories');

describe('ReactDOMFactories', () => {
it('allow factories to be called without warnings', () => {
spyOn(console, 'error');
var element = div();
expect(element.type).toBe('div');
expect(console.error).not.toHaveBeenCalled();
});

it('warns once when accessing React.DOM methods', () => {
spyOn(console, 'error');

var a = React.DOM.a();
var p = React.DOM.p();

expect(a.type).toBe('a');
expect(p.type).toBe('p');

expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error.calls.first().args[0]).toContain(
'Warning: Accessing factories like React.DOM.a has been deprecated',
);
});
});
21 changes: 21 additions & 0 deletions src/isomorphic/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ if (__DEV__) {
},
});
}

// React.DOM factories are deprecated. Wrap these methods so that
// invocations of the React.DOM namespace and alert users to switch
// to the `react-addons-dom-factories` package.
React.DOM = {};
var warnedForFactories = false;
Object.keys(ReactDOMFactories).forEach(function(factory) {
React.DOM[factory] = function(...args) {
if (!warnedForFactories) {
warning(
false,
'Accessing factories like React.DOM.%s has been deprecated ' +
'and will be removed in the future. Use the ' +
'react-addons-dom-factories package instead.',
factory,
);
warnedForFactories = true;
}
return ReactDOMFactories[factory](...args);
};
});
}

module.exports = React;
10 changes: 6 additions & 4 deletions src/isomorphic/classic/element/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe('ReactElement', () => {
expect(React.isValidElement(true)).toEqual(false);
expect(React.isValidElement({})).toEqual(false);
expect(React.isValidElement('string')).toEqual(false);
expect(React.isValidElement(React.DOM.div)).toEqual(false);
expect(React.isValidElement(React.createFactory('div'))).toEqual(false);
expect(React.isValidElement(Component)).toEqual(false);
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);

Expand Down Expand Up @@ -459,7 +459,7 @@ describe('ReactElement', () => {
expect(React.isValidElement(true)).toEqual(false);
expect(React.isValidElement({})).toEqual(false);
expect(React.isValidElement('string')).toEqual(false);
expect(React.isValidElement(React.DOM.div)).toEqual(false);
expect(React.isValidElement(React.createFactory('div'))).toEqual(false);
expect(React.isValidElement(Component)).toEqual(false);
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);

Expand Down Expand Up @@ -520,7 +520,8 @@ describe('comparing jsx vs .createFactory() vs .createElement()', () => {
var childFactory = React.createFactory(Child);
class Parent extends React.Component {
render() {
return React.DOM.div(
return React.createElement(
'div',
{},
childFactory({ref: 'child', foo: 'foo value'}, 'children value'),
);
Expand Down Expand Up @@ -555,7 +556,8 @@ describe('comparing jsx vs .createFactory() vs .createElement()', () => {
beforeEach(() => {
class Parent extends React.Component {
render() {
return React.DOM.div(
return React.createElement(
'div',
{},
React.createElement(
Child,
Expand Down
3 changes: 2 additions & 1 deletion src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,8 @@ describe('ReactDOMComponent', () => {
it('should properly escape text content and attributes values', () => {
expect(
ReactDOMServer.renderToStaticMarkup(
React.DOM.div(
React.createElement(
'div',
{
title: '\'"<>&',
style: {
Expand Down

0 comments on commit 0001036

Please sign in to comment.