|
|
|
import * as React from 'react';
|
|
|
|
import * as PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import { polyfill } from 'react-lifecycles-compat';
|
|
|
|
import Group from './Group';
|
|
|
|
import Search from './Search';
|
|
|
|
import TextArea from './TextArea';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import Password from './Password';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import { Omit, tuple } from '../_util/type';
|
|
|
|
|
|
|
|
function fixControlledValue<T>(value: T) {
|
|
|
|
if (typeof value === 'undefined' || value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const InputSizes = tuple('small', 'default', 'large');
|
|
|
|
|
|
|
|
export interface InputProps
|
|
|
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size' | 'prefix'> {
|
|
|
|
prefixCls?: string;
|
|
|
|
size?: (typeof InputSizes)[number];
|
|
|
|
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
|
|
|
|
addonBefore?: React.ReactNode;
|
|
|
|
addonAfter?: React.ReactNode;
|
|
|
|
prefix?: React.ReactNode;
|
|
|
|
suffix?: React.ReactNode;
|
|
|
|
allowClear?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Input extends React.Component<InputProps, any> {
|
|
|
|
static Group: typeof Group;
|
|
|
|
static Search: typeof Search;
|
|
|
|
static TextArea: typeof TextArea;
|
|
|
|
static Password: typeof Password;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
type: 'text',
|
|
|
|
disabled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
type: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
|
|
|
size: PropTypes.oneOf(InputSizes),
|
|
|
|
maxLength: PropTypes.number,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
value: PropTypes.any,
|
|
|
|
defaultValue: PropTypes.any,
|
|
|
|
className: PropTypes.string,
|
|
|
|
addonBefore: PropTypes.node,
|
|
|
|
addonAfter: PropTypes.node,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
onPressEnter: PropTypes.func,
|
|
|
|
onKeyDown: PropTypes.func,
|
|
|
|
onKeyUp: PropTypes.func,
|
|
|
|
onFocus: PropTypes.func,
|
|
|
|
onBlur: PropTypes.func,
|
|
|
|
prefix: PropTypes.node,
|
|
|
|
suffix: PropTypes.node,
|
|
|
|
allowClear: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(nextProps: InputProps, state: any) {
|
|
|
|
if ('value' in nextProps) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
value: nextProps.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
input: HTMLInputElement;
|
|
|
|
|
|
|
|
constructor(props: InputProps) {
|
|
|
|
super(props);
|
|
|
|
const value = props.value || props.defaultValue;
|
|
|
|
this.state = {
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
const { onPressEnter, onKeyDown } = this.props;
|
|
|
|
if (e.keyCode === 13 && onPressEnter) {
|
|
|
|
onPressEnter(e);
|
|
|
|
}
|
|
|
|
if (onKeyDown) {
|
|
|
|
onKeyDown(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.input.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
select() {
|
|
|
|
this.input. |