ant-design/components/input/Input.tsx

223 lines
5.4 KiB
TypeScript
Raw Normal View History

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';
import calculateNodeHeight from './calculateNodeHeight';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
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);
}
}
interface AutoSizeType {
minRows?: number;
maxRows?: number;
};
export interface InputProps {
prefixCls?: string;
2016-08-22 17:26:14 +08:00
className?: string;
type: string;
id?: number | string;
value?: any;
defaultValue?: any;
2016-08-22 17:26:14 +08:00
placeholder?: string;
size?: 'large' | 'default' | 'small';
disabled?: boolean;
2016-08-22 17:26:14 +08:00
readOnly?: boolean;
addonBefore?: React.ReactNode;
addonAfter?: React.ReactNode;
2016-08-22 17:26:14 +08:00
onPressEnter?: React.FormEventHandler;
onKeyDown?: React.FormEventHandler;
onChange?: React.FormEventHandler;
autosize?: boolean | AutoSizeType;
}
export default class Input extends Component<InputProps, any> {
2016-08-22 17:26:14 +08:00
static Group: any;
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-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,
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
nextFrameActionId: number;
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;
}
2016-08-22 17:26:14 +08:00
const minRows = autosize ? (autosize as AutoSizeType).minRows : null;
const maxRows = autosize ? (autosize as AutoSizeType).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);
// 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}
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}
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());
}
}