2016-07-18 16:44:30 +08:00
|
|
|
import * as React from 'react';
|
2016-07-13 17:22:23 +08:00
|
|
|
import { Component, PropTypes } from 'react';
|
2016-03-31 17:46:35 +08:00
|
|
|
import classNames from 'classnames';
|
2016-06-10 16:44:01 +08:00
|
|
|
import calculateNodeHeight from './calculateNodeHeight';
|
2016-06-22 13:18:43 +08:00
|
|
|
import assign from 'object-assign';
|
2016-07-06 12:21:49 +08:00
|
|
|
import omit from 'object.omit';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
function fixControlledValue(value) {
|
|
|
|
if (typeof value === 'undefined' || value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:44:01 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 15:29:01 +08:00
|
|
|
export default class Input extends Component {
|
2016-03-31 17:46:35 +08:00
|
|
|
static defaultProps = {
|
|
|
|
defaultValue: '',
|
|
|
|
disabled: false,
|
|
|
|
prefixCls: 'ant-input',
|
|
|
|
type: 'text',
|
2016-04-06 18:12:41 +08:00
|
|
|
onPressEnter() {},
|
|
|
|
onKeyDown() {},
|
2016-08-11 15:27:59 +08:00
|
|
|
onChange() {},
|
2016-06-10 16:44:01 +08:00
|
|
|
autosize: false,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
2016-06-10 15:29:01 +08:00
|
|
|
type: PropTypes.string,
|
|
|
|
id: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
2016-03-31 17:46:35 +08:00
|
|
|
]),
|
2016-06-10 15:29:01 +08:00
|
|
|
size: PropTypes.oneOf(['small', 'default', 'large']),
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
value: PropTypes.any,
|
|
|
|
defaultValue: PropTypes.any,
|
|
|
|
className: PropTypes.string,
|
|
|
|
addonBefore: PropTypes.node,
|
|
|
|
addonAfter: PropTypes.node,
|
|
|
|
prefixCls: PropTypes.string,
|
2016-06-10 16:44:01 +08:00
|
|
|
autosize: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
2016-06-10 15:29:01 +08:00
|
|
|
onPressEnter: PropTypes.func,
|
|
|
|
onKeyDown: PropTypes.func,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-04-06 18:12:41 +08:00
|
|
|
|
2016-06-10 16:44:01 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
textareaStyles: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.resizeTextarea();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:12:41 +08:00
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
this.props.onPressEnter(e);
|
|
|
|
}
|
|
|
|
this.props.onKeyDown(e);
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
|
2016-06-10 16:44:01 +08:00
|
|
|
handleTextareaChange = (e) => {
|
2016-08-11 15:27:59 +08:00
|
|
|
if (!('value' in this.props)) {
|
|
|
|
this.resizeTextarea();
|
2016-06-10 16:44:01 +08:00
|
|
|
}
|
2016-08-11 15:27:59 +08:00
|
|
|
this.props.onChange(e);
|
2016-06-10 16:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resizeTextarea = () => {
|
|
|
|
const { type, autosize } = this.props;
|
|
|
|
if (type !== 'textarea' || !autosize || !this.refs.input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const minRows = autosize ? autosize.minRows : null;
|
|
|
|
const maxRows = autosize ? autosize.maxRows : null;
|
|
|
|
const textareaStyles = calculateNodeHeight(this.refs.input, false, minRows, maxRows);
|
|
|
|
this.setState({ textareaStyles });
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-06-22 13:18:43 +08:00
|
|
|
const props = assign({}, this.props);
|
2016-07-06 12:21:49 +08:00
|
|
|
|
|
|
|
// Fix https://fb.me/react-unknown-prop
|
|
|
|
const otherProps = omit(this.props, [
|
|
|
|
'prefixCls',
|
|
|
|
'onPressEnter',
|
|
|
|
'autosize',
|
|
|
|
'addonBefore',
|
|
|
|
'addonAfter',
|
|
|
|
]);
|
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
if ('value' in props) {
|
2016-07-10 15:09:34 +08:00
|
|
|
otherProps.value = fixControlledValue(props.value);
|
2016-04-14 11:58:01 +08:00
|
|
|
// Input elements must be either controlled or uncontrolled,
|
|
|
|
// specify either the value prop, or the defaultValue prop, but not both.
|
2016-07-06 12:21:49 +08:00
|
|
|
delete otherProps.defaultValue;
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|
2016-04-14 11:22:53 +08:00
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
switch (props.type) {
|
|
|
|
case 'textarea':
|
2016-04-06 18:12:41 +08:00
|
|
|
return (
|
2016-06-10 15:29:01 +08:00
|
|
|
<textarea
|
2016-07-06 12:21:49 +08:00
|
|
|
{...otherProps}
|
2016-06-22 13:18:43 +08:00
|
|
|
style={assign({}, props.style, this.state.textareaStyles)}
|
2016-06-10 15:29:01 +08:00
|
|
|
className={inputClassName}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
2016-06-10 16:44:01 +08:00
|
|
|
onChange={this.handleTextareaChange}
|
2016-06-10 15:29:01 +08:00
|
|
|
ref="input"
|
2016-06-06 13:54:10 +08:00
|
|
|
/>
|
2016-04-06 18:12:41 +08:00
|
|
|
);
|
2016-03-31 17:46:35 +08:00
|
|
|
default:
|
2016-04-06 18:12:41 +08:00
|
|
|
return (
|
2016-06-10 15:29:01 +08:00
|
|
|
<input
|
2016-07-06 12:21:49 +08:00
|
|
|
{...otherProps}
|
2016-06-10 15:29:01 +08:00
|
|
|
className={inputClassName}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
ref="input"
|
2016-06-06 13:54:10 +08:00
|
|
|
/>
|
2016-04-06 18:12:41 +08:00
|
|
|
);
|
2016-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return this.renderLabledInput(this.renderInput());
|
|
|
|
}
|
|
|
|
}
|