2015-10-09 13:53:04 +08:00
|
|
|
import React from 'react';
|
2015-12-08 19:18:04 +08:00
|
|
|
import assign from 'object-assign';
|
2016-02-16 10:13:40 +08:00
|
|
|
import classNames from 'classnames';
|
2015-10-09 13:53:04 +08:00
|
|
|
|
|
|
|
function prefixClsFn(prefixCls, ...args) {
|
2016-01-05 14:42:06 +08:00
|
|
|
return args.map((s) => {
|
2016-02-17 18:04:42 +08:00
|
|
|
return `${prefixCls}-${s}`;
|
2015-10-09 13:53:04 +08:00
|
|
|
}).join(' ');
|
|
|
|
}
|
|
|
|
|
2015-11-09 10:58:52 +08:00
|
|
|
function ieGT9() {
|
|
|
|
if (typeof document === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const documentMode = document.documentMode || 0;
|
|
|
|
return documentMode > 9;
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:18:04 +08:00
|
|
|
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() {
|
2016-02-17 18:04:42 +08:00
|
|
|
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;
|
|
|
|
const wrapperClassName = prefixClsFn(props.prefixCls, 'input-group');
|
|
|
|
const addonClassName = prefixClsFn(wrapperClassName, 'addon');
|
|
|
|
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;
|
|
|
|
|
2016-02-16 10:13:40 +08:00
|
|
|
const className = classNames({
|
|
|
|
[`${props.prefixCls}-input-wrapper`]: true,
|
|
|
|
[wrapperClassName]: (addonBefore || addonAfter),
|
|
|
|
});
|
2015-10-31 08:43:38 +08:00
|
|
|
return (
|
2016-02-16 10:13:40 +08:00
|
|
|
<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-31 08:43:38 +08:00
|
|
|
);
|
2015-10-09 13:53:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderInput() {
|
2015-12-08 19:18:04 +08:00
|
|
|
const props = assign({}, this.props);
|
2015-10-09 13:53:04 +08:00
|
|
|
const prefixCls = props.prefixCls;
|
2015-10-25 11:34:02 +08:00
|
|
|
let inputClassName = prefixClsFn(prefixCls, 'input');
|
2015-10-09 13:53:04 +08:00
|
|
|
if (!props.type) {
|
|
|
|
return props.children;
|
|
|
|
}
|
|
|
|
|
2015-10-25 11:34:02 +08:00
|
|
|
switch (props.size) {
|
2016-01-21 22:45:21 +08:00
|
|
|
case 'small': inputClassName = prefixClsFn(prefixCls, 'input', 'input-sm'); break;
|
|
|
|
case 'large': inputClassName = prefixClsFn(prefixCls, 'input', 'input-lg'); break;
|
|
|
|
default:
|
2015-10-25 11:34:02 +08:00
|
|
|
}
|
2015-11-09 10:58:52 +08:00
|
|
|
let placeholder = props.placeholder;
|
2016-01-07 14:21:29 +08:00
|
|
|
if (placeholder && ieGT9()) {
|
2015-11-09 10:58:52 +08:00
|
|
|
placeholder = null;
|
|
|
|
}
|
2015-12-08 19:18:04 +08:00
|
|
|
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':
|
|
|
|
return (
|
2016-01-07 17:46:46 +08:00
|
|
|
<textarea {...props} placeholder={placeholder}
|
|
|
|
className={inputClassName} ref="input" />
|
|
|
|
);
|
2016-01-21 22:45:21 +08:00
|
|
|
default:
|
|
|
|
inputClassName = props.className ? props.className : inputClassName;
|
|
|
|
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,
|
|
|
|
prefixCls: 'ant',
|
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;
|