|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import Group from './Group';
|
|
|
|
import Search from './Search';
|
|
|
|
import TextArea from './TextArea';
|
|
|
|
import Password from './Password';
|
|
|
|
import { Omit } from '../_util/type';
|
|
|
|
import ClearableLabeledInput, { hasPrefixSuffix } from './ClearableLabeledInput';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import SizeContext, { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
|
|
|
|
export interface InputProps
|
|
|
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size' | 'prefix'> {
|
|
|
|
prefixCls?: string;
|
|
|
|
size?: SizeType;
|
|
|
|
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
|
|
|
|
addonBefore?: React.ReactNode;
|
|
|
|
addonAfter?: React.ReactNode;
|
|
|
|
prefix?: React.ReactNode;
|
|
|
|
suffix?: React.ReactNode;
|
|
|
|
allowClear?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function fixControlledValue<T>(value: T) {
|
|
|
|
if (typeof value === 'undefined' || value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveOnChange(
|
|
|
|
target: HTMLInputElement | HTMLTextAreaElement,
|
|
|
|
e:
|
|
|
|
| React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
|
|
|
|
| React.MouseEvent<HTMLElement, MouseEvent>,
|
|
|
|
onChange?: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void,
|
|
|
|
) {
|
|
|
|
if (onChange) {
|
|
|
|
let event = e;
|
|
|
|
if (e.type === 'click') {
|
|
|
|
// click clear icon
|
|
|
|
event = Object.create(e);
|
|
|
|
event.target = target;
|
|
|
|
event.currentTarget = target;
|
|
|
|
const originalInputValue = target.value;
|
|
|
|
// change target ref value cause e.target.value should be '' when clear input
|
|
|
|
target.value = '';
|
|
|
|
onChange(event as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>);
|
|
|
|
// reset target ref value
|
|
|
|
target.value = originalInputValue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange(event as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getInputClassName(
|
|
|
|
prefixCls: string,
|
|
|
|
size?: SizeType,
|
|
|
|
disabled?: boolean,
|
|
|
|
direction?: any,
|
|
|
|
) {
|
|
|
|
return classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InputState {
|
|
|
|
value: any;
|
|
|
|
/** `value` from prev props */
|
|
|
|
prevValue: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Input extends React.Component<InputProps, InputState> {
|
|
|
|
|