ant-design/components/input-number/index.tsx

78 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import RcInputNumber from 'rc-input-number';
2018-07-25 15:14:16 +08:00
import Icon from '../icon';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
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> {
prefixCls?: string;
min?: number;
max?: number;
value?: number;
step?: number | string;
defaultValue?: number;
tabIndex?: number;
2018-12-28 13:57:03 +08:00
onChange?: (value: number | undefined) => void;
disabled?: boolean;
size?: 'large' | 'small' | 'default';
2017-03-10 18:20:28 +08:00
formatter?: (value: number | string | undefined) => string;
parser?: (displayValue: string | undefined) => number;
decimalSeparator?: string;
placeholder?: string;
style?: React.CSSProperties;
className?: string;
name?: string;
id?: string;
precision?: number;
}
export default class InputNumber extends React.Component<InputNumberProps, any> {
static defaultProps = {
step: 1,
2016-07-13 11:14:24 +08:00
};
private inputNumberRef: any;
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`} />;
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}
prefixCls={prefixCls}
2018-07-25 15:14:16 +08:00
{...others}
/>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderInputNumber}</ConfigConsumer>;
2015-07-01 14:48:47 +08:00
}
}