2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-25 15:46:44 +08:00
|
|
|
import classNames from 'classnames';
|
2016-03-21 09:22:14 +08:00
|
|
|
import RcInputNumber from 'rc-input-number';
|
2018-07-25 15:14:16 +08:00
|
|
|
import Icon from '../icon';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2018-05-22 10:46:20 +08:00
|
|
|
import { Omit } from '../_util/type';
|
|
|
|
|
|
|
|
// omitting this attrs because they conflicts with the ones defined in InputNumberProps
|
|
|
|
export type OmitAttrs = 'defaultValue' | 'onChange' | 'size';
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export interface InputNumberProps
|
|
|
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, OmitAttrs> {
|
2016-08-19 17:11:06 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
min?: number;
|
|
|
|
max?: number;
|
|
|
|
value?: number;
|
|
|
|
step?: number | string;
|
|
|
|
defaultValue?: number;
|
2018-01-12 00:09:23 +08:00
|
|
|
tabIndex?: number;
|
2018-12-28 13:57:03 +08:00
|
|
|
onChange?: (value: number | undefined) => void;
|
2016-08-19 17:11:06 +08:00
|
|
|
disabled?: boolean;
|
|
|
|
size?: 'large' | 'small' | 'default';
|
2017-03-10 18:20:28 +08:00
|
|
|
formatter?: (value: number | string | undefined) => string;
|
2017-04-11 21:16:02 +08:00
|
|
|
parser?: (displayValue: string | undefined) => number;
|
2018-09-20 16:37:54 +08:00
|
|
|
decimalSeparator?: string;
|
2016-09-19 10:17:07 +08:00
|
|
|
placeholder?: string;
|
2016-10-10 22:38:47 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2017-05-27 09:31:06 +08:00
|
|
|
name?: string;
|
2017-08-18 18:17:06 +08:00
|
|
|
id?: string;
|
2017-07-04 14:02:56 +08:00
|
|
|
precision?: number;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class InputNumber extends React.Component<InputNumberProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
step: 1,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-09-15 15:47:06 +08:00
|
|
|
private inputNumberRef: any;
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
focus() {
|
|
|
|
this.inputNumberRef.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.inputNumberRef.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderInputNumber = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { className, size, prefixCls: customizePrefixCls, ...others } = this.props;
|
|
|
|
const prefixCls = getPrefixCls('input-number', customizePrefixCls);
|
2018-12-07 20:02:01 +08:00
|
|
|
const inputNumberClass = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
const upIcon = <Icon type="up" className={`${prefixCls}-handler-up-inner`} />;
|
|
|
|
const downIcon = <Icon type="down" className={`${prefixCls}-handler-down-inner`} />;
|
2015-11-25 15:46:44 +08:00
|
|
|
|
2018-07-25 15:14:16 +08:00
|
|
|
return (
|
|
|
|
<RcInputNumber
|
2018-12-07 20:02:01 +08:00
|
|
|
ref={(c: any) => (this.inputNumberRef = c)}
|
2018-07-25 15:14:16 +08:00
|
|
|
className={inputNumberClass}
|
|
|
|
upHandler={upIcon}
|
|
|
|
downHandler={downIcon}
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls={prefixCls}
|
2018-07-25 15:14:16 +08:00
|
|
|
{...others}
|
|
|
|
/>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-09-15 15:47:06 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderInputNumber}</ConfigConsumer>;
|
2015-07-01 14:48:47 +08:00
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|