UNPKG

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