ant-design/components/input/Input.tsx

243 lines
6.3 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import PropTypes from 'prop-types';
2016-03-31 17:46:35 +08:00
import classNames from 'classnames';
import omit from 'omit.js';
2017-05-22 14:44:58 +08:00
import Group from './Group';
import Search from './Search';
import TextArea from './TextArea';
2016-03-31 17:46:35 +08:00
2017-11-21 19:51:31 +08:00
function fixControlledValue(value: undefined | null | string) {
2016-03-31 17:46:35 +08:00
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
2017-05-22 14:44:58 +08:00
export interface AbstractInputProps {
prefixCls?: string;
2016-08-22 17:26:14 +08:00
className?: string;
2017-05-22 14:44:58 +08:00
defaultValue?: any;
value?: any;
tabIndex?: number;
2017-05-22 14:44:58 +08:00
style?: React.CSSProperties;
}
export interface InputProps extends AbstractInputProps {
placeholder?: string;
type?: string;
id?: number | string;
2017-01-05 17:19:50 +08:00
name?: string;
size?: 'large' | 'default' | 'small';
2018-01-20 19:35:14 +08:00
maxLength?: number | string;
disabled?: boolean;
2016-08-22 17:26:14 +08:00
readOnly?: boolean;
addonBefore?: React.ReactNode;
addonAfter?: React.ReactNode;
2017-12-28 10:51:12 +08:00
onPressEnter?: React.FormEventHandler<HTMLInputElement>;
onKeyDown?: React.FormEventHandler<HTMLInputElement>;
onKeyUp?: React.FormEventHandler<HTMLInputElement>;
2017-07-19 13:51:51 +08:00
onChange?: React.ChangeEventHandler<HTMLInputElement>;
2017-12-28 10:51:12 +08:00
onClick?: React.FormEventHandler<HTMLInputElement>;
onFocus?: React.FormEventHandler<HTMLInputElement>;
onBlur?: React.FormEventHandler<HTMLInputElement>;
autoComplete?: string;
prefix?: React.ReactNode;
suffix?: React.ReactNode;
2017-05-16 12:02:52 +08:00
spellCheck?: boolean;
autoFocus?: boolean;
}
export default class Input extends React.Component<InputProps, any> {
2017-05-22 14:44:58 +08:00
static Group: typeof Group;
static Search: typeof Search;
static TextArea: typeof TextArea;
2016-03-31 17:46:35 +08:00
static defaultProps = {
prefixCls: 'ant-input',
type: 'text',
2017-05-22 14:44:58 +08:00
disabled: 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']),
2018-01-20 19:35:14 +08:00
maxLength: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
2016-06-10 15:29:01 +08:00
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,
onKeyUp: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
prefix: PropTypes.node,
suffix: PropTypes.node,
2016-07-13 11:14:24 +08:00
};
2016-04-06 18:12:41 +08:00
input: HTMLInputElement;
2017-11-21 19:51:31 +08:00
handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
2016-10-24 12:04:26 +08:00
const { onPressEnter, onKeyDown } = this.props;
if (e.keyCode === 13 && onPressEnter) {
onPressEnter(e);
}
if (onKeyDown) {
onKeyDown(e);
2016-04-06 18:12:41 +08:00
}
2016-03-31 17:46:35 +08:00
}
2016-12-07 11:08:30 +08:00
focus() {
this.input.focus();
2016-12-07 11:08:30 +08:00
}
2017-05-22 14:44:58 +08:00
blur() {
this.input.blur();
2017-05-22 14:44:58 +08:00
}
getInputClassName() {
const { prefixCls, size, disabled } = this.props;
return classNames(prefixCls, {
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-disabled`]: disabled,
});
}
2017-11-21 19:51:31 +08:00
saveInput = (node: HTMLInputElement) => {
this.input = node;
}
2017-11-21 19:51:31 +08:00
renderLabeledInput(children: React.ReactElement<any>) {
2016-03-31 17:46:35 +08:00
const props = this.props;
2016-12-04 16:42:49 +08:00
// Not wrap when there is not addons
2017-05-22 14:44:58 +08:00
if ((!props.addonBefore && !props.addonAfter)) {
2016-12-04 16:42:49 +08:00
return children;
}
2016-03-31 17:46:35 +08:00
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`, {
2016-03-31 17:46:35 +08:00
[wrapperClassName]: (addonBefore || addonAfter),
});
const groupClassName = classNames(`${props.prefixCls}-group-wrapper`, {
[`${props.prefixCls}-group-wrapper-sm`]: props.size === 'small',
[`${props.prefixCls}-group-wrapper-lg`]: props.size === 'large',
});
// Need another wrapper for changing display:table to display:inline-block
// and put style prop in wrapper
if (addonBefore || addonAfter) {
return (
<span
className={groupClassName}
style={props.style}
>
<span className={className}>
{addonBefore}
{React.cloneElement(children, { style: null })}
{addonAfter}
</span>
</span>
);
}
return (
2016-03-31 17:46:35 +08:00
<span className={className}>
{addonBefore}
{children}
{addonAfter}
</span>
);
}
2017-11-21 19:51:31 +08:00
renderLabeledIcon(children: React.ReactElement<any>) {
const { props } = this;
2017-05-22 14:44:58 +08:00
if (!('prefix' in props || 'suffix' in props)) {
return children;
}
const prefix = props.prefix ? (
<span className={`${props.prefixCls}-prefix`}>
{props.prefix}
</span>
) : null;
const suffix = props.suffix ? (
<span className={`${props.prefixCls}-suffix`}>
{props.suffix}
</span>
) : null;
const affixWrapperCls = classNames(props.className, `${props.prefixCls}-affix-wrapper`, {
[`${props.prefixCls}-affix-wrapper-sm`]: props.size === 'small',
[`${props.prefixCls}-affix-wrapper-lg`]: props.size === 'large',
});
return (
<span
className={affixWrapperCls}
style={props.style}
>
{prefix}
{React.cloneElement(children, { style: null, className: this.getInputClassName() })}
{suffix}
</span>
);
}
2016-03-31 17:46:35 +08:00
renderInput() {
const { value, className } = this.props;
// Fix https://fb.me/react-unknown-prop
const otherProps = omit(this.props, [
'prefixCls',
'onPressEnter',
'addonBefore',
'addonAfter',
'prefix',
'suffix',
]);
if ('value' in this.props) {
otherProps.value = fixControlledValue(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
}
2017-05-22 14:44:58 +08:00
return this.renderLabeledIcon(
<input
{...otherProps}
className={classNames(this.getInputClassName(), className)}
2017-05-22 14:44:58 +08:00
onKeyDown={this.handleKeyDown}
ref={this.saveInput}
2017-05-22 14:44:58 +08:00
/>,
);
2016-03-31 17:46:35 +08:00
}
render() {
return this.renderLabeledInput(this.renderInput());
2016-03-31 17:46:35 +08:00
}
}