react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / cloneWithProps.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @typechecks9* @providesModule cloneWithProps10*/1112"use strict";1314var ReactElement = require('ReactElement');15var ReactPropTransferer = require('ReactPropTransferer');1617var keyOf = require('keyOf');18var warning = require('warning');1920var CHILDREN_PROP = keyOf({children: null});2122/**23* Sometimes you want to change the props of a child passed to you. Usually24* this is to add a CSS class.25*26* @param {object} child child component you'd like to clone27* @param {object} props props you'd like to modify. They will be merged28* as if you used `transferPropsTo()`.29* @return {object} a clone of child with props merged in.30*/31function cloneWithProps(child, props) {32if (__DEV__) {33warning(34!child.ref,35'You are calling cloneWithProps() on a child with a ref. This is ' +36'dangerous because you\'re creating a new child which will not be ' +37'added as a ref to its parent.'38);39}4041var newProps = ReactPropTransferer.mergeProps(props, child.props);4243// Use `child.props.children` if it is provided.44if (!newProps.hasOwnProperty(CHILDREN_PROP) &&45child.props.hasOwnProperty(CHILDREN_PROP)) {46newProps.children = child.props.children;47}4849// The current API doesn't retain _owner and _context, which is why this50// doesn't use ReactElement.cloneAndReplaceProps.51return ReactElement.createElement(child.type, newProps);52}5354module.exports = cloneWithProps;555657