Skip to content

Commit

Permalink
Move ReactDOMFactories into separate package (facebook#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 Apr 24, 2017
1 parent c8a64e2 commit 86dd083
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 16 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/"
]
}
5 changes: 4 additions & 1 deletion scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ scripts/shared/__tests__/evalToString-test.js
* should support string concat (`+`)
* should throw when it finds other types

src/addons/__tests__/ReactDOMFactories-test.js
* allow factories to be called without warnings
* warns once when accessing React.DOM methods

src/isomorphic/__tests__/React-test.js
* should log a deprecation warning once when using React.createMixin
* should warn once when attempting to access React.createClass
Expand Down Expand Up @@ -839,7 +843,6 @@ src/renderers/dom/shared/__tests__/ReactDOM-test.js
* should allow children to be passed as an argument
* should overwrite props.children with children argument
* should purge the DOM cache when removing nodes
* allow React.DOM factories to be called without warnings
* throws in render() if the mount callback is not a function
* throws in render() if the update callback is not a function
* preserves focus
Expand Down
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 @@ -146,6 +146,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 @@ -322,7 +322,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 @@ -465,7 +465,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 @@ -526,7 +526,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 @@ -561,7 +562,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
11 changes: 2 additions & 9 deletions src/renderers/dom/shared/__tests__/ReactDOM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe('ReactDOM', () => {

it('should allow children to be passed as an argument', () => {
var argDiv = ReactTestUtils.renderIntoDocument(
React.DOM.div(null, 'child'),
React.createElement('div', null, 'child'),
);
var argNode = ReactDOM.findDOMNode(argDiv);
expect(argNode.innerHTML).toBe('child');
});

it('should overwrite props.children with children argument', () => {
var conflictDiv = ReactTestUtils.renderIntoDocument(
React.DOM.div({children: 'fakechild'}, 'child'),
React.createElement('div', {children: 'fakechild'}, 'child'),
);
var conflictNode = ReactDOM.findDOMNode(conflictDiv);
expect(conflictNode.innerHTML).toBe('child');
Expand Down Expand Up @@ -110,13 +110,6 @@ describe('ReactDOM', () => {
expect(dog.className).toBe('bigdog');
});

it('allow React.DOM factories to be called without warnings', () => {
spyOn(console, 'error');
var element = React.DOM.div();
expect(element.type).toBe('div');
expectDev(console.error.calls.count()).toBe(0);
});

it('throws in render() if the mount callback is not a function', () => {
spyOn(console, 'error');

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 @@ -1193,7 +1193,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 86dd083

Please sign in to comment.