react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / components / LinkedValueUtils.js
81165 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 LinkedValueUtils9* @typechecks static-only10*/1112"use strict";1314var ReactPropTypes = require('ReactPropTypes');1516var invariant = require('invariant');1718var hasReadOnlyValue = {19'button': true,20'checkbox': true,21'image': true,22'hidden': true,23'radio': true,24'reset': true,25'submit': true26};2728function _assertSingleLink(input) {29invariant(30input.props.checkedLink == null || input.props.valueLink == null,31'Cannot provide a checkedLink and a valueLink. If you want to use ' +32'checkedLink, you probably don\'t want to use valueLink and vice versa.'33);34}35function _assertValueLink(input) {36_assertSingleLink(input);37invariant(38input.props.value == null && input.props.onChange == null,39'Cannot provide a valueLink and a value or onChange event. If you want ' +40'to use value or onChange, you probably don\'t want to use valueLink.'41);42}4344function _assertCheckedLink(input) {45_assertSingleLink(input);46invariant(47input.props.checked == null && input.props.onChange == null,48'Cannot provide a checkedLink and a checked property or onChange event. ' +49'If you want to use checked or onChange, you probably don\'t want to ' +50'use checkedLink'51);52}5354/**55* @param {SyntheticEvent} e change event to handle56*/57function _handleLinkedValueChange(e) {58/*jshint validthis:true */59this.props.valueLink.requestChange(e.target.value);60}6162/**63* @param {SyntheticEvent} e change event to handle64*/65function _handleLinkedCheckChange(e) {66/*jshint validthis:true */67this.props.checkedLink.requestChange(e.target.checked);68}6970/**71* Provide a linked `value` attribute for controlled forms. You should not use72* this outside of the ReactDOM controlled form components.73*/74var LinkedValueUtils = {75Mixin: {76propTypes: {77value: function(props, propName, componentName) {78if (!props[propName] ||79hasReadOnlyValue[props.type] ||80props.onChange ||81props.readOnly ||82props.disabled) {83return;84}85return new Error(86'You provided a `value` prop to a form field without an ' +87'`onChange` handler. This will render a read-only field. If ' +88'the field should be mutable use `defaultValue`. Otherwise, ' +89'set either `onChange` or `readOnly`.'90);91},92checked: function(props, propName, componentName) {93if (!props[propName] ||94props.onChange ||95props.readOnly ||96props.disabled) {97return;98}99return new Error(100'You provided a `checked` prop to a form field without an ' +101'`onChange` handler. This will render a read-only field. If ' +102'the field should be mutable use `defaultChecked`. Otherwise, ' +103'set either `onChange` or `readOnly`.'104);105},106onChange: ReactPropTypes.func107}108},109110/**111* @param {ReactComponent} input Form component112* @return {*} current value of the input either from value prop or link.113*/114getValue: function(input) {115if (input.props.valueLink) {116_assertValueLink(input);117return input.props.valueLink.value;118}119return input.props.value;120},121122/**123* @param {ReactComponent} input Form component124* @return {*} current checked status of the input either from checked prop125* or link.126*/127getChecked: function(input) {128if (input.props.checkedLink) {129_assertCheckedLink(input);130return input.props.checkedLink.value;131}132return input.props.checked;133},134135/**136* @param {ReactComponent} input Form component137* @return {function} change callback either from onChange prop or link.138*/139getOnChange: function(input) {140if (input.props.valueLink) {141_assertValueLink(input);142return _handleLinkedValueChange;143} else if (input.props.checkedLink) {144_assertCheckedLink(input);145return _handleLinkedCheckChange;146}147return input.props.onChange;148}149};150151module.exports = LinkedValueUtils;152153154