import DownOutlined from '@ant-design/icons/DownOutlined'; import UpOutlined from '@ant-design/icons/UpOutlined'; import classNames from 'classnames'; import RcInputNumber, { InputNumberProps as RcInputNumberProps } from 'rc-input-number'; import * as React from 'react'; import { useContext } from 'react'; import { ConfigContext } from '../config-provider'; import SizeContext, { SizeType } from '../config-provider/SizeContext'; import { FormItemInputContext, NoFormStatus } from '../form/context'; import { cloneElement } from '../_util/reactNode'; import { getStatusClassNames, InputStatus, getMergedStatus } from '../_util/statusUtils'; type ValueType = string | number; export interface InputNumberProps extends Omit, 'prefix' | 'size' | 'controls'> { prefixCls?: string; addonBefore?: React.ReactNode; addonAfter?: React.ReactNode; prefix?: React.ReactNode; size?: SizeType; bordered?: boolean; status?: InputStatus; controls?: boolean | { upIcon?: React.ReactNode; downIcon?: React.ReactNode }; } const InputNumber = React.forwardRef((props, ref) => { const { getPrefixCls, direction } = React.useContext(ConfigContext); const size = React.useContext(SizeContext); const [focused, setFocus] = React.useState(false); const inputRef = React.useRef(null); React.useImperativeHandle(ref, () => inputRef.current!); const { className, size: customizeSize, prefixCls: customizePrefixCls, addonBefore, addonAfter, prefix, bordered = true, readOnly, status: customStatus, controls, ...others } = props; const prefixCls = getPrefixCls('input-number', customizePrefixCls); let upIcon = ; let downIcon = ; const controlsTemp = typeof controls === 'boolean' ? controls : undefined; if (typeof controls === 'object') { upIcon = typeof controls.upIcon === 'undefined' ? ( upIcon ) : ( {controls.upIcon} ); downIcon = typeof controls.downIcon === 'undefined' ? ( downIcon ) : ( {controls.downIcon} ); } const { hasFeedback, status: contextStatus, isFormItemInput, feedbackIcon, } = useContext(FormItemInputContext); const mergedStatus = getMergedStatus(contextStatus, customStatus); const mergeSize = customizeSize || size; const inputNumberClass = classNames( { [`${prefixCls}-lg`]: mergeSize === 'large', [`${prefixCls}-sm`]: mergeSize === 'small', [`${prefixCls}-rtl`]: direction === 'rtl', [`${prefixCls}-readonly`]: readOnly, [`${prefixCls}-borderless`]: !bordered, [`${prefixCls}-in-form-item`]: isFormItemInput, }, getStatusClassNames(prefixCls, mergedStatus), className, ); let element = ( ); if (prefix != null || hasFeedback) { const affixWrapperCls = classNames( `${prefixCls}-affix-wrapper`, getStatusClassNames(`${prefixCls}-affix-wrapper`, mergedStatus, hasFeedback), { [`${prefixCls}-affix-wrapper-focused`]: focused, [`${prefixCls}-affix-wrapper-disabled`]: props.disabled, [`${prefixCls}-affix-wrapper-sm`]: size === 'small', [`${prefixCls}-affix-wrapper-lg`]: size === 'large', [`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl', [`${prefixCls}-affix-wrapper-readonly`]: readOnly, [`${prefixCls}-affix-wrapper-borderless`]: !bordered, // className will go to addon wrapper [`${className}`]: !(addonBefore || addonAfter) && className, }, ); element = (
inputRef.current!.focus()} > {prefix && {prefix}} {cloneElement(element, { style: null, value: props.value, onFocus: (event: React.FocusEvent) => { setFocus(true); props.onFocus?.(event); }, onBlur: (event: React.FocusEvent) => { setFocus(false); props.onBlur?.(event); }, })} {hasFeedback && {feedbackIcon}}
); } if (addonBefore != null || addonAfter != null) { const wrapperClassName = `${prefixCls}-group`; const addonClassName = `${wrapperClassName}-addon`; const addonBeforeNode = addonBefore ? (
{addonBefore}
) : null; const addonAfterNode = addonAfter ?
{addonAfter}
: null; const mergedWrapperClassName = classNames(`${prefixCls}-wrapper`, wrapperClassName, { [`${wrapperClassName}-rtl`]: direction === 'rtl', }); const mergedGroupClassName = classNames( `${prefixCls}-group-wrapper`, { [`${prefixCls}-group-wrapper-sm`]: size === 'small', [`${prefixCls}-group-wrapper-lg`]: size === 'large', [`${prefixCls}-group-wrapper-rtl`]: direction === 'rtl', }, getStatusClassNames(`${prefixCls}-group-wrapper`, mergedStatus, hasFeedback), className, ); element = (
{addonBeforeNode && {addonBeforeNode}} {cloneElement(element, { style: null })} {addonAfterNode && {addonAfterNode}}
); } return element; }); export default InputNumber as (( props: React.PropsWithChildren> & { ref?: React.Ref; }, ) => React.ReactElement) & { displayName?: string };