react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / ReactPropTransferer.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* @providesModule ReactPropTransferer9*/1011"use strict";1213var assign = require('Object.assign');14var emptyFunction = require('emptyFunction');15var invariant = require('invariant');16var joinClasses = require('joinClasses');17var warning = require('warning');1819var didWarn = false;2021/**22* Creates a transfer strategy that will merge prop values using the supplied23* `mergeStrategy`. If a prop was previously unset, this just sets it.24*25* @param {function} mergeStrategy26* @return {function}27*/28function createTransferStrategy(mergeStrategy) {29return function(props, key, value) {30if (!props.hasOwnProperty(key)) {31props[key] = value;32} else {33props[key] = mergeStrategy(props[key], value);34}35};36}3738var transferStrategyMerge = createTransferStrategy(function(a, b) {39// `merge` overrides the first object's (`props[key]` above) keys using the40// second object's (`value`) keys. An object's style's existing `propA` would41// get overridden. Flip the order here.42return assign({}, b, a);43});4445/**46* Transfer strategies dictate how props are transferred by `transferPropsTo`.47* NOTE: if you add any more exceptions to this list you should be sure to48* update `cloneWithProps()` accordingly.49*/50var TransferStrategies = {51/**52* Never transfer `children`.53*/54children: emptyFunction,55/**56* Transfer the `className` prop by merging them.57*/58className: createTransferStrategy(joinClasses),59/**60* Transfer the `style` prop (which is an object) by merging them.61*/62style: transferStrategyMerge63};6465/**66* Mutates the first argument by transferring the properties from the second67* argument.68*69* @param {object} props70* @param {object} newProps71* @return {object}72*/73function transferInto(props, newProps) {74for (var thisKey in newProps) {75if (!newProps.hasOwnProperty(thisKey)) {76continue;77}7879var transferStrategy = TransferStrategies[thisKey];8081if (transferStrategy && TransferStrategies.hasOwnProperty(thisKey)) {82transferStrategy(props, thisKey, newProps[thisKey]);83} else if (!props.hasOwnProperty(thisKey)) {84props[thisKey] = newProps[thisKey];85}86}87return props;88}8990/**91* ReactPropTransferer are capable of transferring props to another component92* using a `transferPropsTo` method.93*94* @class ReactPropTransferer95*/96var ReactPropTransferer = {9798TransferStrategies: TransferStrategies,99100/**101* Merge two props objects using TransferStrategies.102*103* @param {object} oldProps original props (they take precedence)104* @param {object} newProps new props to merge in105* @return {object} a new object containing both sets of props merged.106*/107mergeProps: function(oldProps, newProps) {108return transferInto(assign({}, oldProps), newProps);109},110111/**112* @lends {ReactPropTransferer.prototype}113*/114Mixin: {115116/**117* Transfer props from this component to a target component.118*119* Props that do not have an explicit transfer strategy will be transferred120* only if the target component does not already have the prop set.121*122* This is usually used to pass down props to a returned root component.123*124* @param {ReactElement} element Component receiving the properties.125* @return {ReactElement} The supplied `component`.126* @final127* @protected128*/129transferPropsTo: function(element) {130invariant(131element._owner === this,132'%s: You can\'t call transferPropsTo() on a component that you ' +133'don\'t own, %s. This usually means you are calling ' +134'transferPropsTo() on a component passed in as props or children.',135this.constructor.displayName,136typeof element.type === 'string' ?137element.type :138element.type.displayName139);140141if (__DEV__) {142if (!didWarn) {143didWarn = true;144warning(145false,146'transferPropsTo is deprecated. ' +147'See http://fb.me/react-transferpropsto for more information.'148);149}150}151152// Because elements are immutable we have to merge into the existing153// props object rather than clone it.154transferInto(element.props, this.props);155156return element;157}158159}160};161162module.exports = ReactPropTransferer;163164165