2022-02-16 11:48:24 +08:00
|
|
|
import DownOutlined from '@ant-design/icons/DownOutlined';
|
|
|
|
import UpOutlined from '@ant-design/icons/UpOutlined';
|
2015-11-25 15:46:44 +08:00
|
|
|
import classNames from 'classnames';
|
2022-04-06 21:49:30 +08:00
|
|
|
import type { InputNumberProps as RcInputNumberProps } from 'rc-input-number';
|
|
|
|
import RcInputNumber from 'rc-input-number';
|
2022-02-16 11:48:24 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useContext } from 'react';
|
2020-10-12 10:59:40 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-04-06 21:49:30 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2022-04-29 20:48:10 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext, NoFormStatus } from '../form/context';
|
2021-08-31 13:09:03 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-03-14 11:40:13 +08:00
|
|
|
import useStyle from './style';
|
2022-04-06 21:49:30 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getStatusClassNames, getMergedStatus } from '../_util/statusUtils';
|
2018-05-22 10:46:20 +08:00
|
|
|
|
2021-02-22 22:07:19 +08:00
|
|
|
type ValueType = string | number;
|
2018-05-22 10:46:20 +08:00
|
|
|
|
2021-02-22 22:07:19 +08:00
|
|
|
export interface InputNumberProps<T extends ValueType = ValueType>
|
2022-02-09 18:06:36 +08:00
|
|
|
extends Omit<RcInputNumberProps<T>, 'prefix' | 'size' | 'controls'> {
|
2016-08-19 17:11:06 +08:00
|
|
|
prefixCls?: string;
|
2021-08-31 13:09:03 +08:00
|
|
|
addonBefore?: React.ReactNode;
|
|
|
|
addonAfter?: React.ReactNode;
|
2021-10-30 22:25:12 +08:00
|
|
|
prefix?: React.ReactNode;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled?: boolean;
|
2021-02-01 17:06:20 +08:00
|
|
|
bordered?: boolean;
|
2022-02-16 11:48:24 +08:00
|
|
|
status?: InputStatus;
|
2022-02-09 18:06:36 +08:00
|
|
|
controls?: boolean | { upIcon?: React.ReactNode; downIcon?: React.ReactNode };
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-22 22:07:19 +08:00
|
|
|
const InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {
|
2022-04-06 21:49:30 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-10-12 10:59:40 +08:00
|
|
|
const size = React.useContext(SizeContext);
|
2021-10-30 22:25:12 +08:00
|
|
|
const [focused, setFocus] = React.useState(false);
|
|
|
|
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => inputRef.current!);
|
2015-11-25 15:46:44 +08:00
|
|
|
|
2020-10-12 10:59:40 +08:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
size: customizeSize,
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled: customDisabled,
|
2020-10-12 10:59:40 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2021-08-31 13:09:03 +08:00
|
|
|
addonBefore,
|
|
|
|
addonAfter,
|
2021-10-30 22:25:12 +08:00
|
|
|
prefix,
|
2021-02-01 17:06:20 +08:00
|
|
|
bordered = true,
|
2020-10-12 10:59:40 +08:00
|
|
|
readOnly,
|
2022-02-16 11:48:24 +08:00
|
|
|
status: customStatus,
|
2022-02-09 18:06:36 +08:00
|
|
|
controls,
|
2020-10-12 10:59:40 +08:00
|
|
|
...others
|
|
|
|
} = props;
|
2020-01-03 13:38:16 +08:00
|
|
|
|
2020-10-12 10:59:40 +08:00
|
|
|
const prefixCls = getPrefixCls('input-number', customizePrefixCls);
|
2022-03-14 11:40:13 +08:00
|
|
|
|
|
|
|
// Style
|
2022-04-06 21:49:30 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-14 11:40:13 +08:00
|
|
|
|
2022-02-09 18:06:36 +08:00
|
|
|
let upIcon = <UpOutlined className={`${prefixCls}-handler-up-inner`} />;
|
|
|
|
let downIcon = <DownOutlined className={`${prefixCls}-handler-down-inner`} />;
|
|
|
|
const controlsTemp = typeof controls === 'boolean' ? controls : undefined;
|
|
|
|
|
|
|
|
if (typeof controls === 'object') {
|
|
|
|
upIcon =
|
|
|
|
typeof controls.upIcon === 'undefined' ? (
|
|
|
|
upIcon
|
|
|
|
) : (
|
|
|
|
<span className={`${prefixCls}-handler-up-inner`}>{controls.upIcon}</span>
|
|
|
|
);
|
|
|
|
downIcon =
|
|
|
|
typeof controls.downIcon === 'undefined' ? (
|
|
|
|
downIcon
|
|
|
|
) : (
|
|
|
|
<span className={`${prefixCls}-handler-down-inner`}>{controls.downIcon}</span>
|
|
|
|
);
|
|
|
|
}
|
2017-09-15 15:47:06 +08:00
|
|
|
|
2022-03-25 17:48:12 +08:00
|
|
|
const {
|
|
|
|
hasFeedback,
|
|
|
|
status: contextStatus,
|
|
|
|
isFormItemInput,
|
|
|
|
feedbackIcon,
|
|
|
|
} = useContext(FormItemInputContext);
|
2022-02-16 11:48:24 +08:00
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
|
|
|
|
2020-10-12 10:59:40 +08:00
|
|
|
const mergeSize = customizeSize || size;
|
2022-04-29 20:48:10 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled || disabled;
|
|
|
|
|
2020-10-12 10:59:40 +08:00
|
|
|
const inputNumberClass = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergeSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergeSize === 'small',
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-readonly`]: readOnly,
|
2021-02-01 17:06:20 +08:00
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
2022-03-24 21:54:20 +08:00
|
|
|
[`${prefixCls}-in-form-item`]: isFormItemInput,
|
2020-10-12 10:59:40 +08:00
|
|
|
},
|
2022-02-16 11:48:24 +08:00
|
|
|
getStatusClassNames(prefixCls, mergedStatus),
|
2022-03-14 11:40:13 +08:00
|
|
|
hashId,
|
2020-10-12 10:59:40 +08:00
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
2021-10-30 22:25:12 +08:00
|
|
|
let element = (
|
2020-10-12 10:59:40 +08:00
|
|
|
<RcInputNumber
|
2021-10-30 22:25:12 +08:00
|
|
|
ref={inputRef}
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled={mergedDisabled}
|
2020-10-12 10:59:40 +08:00
|
|
|
className={inputNumberClass}
|
|
|
|
upHandler={upIcon}
|
|
|
|
downHandler={downIcon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
readOnly={readOnly}
|
2022-02-09 18:06:36 +08:00
|
|
|
controls={controlsTemp}
|
2020-10-12 10:59:40 +08:00
|
|
|
{...others}
|
|
|
|
/>
|
|
|
|
);
|
2021-08-31 13:09:03 +08:00
|
|
|
|
2022-02-16 11:48:24 +08:00
|
|
|
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,
|
|
|
|
},
|
2022-03-14 11:40:13 +08:00
|
|
|
hashId,
|
2022-02-16 11:48:24 +08:00
|
|
|
);
|
|
|
|
|
2021-10-30 22:25:12 +08:00
|
|
|
element = (
|
|
|
|
<div
|
|
|
|
className={affixWrapperCls}
|
|
|
|
style={props.style}
|
|
|
|
onMouseUp={() => inputRef.current!.focus()}
|
|
|
|
>
|
2022-02-16 11:48:24 +08:00
|
|
|
{prefix && <span className={`${prefixCls}-prefix`}>{prefix}</span>}
|
2021-10-30 22:25:12 +08:00
|
|
|
{cloneElement(element, {
|
|
|
|
style: null,
|
|
|
|
value: props.value,
|
|
|
|
onFocus: (event: React.FocusEvent<HTMLInputElement>) => {
|
|
|
|
setFocus(true);
|
|
|
|
props.onFocus?.(event);
|
|
|
|
},
|
|
|
|
onBlur: (event: React.FocusEvent<HTMLInputElement>) => {
|
|
|
|
setFocus(false);
|
|
|
|
props.onBlur?.(event);
|
|
|
|
},
|
|
|
|
})}
|
2022-03-25 17:48:12 +08:00
|
|
|
{hasFeedback && <span className={`${prefixCls}-suffix`}>{feedbackIcon}</span>}
|
2021-10-30 22:25:12 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-31 13:09:03 +08:00
|
|
|
if (addonBefore != null || addonAfter != null) {
|
|
|
|
const wrapperClassName = `${prefixCls}-group`;
|
|
|
|
const addonClassName = `${wrapperClassName}-addon`;
|
|
|
|
const addonBeforeNode = addonBefore ? (
|
|
|
|
<div className={addonClassName}>{addonBefore}</div>
|
|
|
|
) : null;
|
|
|
|
const addonAfterNode = addonAfter ? <div className={addonClassName}>{addonAfter}</div> : null;
|
|
|
|
|
2022-03-14 11:40:13 +08:00
|
|
|
const mergedWrapperClassName = classNames(`${prefixCls}-wrapper`, wrapperClassName, hashId, {
|
2021-08-31 13:09:03 +08:00
|
|
|
[`${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',
|
|
|
|
},
|
2022-02-16 11:48:24 +08:00
|
|
|
getStatusClassNames(`${prefixCls}-group-wrapper`, mergedStatus, hasFeedback),
|
2022-03-14 11:40:13 +08:00
|
|
|
hashId,
|
2021-08-31 13:09:03 +08:00
|
|
|
className,
|
|
|
|
);
|
2021-10-30 22:25:12 +08:00
|
|
|
element = (
|
2021-08-31 13:09:03 +08:00
|
|
|
<div className={mergedGroupClassName} style={props.style}>
|
|
|
|
<div className={mergedWrapperClassName}>
|
2022-03-10 19:04:51 +08:00
|
|
|
{addonBeforeNode && <NoFormStatus>{addonBeforeNode}</NoFormStatus>}
|
2022-04-29 20:48:10 +08:00
|
|
|
{cloneElement(element, { style: null, disabled: mergedDisabled })}
|
2022-03-10 19:04:51 +08:00
|
|
|
{addonAfterNode && <NoFormStatus>{addonAfterNode}</NoFormStatus>}
|
2021-08-31 13:09:03 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-14 11:40:13 +08:00
|
|
|
return wrapSSR(element);
|
2020-04-22 09:48:10 +08:00
|
|
|
});
|
|
|
|
|
2021-02-22 22:07:19 +08:00
|
|
|
export default InputNumber as (<T extends ValueType = ValueType>(
|
|
|
|
props: React.PropsWithChildren<InputNumberProps<T>> & {
|
|
|
|
ref?: React.Ref<HTMLInputElement>;
|
|
|
|
},
|
|
|
|
) => React.ReactElement) & { displayName?: string };
|