UNPKG

2.51 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var _prodInvariant = require('./reactProdInvariant');
12
13var ReactChildren = require('./ReactChildren');
14var ReactElement = require('./ReactElement');
15
16var emptyFunction = require('fbjs/lib/emptyFunction');
17var invariant = require('fbjs/lib/invariant');
18var warning = require('fbjs/lib/warning');
19
20/**
21 * We used to allow keyed objects to serve as a collection of ReactElements,
22 * or nested sets. This allowed us a way to explicitly key a set or fragment of
23 * components. This is now being replaced with an opaque data structure.
24 * The upgrade path is to call React.addons.createFragment({ key: value }) to
25 * create a keyed fragment. The resulting data structure is an array.
26 */
27
28var numericPropertyRegex = /^\d+$/;
29
30var warnedAboutNumeric = false;
31
32var ReactFragment = {
33 /**
34 * Wrap a keyed object in an opaque proxy that warns you if you access any
35 * of its properties.
36 * See https://facebook.github.io/react/docs/create-fragment.html
37 */
38 create: function (object) {
39 if (typeof object !== 'object' || !object || Array.isArray(object)) {
40 process.env.NODE_ENV !== 'production' ? warning(false, 'React.addons.createFragment only accepts a single object. Got: %s', object) : void 0;
41 return object;
42 }
43 if (ReactElement.isValidElement(object)) {
44 process.env.NODE_ENV !== 'production' ? warning(false, 'React.addons.createFragment does not accept a ReactElement ' + 'without a wrapper object.') : void 0;
45 return object;
46 }
47
48 !(object.nodeType !== 1) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'React.addons.createFragment(...): Encountered an invalid child; DOM elements are not valid children of React components.') : _prodInvariant('0') : void 0;
49
50 var result = [];
51
52 for (var key in object) {
53 if (process.env.NODE_ENV !== 'production') {
54 if (!warnedAboutNumeric && numericPropertyRegex.test(key)) {
55 process.env.NODE_ENV !== 'production' ? warning(false, 'React.addons.createFragment(...): Child objects should have ' + 'non-numeric keys so ordering is preserved.') : void 0;
56 warnedAboutNumeric = true;
57 }
58 }
59 ReactChildren.mapIntoWithKeyPrefixInternal(object[key], result, key, emptyFunction.thatReturnsArgument);
60 }
61
62 return result;
63 }
64};
65
66module.exports = ReactFragment;
\No newline at end of file