react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / components / ReactDOMTextarea.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 ReactDOMTextarea9*/1011"use strict";1213var AutoFocusMixin = require('AutoFocusMixin');14var DOMPropertyOperations = require('DOMPropertyOperations');15var LinkedValueUtils = require('LinkedValueUtils');16var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');17var ReactCompositeComponent = require('ReactCompositeComponent');18var ReactElement = require('ReactElement');19var ReactDOM = require('ReactDOM');20var ReactUpdates = require('ReactUpdates');2122var assign = require('Object.assign');23var invariant = require('invariant');2425var warning = require('warning');2627// Store a reference to the <textarea> `ReactDOMComponent`. TODO: use string28var textarea = ReactElement.createFactory(ReactDOM.textarea.type);2930function forceUpdateIfMounted() {31/*jshint validthis:true */32if (this.isMounted()) {33this.forceUpdate();34}35}3637/**38* Implements a <textarea> native component that allows setting `value`, and39* `defaultValue`. This differs from the traditional DOM API because value is40* usually set as PCDATA children.41*42* If `value` is not supplied (or null/undefined), user actions that affect the43* value will trigger updates to the element.44*45* If `value` is supplied (and not null/undefined), the rendered element will46* not trigger updates to the element. Instead, the `value` prop must change in47* order for the rendered element to be updated.48*49* The rendered element will be initialized with an empty value, the prop50* `defaultValue` if specified, or the children content (deprecated).51*/52var ReactDOMTextarea = ReactCompositeComponent.createClass({53displayName: 'ReactDOMTextarea',5455mixins: [AutoFocusMixin, LinkedValueUtils.Mixin, ReactBrowserComponentMixin],5657getInitialState: function() {58var defaultValue = this.props.defaultValue;59// TODO (yungsters): Remove support for children content in <textarea>.60var children = this.props.children;61if (children != null) {62if (__DEV__) {63warning(64false,65'Use the `defaultValue` or `value` props instead of setting ' +66'children on <textarea>.'67);68}69invariant(70defaultValue == null,71'If you supply `defaultValue` on a <textarea>, do not pass children.'72);73if (Array.isArray(children)) {74invariant(75children.length <= 1,76'<textarea> can only have at most one child.'77);78children = children[0];79}8081defaultValue = '' + children;82}83if (defaultValue == null) {84defaultValue = '';85}86var value = LinkedValueUtils.getValue(this);87return {88// We save the initial value so that `ReactDOMComponent` doesn't update89// `textContent` (unnecessary since we update value).90// The initial value can be a boolean or object so that's why it's91// forced to be a string.92initialValue: '' + (value != null ? value : defaultValue)93};94},9596render: function() {97// Clone `this.props` so we don't mutate the input.98var props = assign({}, this.props);99100invariant(101props.dangerouslySetInnerHTML == null,102'`dangerouslySetInnerHTML` does not make sense on <textarea>.'103);104105props.defaultValue = null;106props.value = null;107props.onChange = this._handleChange;108109// Always set children to the same thing. In IE9, the selection range will110// get reset if `textContent` is mutated.111return textarea(props, this.state.initialValue);112},113114componentDidUpdate: function(prevProps, prevState, prevContext) {115var value = LinkedValueUtils.getValue(this);116if (value != null) {117var rootNode = this.getDOMNode();118// Cast `value` to a string to ensure the value is set correctly. While119// browsers typically do this as necessary, jsdom doesn't.120DOMPropertyOperations.setValueForProperty(rootNode, 'value', '' + value);121}122},123124_handleChange: function(event) {125var returnValue;126var onChange = LinkedValueUtils.getOnChange(this);127if (onChange) {128returnValue = onChange.call(this, event);129}130ReactUpdates.asap(forceUpdateIfMounted, this);131return returnValue;132}133134});135136module.exports = ReactDOMTextarea;137138139