react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / addons / link / ReactLink.js
81155 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 ReactLink9* @typechecks static-only10*/1112"use strict";1314/**15* ReactLink encapsulates a common pattern in which a component wants to modify16* a prop received from its parent. ReactLink allows the parent to pass down a17* value coupled with a callback that, when invoked, expresses an intent to18* modify that value. For example:19*20* React.createClass({21* getInitialState: function() {22* return {value: ''};23* },24* render: function() {25* var valueLink = new ReactLink(this.state.value, this._handleValueChange);26* return <input valueLink={valueLink} />;27* },28* this._handleValueChange: function(newValue) {29* this.setState({value: newValue});30* }31* });32*33* We have provided some sugary mixins to make the creation and34* consumption of ReactLink easier; see LinkedValueUtils and LinkedStateMixin.35*/3637var React = require('React');3839/**40* @param {*} value current value of the link41* @param {function} requestChange callback to request a change42*/43function ReactLink(value, requestChange) {44this.value = value;45this.requestChange = requestChange;46}4748/**49* Creates a PropType that enforces the ReactLink API and optionally checks the50* type of the value being passed inside the link. Example:51*52* MyComponent.propTypes = {53* tabIndexLink: ReactLink.PropTypes.link(React.PropTypes.number)54* }55*/56function createLinkTypeChecker(linkType) {57var shapes = {58value: typeof linkType === 'undefined' ?59React.PropTypes.any.isRequired :60linkType.isRequired,61requestChange: React.PropTypes.func.isRequired62};63return React.PropTypes.shape(shapes);64}6566ReactLink.PropTypes = {67link: createLinkTypeChecker68};6970module.exports = ReactLink;717273