Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, 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
* @providesModule ReactLink
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
/**
16
* ReactLink encapsulates a common pattern in which a component wants to modify
17
* a prop received from its parent. ReactLink allows the parent to pass down a
18
* value coupled with a callback that, when invoked, expresses an intent to
19
* modify that value. For example:
20
*
21
* React.createClass({
22
* getInitialState: function() {
23
* return {value: ''};
24
* },
25
* render: function() {
26
* var valueLink = new ReactLink(this.state.value, this._handleValueChange);
27
* return <input valueLink={valueLink} />;
28
* },
29
* this._handleValueChange: function(newValue) {
30
* this.setState({value: newValue});
31
* }
32
* });
33
*
34
* We have provided some sugary mixins to make the creation and
35
* consumption of ReactLink easier; see LinkedValueUtils and LinkedStateMixin.
36
*/
37
38
var React = require('React');
39
40
/**
41
* @param {*} value current value of the link
42
* @param {function} requestChange callback to request a change
43
*/
44
function ReactLink(value, requestChange) {
45
this.value = value;
46
this.requestChange = requestChange;
47
}
48
49
/**
50
* Creates a PropType that enforces the ReactLink API and optionally checks the
51
* type of the value being passed inside the link. Example:
52
*
53
* MyComponent.propTypes = {
54
* tabIndexLink: ReactLink.PropTypes.link(React.PropTypes.number)
55
* }
56
*/
57
function createLinkTypeChecker(linkType) {
58
var shapes = {
59
value: typeof linkType === 'undefined' ?
60
React.PropTypes.any.isRequired :
61
linkType.isRequired,
62
requestChange: React.PropTypes.func.isRequired
63
};
64
return React.PropTypes.shape(shapes);
65
}
66
67
ReactLink.PropTypes = {
68
link: createLinkTypeChecker
69
};
70
71
module.exports = ReactLink;
72
73