2023-12-27 11:53:19 +08:00
|
|
|
import * as React from 'react';
|
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';
|
2023-07-19 11:01:14 +08:00
|
|
|
import type { InputNumberProps as RcInputNumberProps, ValueType } from 'rc-input-number';
|
2022-04-06 21:49:30 +08:00
|
|
|
import RcInputNumber from 'rc-input-number';
|
2023-12-27 11:53:19 +08:00
|
|
|
|
2024-06-21 02:10:21 +08:00
|
|
|
import ContextIsolator from '../_util/ContextIsolator';
|
2023-05-12 14:53:47 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2023-12-27 14:42:51 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-09-22 11:54:05 +08:00
|
|
|
import ConfigProvider, { ConfigContext } from '../config-provider';
|
2022-06-06 23:39:00 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2023-12-27 11:53:19 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2023-12-27 11:53:19 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
2024-06-16 15:44:18 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
2024-06-21 17:47:09 +08:00
|
|
|
import type { Variant } from '../config-provider';
|
2023-12-22 10:05:15 +08:00
|
|
|
import useVariant from '../form/hooks/useVariants';
|
2024-06-16 15:44:18 +08:00
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
2022-06-21 19:40:22 +08:00
|
|
|
import useStyle from './style';
|
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;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: 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;
|
2024-07-10 14:43:39 +08:00
|
|
|
suffix?: React.ReactNode;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled?: boolean;
|
2023-12-11 15:34:07 +08:00
|
|
|
/** @deprecated Use `variant` instead. */
|
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 };
|
2023-12-18 17:53:31 +08:00
|
|
|
/**
|
|
|
|
* @since 5.13.0
|
|
|
|
* @default "outlined"
|
|
|
|
*/
|
2023-12-22 10:05:15 +08:00
|
|
|
variant?: Variant;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-22 22:07:19 +08:00
|
|
|
const InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {
|
2023-12-11 15:34:07 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2024-06-28 23:19:58 +08:00
|
|
|
const typeWarning = devUseWarning('InputNumber');
|
|
|
|
typeWarning.deprecated(!('bordered' in props), 'bordered', 'variant');
|
|
|
|
typeWarning(
|
|
|
|
!(props.type === 'number' && props.changeOnWheel),
|
|
|
|
'usage',
|
|
|
|
'When `type=number` is used together with `changeOnWheel`, changeOnWheel may not work properly. Please delete `type=number` if it is not necessary.',
|
|
|
|
);
|
2023-12-11 15:34:07 +08:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:49:30 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2023-05-12 14:53:47 +08:00
|
|
|
|
2021-10-30 22:25:12 +08:00
|
|
|
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,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-10-12 10:59:40 +08:00
|
|
|
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,
|
2024-07-10 14:43:39 +08:00
|
|
|
suffix,
|
2023-12-11 15:34:07 +08:00
|
|
|
bordered,
|
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,
|
2023-12-11 15:34:07 +08:00
|
|
|
variant: customVariant,
|
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
|
2023-12-14 14:58:53 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2022-03-14 11:40:13 +08:00
|
|
|
|
2022-10-18 16:23:10 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
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,
|
2022-12-06 21:09:59 +08:00
|
|
|
} = React.useContext(FormItemInputContext);
|
2022-02-16 11:48:24 +08:00
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
|
|
|
|
2023-06-19 14:26:48 +08:00
|
|
|
const mergedSize = useSize((ctx) => customizeSize ?? compactSize ?? ctx);
|
2023-01-20 11:03:50 +08:00
|
|
|
|
2022-04-29 20:48:10 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
2022-09-20 16:48:59 +08:00
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
2024-06-21 17:47:09 +08:00
|
|
|
const [variant, enableVariantCls] = useVariant('inputNumber', customVariant, bordered);
|
2023-12-11 15:34:07 +08:00
|
|
|
|
2024-09-19 03:30:19 +08:00
|
|
|
/* biome-ignore lint/complexity/noUselessFragments: avoid falsy value */
|
2023-12-18 17:53:31 +08:00
|
|
|
const suffixNode = hasFeedback && <>{feedbackIcon}</>;
|
|
|
|
|
2020-10-12 10:59:40 +08:00
|
|
|
const inputNumberClass = classNames(
|
|
|
|
{
|
2023-05-12 14:53:47 +08:00
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
2020-10-12 10:59:40 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2022-03-24 21:54:20 +08:00
|
|
|
[`${prefixCls}-in-form-item`]: isFormItemInput,
|
2020-10-12 10:59:40 +08:00
|
|
|
},
|
2022-03-14 11:40:13 +08:00
|
|
|
hashId,
|
2020-10-12 10:59:40 +08:00
|
|
|
);
|
2023-06-14 11:28:40 +08:00
|
|
|
const wrapperClassName = `${prefixCls}-group`;
|
2020-10-12 10:59:40 +08:00
|
|
|
|
2023-06-14 11:28:40 +08:00
|
|
|
const 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}
|
2023-12-14 14:58:53 +08:00
|
|
|
className={classNames(cssVarCls, rootCls, className, rootClassName, compactItemClassnames)}
|
2020-10-12 10:59:40 +08:00
|
|
|
upHandler={upIcon}
|
|
|
|
downHandler={downIcon}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
readOnly={readOnly}
|
2022-02-09 18:06:36 +08:00
|
|
|
controls={controlsTemp}
|
2023-06-14 11:28:40 +08:00
|
|
|
prefix={prefix}
|
2024-07-10 14:43:39 +08:00
|
|
|
suffix={suffixNode || suffix}
|
2024-06-21 02:10:21 +08:00
|
|
|
addonBefore={
|
|
|
|
addonBefore && (
|
2024-06-22 21:59:12 +08:00
|
|
|
<ContextIsolator form space>
|
2024-06-21 02:10:21 +08:00
|
|
|
{addonBefore}
|
|
|
|
</ContextIsolator>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
addonAfter={
|
|
|
|
addonAfter && (
|
2024-06-22 21:59:12 +08:00
|
|
|
<ContextIsolator form space>
|
2024-06-21 02:10:21 +08:00
|
|
|
{addonAfter}
|
|
|
|
</ContextIsolator>
|
|
|
|
)
|
|
|
|
}
|
2023-06-14 11:28:40 +08:00
|
|
|
classNames={{
|
2024-01-08 10:37:10 +08:00
|
|
|
input: inputNumberClass,
|
2023-12-18 17:53:31 +08:00
|
|
|
variant: classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${variant}`]: enableVariantCls,
|
|
|
|
},
|
|
|
|
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
|
|
|
|
),
|
2023-06-14 11:28:40 +08:00
|
|
|
affixWrapper: classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-affix-wrapper-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-affix-wrapper-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
|
2024-08-16 20:07:26 +08:00
|
|
|
[`${prefixCls}-affix-wrapper-without-controls`]: controls === false,
|
2023-06-14 11:28:40 +08:00
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
),
|
|
|
|
wrapper: classNames(
|
|
|
|
{
|
|
|
|
[`${wrapperClassName}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
),
|
2023-12-18 17:53:31 +08:00
|
|
|
groupWrapper: classNames(
|
2023-06-14 11:28:40 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-group-wrapper-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-group-wrapper-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-group-wrapper-rtl`]: direction === 'rtl',
|
2023-12-18 17:53:31 +08:00
|
|
|
[`${prefixCls}-group-wrapper-${variant}`]: enableVariantCls,
|
2023-06-14 11:28:40 +08:00
|
|
|
},
|
|
|
|
getStatusClassNames(`${prefixCls}-group-wrapper`, mergedStatus, hasFeedback),
|
|
|
|
hashId,
|
|
|
|
),
|
|
|
|
}}
|
2020-10-12 10:59:40 +08:00
|
|
|
{...others}
|
|
|
|
/>
|
|
|
|
);
|
2021-08-31 13:09:03 +08:00
|
|
|
|
2023-11-13 17:45:45 +08:00
|
|
|
return wrapCSSVar(element);
|
2020-04-22 09:48:10 +08:00
|
|
|
});
|
|
|
|
|
2022-09-22 11:54:05 +08:00
|
|
|
const TypedInputNumber = InputNumber as unknown as (<T extends ValueType = ValueType>(
|
2023-12-27 11:53:19 +08:00
|
|
|
props: React.PropsWithChildren<InputNumberProps<T>> & React.RefAttributes<HTMLInputElement>,
|
2022-09-22 11:54:05 +08:00
|
|
|
) => React.ReactElement) & {
|
|
|
|
displayName?: string;
|
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PureInputNumber;
|
|
|
|
};
|
|
|
|
|
2023-07-22 12:18:17 +08:00
|
|
|
/** @private Internal Component. Do not use in your production. */
|
|
|
|
const PureInputNumber: React.FC<InputNumberProps> = (props) => (
|
|
|
|
<ConfigProvider theme={{ components: { InputNumber: { handleVisible: true } } }}>
|
2022-09-22 11:54:05 +08:00
|
|
|
<InputNumber {...props} />
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
TypedInputNumber.displayName = 'InputNumber';
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:54:05 +08:00
|
|
|
TypedInputNumber._InternalPanelDoNotUseOrYouWillBeFired = PureInputNumber;
|
|
|
|
|
|
|
|
export default TypedInputNumber;
|