ant-design/components/input/Input.jsx

197 lines
4.8 KiB
React
Raw Normal View History

2016-06-10 15:29:01 +08:00
import React, { Component, PropTypes } from 'react';
2016-03-31 17:46:35 +08:00
import classNames from 'classnames';
import calculateNodeHeight from './calculateNodeHeight';
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;
}
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() {},
onChange() {},
autosize: false,
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,
autosize: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
2016-06-10 15:29:01 +08:00
onPressEnter: PropTypes.func,
onKeyDown: PropTypes.func,
2016-04-06 18:12:41 +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
}
handleTextareaChange = (e) => {
if (!('value' in this.props)) {
this.resizeTextarea();
}
this.props.onChange(e);
}
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() {
const props = { ...this.props };
// 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) {
otherProps.value = fixControlledValue(props.value);
// Input elements must be either controlled or uncontrolled,
// specify either the value prop, or the defaultValue prop, but not both.
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
{...otherProps}
style={{
...props.style,
...this.state.textareaStyles,
}}
2016-06-10 15:29:01 +08:00
className={inputClassName}
onKeyDown={this.handleKeyDown}
onChange={this.handleTextareaChange}
2016-06-10 15:29:01 +08:00
ref="input"
/>
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
{...otherProps}
2016-06-10 15:29:01 +08:00
className={inputClassName}
onKeyDown={this.handleKeyDown}
ref="input"
/>
2016-04-06 18:12:41 +08:00
);
2016-03-31 17:46:35 +08:00
}
}
render() {
return this.renderLabledInput(this.renderInput());
}
}