forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change LinkedValueMixin to a util class
This makes it clear that .getValue() is not a public API.
- Loading branch information
1 parent
e3248ef
commit 95a8059
Showing
6 changed files
with
118 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright 2013 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @providesModule LinkedValueUtils | ||
* @typechecks static-only | ||
*/ | ||
|
||
"use strict"; | ||
|
||
var invariant = require('invariant'); | ||
|
||
var hasReadOnlyValue = { | ||
'checkbox': true, | ||
'hidden': true, | ||
'radio': true | ||
}; | ||
|
||
function _assertLink(input) { | ||
invariant( | ||
input.props.value == null && input.props.onChange == null, | ||
'Cannot provide a valueLink and a value or onChange event. If you want ' + | ||
'to use value or onChange, you probably don\'t want to use valueLink.' | ||
); | ||
} | ||
|
||
/** | ||
* @param {SyntheticEvent} e change event to handle | ||
*/ | ||
function _handleLinkedValueChange(e) { | ||
/*jshint validthis:true */ | ||
this.props.valueLink.requestChange(e.target.value); | ||
} | ||
|
||
/** | ||
* Provide a linked `value` attribute for controlled forms. You should not use | ||
* this outside of the ReactDOM controlled form components. | ||
*/ | ||
var LinkedValueUtils = { | ||
Mixin: { | ||
propTypes: { | ||
value: function(props, propName, componentName) { | ||
if (__DEV__) { | ||
if (props[propName] && | ||
!hasReadOnlyValue[props.type] && | ||
!props.onChange && | ||
!props.readOnly && | ||
!props.disabled) { | ||
console.warn( | ||
'You provided a `value` prop to a form field without an ' + | ||
'`onChange` handler. This will render a read-only field. If ' + | ||
'the field should be mutable use `defaultValue`. Otherwise, ' + | ||
'set either `onChange` or `readOnly`.' | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* @param {ReactComponent} input Form component | ||
* @return {*} current value of the input either from value prop or link. | ||
*/ | ||
getValue: function(input) { | ||
if (input.props.valueLink) { | ||
_assertLink(input); | ||
return input.props.valueLink.value; | ||
} | ||
return input.props.value; | ||
}, | ||
|
||
/** | ||
* @param {ReactComponent} input Form component | ||
* @return {function} change callback either from onChange prop or link. | ||
*/ | ||
getOnChange: function(input) { | ||
if (input.props.valueLink) { | ||
_assertLink(input); | ||
return _handleLinkedValueChange; | ||
} | ||
return input.props.onChange; | ||
} | ||
}; | ||
|
||
module.exports = LinkedValueUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters