ant-design/components/input/Input.jsx

129 lines
3.2 KiB
React
Raw Normal View History

2016-03-31 17:46:35 +08:00
import React from 'react';
import classNames from 'classnames';
function ieGT9() {
if (typeof document === 'undefined') {
2016-03-31 17:46:35 +08:00
return false;
}
const documentMode = document.documentMode || 0;
return documentMode > 9;
}
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
export default class Input extends React.Component {
static defaultProps = {
defaultValue: '',
disabled: false,
prefixCls: 'ant-input',
type: 'text',
2016-04-06 18:12:41 +08:00
onPressEnter() {},
onKeyDown() {},
2016-03-31 17:46:35 +08:00
}
static propTypes = {
type: React.PropTypes.string,
id: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
size: React.PropTypes.oneOf(['small', 'default', 'large']),
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,
2016-04-06 18:12:41 +08:00
onPressEnter: React.PropTypes.func,
onKeyDown: React.PropTypes.func,
}
handleKeyDown = (e) => {
if (e.keyCode === 13) {
this.props.onPressEnter(e);
}
this.props.onKeyDown(e);
2016-03-31 17:46:35 +08:00
}
renderLabledInput(children) {
const props = this.props;
const wrapperClassName = `${props.prefixCls}-group`;
const addonClassName = `${wrapperClassName}-addon`;
const addonBefore = props.addonBefore ? (
<span className={addonClassName}>
{props.addonBefore}
</span>
) : null;
const addonAfter = props.addonAfter ? (
<span className={addonClassName}>
{props.addonAfter}
</span>
) : null;
const className = classNames({
[`${props.prefixCls}-wrapper`]: true,
[wrapperClassName]: (addonBefore || addonAfter),
});
return (
<span className={className}>
{addonBefore}
{children}
{addonAfter}
</span>
);
}
renderInput() {
const props = { ...this.props };
const prefixCls = props.prefixCls;
if (!props.type) {
return props.children;
}
const inputClassName = classNames({
[prefixCls]: true,
[`${prefixCls}-sm`]: props.size === 'small',
[`${prefixCls}-lg`]: props.size === 'large',
[props.className]: !!props.className,
});
let placeholder = props.placeholder;
if (placeholder && ieGT9()) {
placeholder = null;
}
if ('value' in props) {
props.value = fixControlledValue(props.value);
}
2016-04-14 11:22:53 +08:00
// Remove React warning.
// Input elements must be either controlled or uncontrolled
// (specify either the value prop, or the defaultValue prop, but not both).
delete props.defaultValue;
2016-03-31 17:46:35 +08:00
switch (props.type) {
case 'textarea':
2016-04-06 18:12:41 +08:00
return (
<textarea {...props} placeholder={placeholder}
className={inputClassName} onKeyDown={this.handleKeyDown} ref="input" />
);
2016-03-31 17:46:35 +08:00
default:
2016-04-06 18:12:41 +08:00
return (
<input {...props} placeholder={placeholder}
className={inputClassName} onKeyDown={this.handleKeyDown} ref="input" />
);
2016-03-31 17:46:35 +08:00
}
}
render() {
return this.renderLabledInput(this.renderInput());
}
}