antd
Version:
An enterprise-class UI design language and React-based implementation
186 lines (171 loc) • 7.28 kB
JavaScript
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 * as PropTypes from 'prop-types';
import classNames from 'classnames';
import omit from 'omit.js';
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
var Input = function (_React$Component) {
_inherits(Input, _React$Component);
function Input() {
_classCallCheck(this, Input);
var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).apply(this, arguments));
_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.saveInput = function (node) {
_this.input = node;
};
return _this;
}
_createClass(Input, [{
key: 'focus',
value: function focus() {
this.input.focus();
}
}, {
key: 'blur',
value: function blur() {
this.input.blur();
}
}, {
key: 'getInputClassName',
value: function getInputClassName() {
var _classNames;
var _props = this.props,
prefixCls = _props.prefixCls,
size = _props.size,
disabled = _props.disabled;
return classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-sm', size === 'small'), _defineProperty(_classNames, prefixCls + '-lg', size === 'large'), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _classNames));
}
}, {
key: 'renderLabeledInput',
value: function renderLabeledInput(children) {
var _classNames3;
var props = this.props;
// Not wrap when there is not addons
if (!props.addonBefore && !props.addonAfter) {
return children;
}
var wrapperClassName = props.prefixCls + '-group';
var addonClassName = wrapperClassName + '-addon';
var addonBefore = props.addonBefore ? React.createElement(
'span',
{ className: addonClassName },
props.addonBefore
) : null;
var addonAfter = props.addonAfter ? React.createElement(
'span',
{ className: addonClassName },
props.addonAfter
) : null;
var className = classNames(props.prefixCls + '-wrapper', _defineProperty({}, wrapperClassName, addonBefore || addonAfter));
var groupClassName = classNames(props.prefixCls + '-group-wrapper', (_classNames3 = {}, _defineProperty(_classNames3, props.prefixCls + '-group-wrapper-sm', props.size === 'small'), _defineProperty(_classNames3, props.prefixCls + '-group-wrapper-lg', props.size === 'large'), _classNames3));
// Need another wrapper for changing display:table to display:inline-block
// and put style prop in wrapper
return React.createElement(
'span',
{ className: groupClassName, style: props.style },
React.createElement(
'span',
{ className: className },
addonBefore,
React.cloneElement(children, { style: null }),
addonAfter
)
);
}
}, {
key: 'renderLabeledIcon',
value: function renderLabeledIcon(children) {
var _classNames4;
var props = this.props;
if (!('prefix' in props || 'suffix' in props)) {
return children;
}
var prefix = props.prefix ? React.createElement(
'span',
{ className: props.prefixCls + '-prefix' },
props.prefix
) : null;
var suffix = props.suffix ? React.createElement(
'span',
{ className: props.prefixCls + '-suffix' },
props.suffix
) : null;
var affixWrapperCls = classNames(props.className, props.prefixCls + '-affix-wrapper', (_classNames4 = {}, _defineProperty(_classNames4, props.prefixCls + '-affix-wrapper-sm', props.size === 'small'), _defineProperty(_classNames4, props.prefixCls + '-affix-wrapper-lg', props.size === 'large'), _classNames4));
return React.createElement(
'span',
{ className: affixWrapperCls, style: props.style },
prefix,
React.cloneElement(children, { style: null, className: this.getInputClassName() }),
suffix
);
}
}, {
key: 'renderInput',
value: function renderInput() {
var _props2 = this.props,
value = _props2.value,
className = _props2.className;
// Fix https://fb.me/react-unknown-prop
var otherProps = omit(this.props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix']);
if ('value' in this.props) {
otherProps.value = fixControlledValue(value);
// Input elements must be either controlled or uncontrolled,
// specify either the value prop, or the defaultValue prop, but not both.
delete otherProps.defaultValue;
}
return this.renderLabeledIcon(React.createElement('input', _extends({}, otherProps, { className: classNames(this.getInputClassName(), className), onKeyDown: this.handleKeyDown, ref: this.saveInput })));
}
}, {
key: 'render',
value: function render() {
return this.renderLabeledInput(this.renderInput());
}
}]);
return Input;
}(React.Component);
export default Input;
Input.defaultProps = {
prefixCls: 'ant-input',
type: 'text',
disabled: false
};
Input.propTypes = {
type: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
size: PropTypes.oneOf(['small', 'default', 'large']),
maxLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
disabled: PropTypes.bool,
value: PropTypes.any,
defaultValue: PropTypes.any,
className: PropTypes.string,
addonBefore: PropTypes.node,
addonAfter: PropTypes.node,
prefixCls: PropTypes.string,
onPressEnter: PropTypes.func,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
prefix: PropTypes.node,
suffix: PropTypes.node
};