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';
|
2020-03-02 12:09:38 +08:00
|
|
|
import UpOutlined from '@ant-design/icons/UpOutlined';
|
|
|
|
import DownOutlined from '@ant-design/icons/DownOutlined';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
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';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext, { SizeType } from '../config-provider/SizeContext';
|
2018-05-22 10:46:20 +08:00
|
|
|
|
|
|
|
// 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;
|
2020-05-05 15:38:05 +08:00
|
|
|
onChange?: (value: number | string | undefined) => void;
|
2016-08-19 17:11:06 +08:00
|
|
|
disabled?: boolean;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2017-03-10 18:20:28 +08:00
|
|
|
formatter?: (value: number | string | undefined) => string;
|
2019-05-26 13:07:27 +08:00
|
|
|
parser?: (displayValue: string | undefined) => number | string;
|
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;
|
2019-08-19 16:32:19 +08:00
|
|
|
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 09:48:10 +08:00
|
|
|
const InputNumber = React.forwardRef<unknown, InputNumberProps>((props, ref) => {
|
|
|
|
const renderInputNumber = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
|
|
|
const { className, size: customizeSize, prefixCls: customizePrefixCls, ...others } = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('input-number', customizePrefixCls);
|
2019-11-28 12:34:33 +08:00
|
|
|
const upIcon = <UpOutlined className={`${prefixCls}-handler-up-inner`} />;
|
|
|
|
const downIcon = <DownOutlined className={`${prefixCls}-handler-down-inner`} />;
|
2015-11-25 15:46:44 +08:00
|
|
|
|
2018-07-25 15:14:16 +08:00
|
|
|
return (
|
2020-01-03 13:38:16 +08:00
|
|
|
<SizeContext.Consumer>
|
|
|
|
{size => {
|
|
|
|
const mergeSize = customizeSize || size;
|
|
|
|
const inputNumberClass = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergeSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergeSize === 'small',
|
2020-03-20 14:43:32 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-01-03 13:38:16 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RcInputNumber
|
2020-04-22 09:48:10 +08:00
|
|
|
ref={ref}
|
2020-01-03 13:38:16 +08:00
|
|
|
className={inputNumberClass}
|
|
|
|
upHandler={upIcon}
|
|
|
|
downHandler={downIcon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
{...others}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SizeContext.Consumer>
|
2018-07-25 15:14:16 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-09-15 15:47:06 +08:00
|
|
|
|
2020-04-22 09:48:10 +08:00
|
|
|
return <ConfigConsumer>{renderInputNumber}</ConfigConsumer>;
|
|
|
|
});
|
|
|
|
|
|
|
|
InputNumber.defaultProps = {
|
|
|
|
step: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default InputNumber;
|