ant-design/components/input/Input.tsx

229 lines
5.7 KiB
TypeScript
Raw Normal View History

2017-05-22 14:44:58 +08:00
import React, { Component, cloneElement } 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
function fixControlledValue(value) {
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;
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';
maxLength?: string;
disabled?: boolean;
2016-08-22 17:26:14 +08:00
readOnly?: boolean;
addonBefore?: React.ReactNode;
addonAfter?: React.ReactNode;
2017-05-22 14:44:58 +08:00
onPressEnter?: React.FormEventHandler<any>;
2017-05-16 12:02:52 +08:00
onKeyDown?: React.FormEventHandler<any>;
2017-07-19 13:51:51 +08:00
onChange?: React.ChangeEventHandler<HTMLInputElement>;
2016-10-19 17:51:33 +08:00
onClick?: React.FormEventHandler<any>;
onFocus?: React.FormEventHandler<any>;
2016-10-19 17:51:33 +08:00
onBlur?: React.FormEventHandler<any>;
autoComplete?: string;
prefix?: React.ReactNode;
suffix?: React.ReactNode;
2017-05-16 12:02:52 +08:00
spellCheck?: boolean;
autoFocus?: boolean;
}
2017-05-16 12:02:52 +08:00
export default class Input extends 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']),
maxLength: PropTypes.string,
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,
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
2016-08-24 16:09:55 +08:00
refs: {
2017-05-22 14:44:58 +08:00
input: HTMLInputElement;
2016-11-15 15:18:40 +08:00
};
2016-04-06 18:12:41 +08:00
handleKeyDown = (e) => {
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.refs.input.focus();
}
2017-05-22 14:44:58 +08:00
blur() {
this.refs.input.blur();
}
getInputClassName() {
const { prefixCls, size, disabled } = this.props;
return classNames(prefixCls, {
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-disabled`]: disabled,
});
}
renderLabeledInput(children) {
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),
});
// Need another wrapper for changing display:table to display:inline-block
// and put style prop in wrapper
if (addonBefore || addonAfter) {
return (
<span
className={`${props.prefixCls}-group-wrapper`}
style={props.style}
>
<span className={className}>
{addonBefore}
{cloneElement(children, { style: null })}
{addonAfter}
</span>
</span>
);
}
return (
2016-03-31 17:46:35 +08:00
<span className={className}>
{addonBefore}
{children}
{addonAfter}
</span>
);
}
renderLabeledIcon(children) {
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;
return (
<span
className={classNames(props.className, `${props.prefixCls}-affix-wrapper`)}
style={props.style}
>
{prefix}
{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="input"
/>,
);
2016-03-31 17:46:35 +08:00
}
render() {
2017-05-22 14:44:58 +08:00
if (this.props.type === 'textarea') {
return <TextArea {...this.props as any} ref="input" />;
}
return this.renderLabeledInput(this.renderInput());
2016-03-31 17:46:35 +08:00
}
}