@ -10,6 +10,7 @@ import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import Password from './Password' ;
import Icon from '../icon' ;
import { Omit , tuple } from '../_util/type' ;
import warning from '../_util/warning' ;
function fixControlledValue < T > ( value : T ) {
if ( typeof value === 'undefined' || value === null ) {
@ -18,6 +19,10 @@ function fixControlledValue<T>(value: T) {
return value ;
}
function hasPrefixSuffix ( props : InputProps ) {
return 'prefix' in props || props . suffix || props . allowClear ;
}
const InputSizes = tuple ( 'small' , 'default' , 'large' ) ;
export interface InputProps
@ -84,6 +89,20 @@ class Input extends React.Component<InputProps, any> {
} ;
}
getSnapshotBeforeUpdate ( prevProps : InputProps ) {
if ( hasPrefixSuffix ( prevProps ) !== hasPrefixSuffix ( this . props ) ) {
warning (
this . input !== document . activeElement ,
` When Input is focused, dynamic add or remove prefix / suffix will make it lose focus caused by dom structure change. Read more: https://ant.design/components/input/#FAQ ` ,
) ;
}
return null ;
}
// Since polyfill `getSnapshotBeforeUpdate` need work with `componentDidUpdate`.
// We keep an empty function here.
componentDidUpdate() { }
handleKeyDown = ( e : React.KeyboardEvent < HTMLInputElement > ) = > {
const { onPressEnter , onKeyDown } = this . props ;
if ( e . keyCode === 13 && onPressEnter ) {
@ -224,7 +243,7 @@ class Input extends React.Component<InputProps, any> {
const { props } = this ;
const suffix = this . renderSuffix ( prefixCls ) ;
if ( ! ( 'prefix' in props ) && ! suffix ) {
if ( ! hasPrefixSuffix ( props ) ) {
return children ;
}