ant-design/components/input/Input.tsx

214 lines
5.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as 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';
import { Omit } from '../_util/type';
2016-03-31 17:46:35 +08:00
function fixControlledValue<T>(value: T) {
2016-03-31 17:46:35 +08:00
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
export interface InputProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size' | 'prefix'> {
prefixCls?: string;
size?: 'large' | 'default' | 'small';
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
addonBefore?: React.ReactNode;
addonAfter?: React.ReactNode;
prefix?: React.ReactNode;
suffix?: React.ReactNode;
}
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,
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
}
select() {
this.input.select();
}
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
return (
2018-07-31 16:40:48 +08:00
<span
className={groupClassName}
style={props.style}
>
<span className={className}>
{addonBefore}
{React.cloneElement(children, { style: null })}
{addonAfter}
</span>
2016-03-31 17:46:35 +08:00
</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
}
}