|
|
|
import React, { Component, cloneElement } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import Group from './Group';
|
|
|
|
import Search from './Search';
|
|
|
|
import TextArea from './TextArea';
|
|
|
|
|
|
|
|
function fixControlledValue(value) {
|
|
|
|
if (typeof value === 'undefined' || value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AbstractInputProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
defaultValue?: any;
|
|
|
|
value?: any;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InputProps extends AbstractInputProps {
|
|
|
|
placeholder?: string;
|
|
|
|
type?: string;
|
|
|
|
id?: number | string;
|
|
|
|
name?: string;
|
|
|
|
size?: 'large' | 'default' | 'small';
|
|
|
|
disabled?: boolean;
|
|
|
|
readOnly?: boolean;
|
|
|
|
addonBefore?: React.ReactNode;
|
|
|
|
addonAfter?: React.ReactNode;
|
|
|
|
onPressEnter?: React.FormEventHandler<any>;
|
|
|
|
onKeyDown?: React.FormEventHandler<any>;
|
|
|
|
onChange?: React.FormEventHandler<any>;
|
|
|
|
onClick?: React.FormEventHandler<any>;
|
|
|
|
onFocus?: React.FormEventHandler<any>;
|
|
|
|
onBlur?: React.FormEventHandler<any>;
|
|
|
|
autoComplete?: 'on' | 'off';
|
|
|
|
prefix?: React.ReactNode;
|
|
|
|
suffix?: React.ReactNode;
|
|
|
|
spellCheck?: boolean;
|
|
|
|
autoFocus?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Input extends Component<InputProps, any> {
|
|
|
|
static Group: typeof Group;
|
|
|
|
static Search: typeof Search;
|
|
|
|
static TextArea: typeof TextArea;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-input',
|
|
|
|
type: 'text',
|
|
|
|
disabled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
type: PropTypes.string,
|
|
|
|
id: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number,
|
|
|
|
]),
|
|
|
|
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]),
|
|
|
|
onPressEnter: PropTypes.func,
|
|
|
|
onKeyDown: PropTypes.func,
|
|
|
|
onFocus: PropTypes.func,
|
|
|
|
onBlur: PropTypes.func,
|
|
|
|
prefix: PropTypes.node,
|
|
|
|
suffix: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
refs: {
|
|
|
|
input: HTMLInputElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
const { onPressEnter, onKeyDown } = this.props;
|
|
|
|
if (e.keyCode === 13 && onPressEnter) {
|
|
|
|
onPressEnter(e);
|
|
|
|
}
|
|
|
|
if (onKeyDown) {
|
|
|
|
onKeyDown(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.refs.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.refs.input.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLabeledInput(children) {
|
|
|
|
const props = this.props;
|
|
|
|
// Not wrap when there is not addons
|
|
|
|
if ((!props.addonBefore && !props.addonAfter)) {
|
|
|
|
return children;
|
|
<