UNPKG

antd

Version:

An enterprise-class UI design language and React-based implementation

133 lines (121 loc) 4.7 kB
import _extends from 'babel-runtime/helpers/extends'; import _defineProperty from 'babel-runtime/helpers/defineProperty'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import * as React from 'react'; import omit from 'omit.js'; import classNames from 'classnames'; import calculateNodeHeight from './calculateNodeHeight'; function onNextFrame(cb) { if (window.requestAnimationFrame) { return window.requestAnimationFrame(cb); } return window.setTimeout(cb, 1); } function clearNextFrameAction(nextFrameId) { if (window.cancelAnimationFrame) { window.cancelAnimationFrame(nextFrameId); } else { window.clearTimeout(nextFrameId); } } var TextArea = function (_React$Component) { _inherits(TextArea, _React$Component); function TextArea() { _classCallCheck(this, TextArea); var _this = _possibleConstructorReturn(this, (TextArea.__proto__ || Object.getPrototypeOf(TextArea)).apply(this, arguments)); _this.state = { textareaStyles: {} }; _this.resizeTextarea = function () { var autosize = _this.props.autosize; if (!autosize || !_this.textAreaRef) { return; } var minRows = autosize ? autosize.minRows : null; var maxRows = autosize ? autosize.maxRows : null; var textareaStyles = calculateNodeHeight(_this.textAreaRef, false, minRows, maxRows); _this.setState({ textareaStyles: textareaStyles }); }; _this.handleTextareaChange = function (e) { if (!('value' in _this.props)) { _this.resizeTextarea(); } var onChange = _this.props.onChange; if (onChange) { onChange(e); } }; _this.handleKeyDown = function (e) { var _this$props = _this.props, onPressEnter = _this$props.onPressEnter, onKeyDown = _this$props.onKeyDown; if (e.keyCode === 13 && onPressEnter) { onPressEnter(e); } if (onKeyDown) { onKeyDown(e); } }; _this.saveTextAreaRef = function (textArea) { _this.textAreaRef = textArea; }; return _this; } _createClass(TextArea, [{ key: 'componentDidMount', value: function componentDidMount() { this.resizeTextarea(); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { // Re-render with the new content then recalculate the height as required. if (this.props.value !== nextProps.value) { if (this.nextFrameActionId) { clearNextFrameAction(this.nextFrameActionId); } this.nextFrameActionId = onNextFrame(this.resizeTextarea); } } }, { key: 'focus', value: function focus() { this.textAreaRef.focus(); } }, { key: 'blur', value: function blur() { this.textAreaRef.blur(); } }, { key: 'getTextAreaClassName', value: function getTextAreaClassName() { var _props = this.props, prefixCls = _props.prefixCls, className = _props.className, disabled = _props.disabled; return classNames(prefixCls, className, _defineProperty({}, prefixCls + '-disabled', disabled)); } }, { key: 'render', value: function render() { var props = this.props; var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'autosize']); var style = _extends({}, props.style, this.state.textareaStyles); // Fix https://github.com/ant-design/ant-design/issues/6776 // Make sure it could be reset when using form.getFieldDecorator if ('value' in otherProps) { otherProps.value = otherProps.value || ''; } return React.createElement('textarea', _extends({}, otherProps, { className: this.getTextAreaClassName(), style: style, onKeyDown: this.handleKeyDown, onChange: this.handleTextareaChange, ref: this.saveTextAreaRef })); } }]); return TextArea; }(React.Component); export default TextArea; TextArea.defaultProps = { prefixCls: 'ant-input' };