ant-design/components/input/index.jsx

128 lines
3.0 KiB
React
Raw Normal View History

2015-10-09 13:53:04 +08:00
import React from 'react';
import assign from 'object-assign';
import classNames from 'classnames';
2015-10-09 13:53:04 +08:00
2015-11-09 10:58:52 +08:00
function ieGT9() {
if (typeof document === 'undefined') {
2015-11-09 10:58:52 +08:00
return false;
}
const documentMode = document.documentMode || 0;
return documentMode > 9;
}
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
2015-10-09 13:53:04 +08:00
class Group extends React.Component {
render() {
const className = classNames({
'ant-input-group': true,
[this.props.className]: !!this.props.className,
});
2015-10-09 13:53:04 +08:00
return (
2016-01-05 14:42:06 +08:00
<span className={className} style={this.props.style}>
2015-10-09 13:53:04 +08:00
{this.props.children}
2015-12-02 15:55:02 +08:00
</span>
2015-10-09 13:53:04 +08:00
);
}
}
Group.propTypes = {
children: React.PropTypes.any,
};
class Input extends React.Component {
renderLabledInput(children) {
const props = this.props;
2016-03-02 17:11:12 +08:00
const wrapperClassName = `${props.prefixCls}-group`;
const addonClassName = `${wrapperClassName}-addon`;
2015-10-09 13:53:04 +08:00
const addonBefore = props.addonBefore ? (
2015-10-27 19:23:45 +08:00
<span className={addonClassName}>
2015-10-09 13:53:04 +08:00
{props.addonBefore}
</span>
) : null;
const addonAfter = props.addonAfter ? (
2015-10-27 19:23:45 +08:00
<span className={addonClassName}>
2015-10-09 13:53:04 +08:00
{props.addonAfter}
</span>
) : null;
const className = classNames({
2016-03-02 17:11:12 +08:00
[`${props.prefixCls}-wrapper`]: true,
[wrapperClassName]: (addonBefore || addonAfter),
});
2016-03-02 17:11:12 +08:00
return (
<span className={className}>
2015-10-09 13:53:04 +08:00
{addonBefore}
{children}
{addonAfter}
2015-12-02 15:55:02 +08:00
</span>
);
2015-10-09 13:53:04 +08:00
}
renderInput() {
const props = assign({}, this.props);
2015-10-09 13:53:04 +08:00
const prefixCls = props.prefixCls;
if (!props.type) {
return props.children;
}
2016-03-02 17:11:12 +08:00
const inputClassName = classNames({
[prefixCls]: true,
[`${prefixCls}-sm`]: props.size === 'small',
[`${prefixCls}-lg`]: props.size === 'large',
[props.className]: !!props.className,
});
2015-11-09 10:58:52 +08:00
let placeholder = props.placeholder;
if (placeholder && ieGT9()) {
2015-11-09 10:58:52 +08:00
placeholder = null;
}
if ('value' in props) {
props.value = fixControlledValue(props.value);
}
2015-10-09 13:53:04 +08:00
switch (props.type) {
2016-01-21 22:45:21 +08:00
case 'textarea':
2016-03-02 17:11:12 +08:00
return <textarea {...props} placeholder={placeholder} className={inputClassName} ref="input" />;
2016-01-21 22:45:21 +08:00
default:
return <input {...props} placeholder={placeholder} className={inputClassName} ref="input" />;
2015-10-09 13:53:04 +08:00
}
}
render() {
return this.renderLabledInput(this.renderInput());
}
}
Input.propTypes = {
type: React.PropTypes.string,
id: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
2015-10-25 11:34:02 +08:00
size: React.PropTypes.oneOf(['small', 'default', 'large']),
2015-10-09 13:53:04 +08:00
disabled: React.PropTypes.bool,
value: React.PropTypes.any,
defaultValue: React.PropTypes.any,
className: React.PropTypes.string,
addonBefore: React.PropTypes.node,
addonAfter: React.PropTypes.node,
prefixCls: React.PropTypes.string,
};
Input.defaultProps = {
defaultValue: '',
disabled: false,
2016-03-02 17:11:12 +08:00
prefixCls: 'ant-input',
2015-10-29 08:39:35 +08:00
type: 'text',
2015-10-09 13:53:04 +08:00
};
module.exports = Input;
module.exports.Group = Group;