diff --git a/package.json b/package.json index dbd6d89f..3f7e7a5b 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@babel/runtime": "^7.10.1", "@rc-component/mini-decimal": "^1.0.1", "classnames": "^2.2.5", + "rc-input": "~1.1.0", "rc-util": "^5.28.0" }, "devDependencies": { diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index f182de76..da68ef05 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -6,7 +6,8 @@ import getMiniDecimal, { validateNumber, ValueType, } from '@rc-component/mini-decimal'; -import classNames from 'classnames'; +import clsx from 'classnames'; +import { BaseInput } from 'rc-input'; import { useLayoutUpdateEffect } from 'rc-util/lib/hooks/useLayoutEffect'; import { composeRef } from 'rc-util/lib/ref'; import * as React from 'react'; @@ -14,6 +15,7 @@ import useCursor from './hooks/useCursor'; import StepHandler from './StepHandler'; import { getDecupleSteps } from './utils/numberUtil'; +import { InputFocusOptions, triggerFocus } from 'rc-input/lib/utils/commonUtils'; import useFrame from './hooks/useFrame'; /** @@ -43,7 +45,7 @@ const getDecimalIfValidate = (value: ValueType) => { export interface InputNumberProps extends Omit< React.InputHTMLAttributes, - 'value' | 'defaultValue' | 'onInput' | 'onChange' + 'value' | 'defaultValue' | 'onInput' | 'onChange' | 'prefix' | 'suffix' > { /** value will show as string */ stringMode?: boolean; @@ -59,6 +61,21 @@ export interface InputNumberProps step?: ValueType; tabIndex?: number; controls?: boolean; + prefix?: React.ReactNode; + suffix?: React.ReactNode; + addonBefore?: React.ReactNode; + addonAfter?: React.ReactNode; + classes?: { + affixWrapper?: string; + group?: string; + wrapper?: string; + }; + classNames?: { + affixWrapper?: string; + group?: string; + wrapper?: string; + input?: string; + }; // Customize handler node upHandler?: React.ReactNode; @@ -86,7 +103,7 @@ export interface InputNumberProps // size?: ISize; } -const InputNumber = React.forwardRef( +const InternalInputNumber = React.forwardRef( (props: InputNumberProps, ref: React.Ref) => { const { prefixCls = 'rc-input-number', @@ -104,6 +121,7 @@ const InputNumber = React.forwardRef( keyboard, controls = true, + classNames, stringMode, parser, @@ -527,13 +545,18 @@ const InputNumber = React.forwardRef( // ============================ Render ============================ return (
{ setFocus(true); @@ -575,6 +598,63 @@ const InputNumber = React.forwardRef(
); }, +); + +const InputNumber = React.forwardRef( + (props: InputNumberProps, ref: React.Ref) => { + const { + disabled, + style, + prefixCls, + value, + prefix, + addonBefore, + addonAfter, + classes, + className, + classNames, + ...rest + } = props; + + const inputFocusRef = React.useRef(null); + + const focus = (option?: InputFocusOptions) => { + if (inputFocusRef.current) { + triggerFocus(inputFocusRef.current, option); + } + }; + + return ( + + } + className={className} + triggerFocus={focus} + prefixCls={prefixCls} + value={value} + disabled={disabled} + style={style} + prefix={prefix} + addonAfter={addonAfter} + addonBefore={addonBefore} + classes={classes} + classNames={classNames} + components={{ + affixWrapper: 'div', + groupWrapper: 'div', + wrapper: 'div', + groupAddon: 'div', + }} + /> + ); + }, ) as (( props: React.PropsWithChildren> & { ref?: React.Ref; diff --git a/tests/__snapshots__/baseInput.test.tsx.snap b/tests/__snapshots__/baseInput.test.tsx.snap new file mode 100644 index 00000000..a8e10153 --- /dev/null +++ b/tests/__snapshots__/baseInput.test.tsx.snap @@ -0,0 +1,185 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`baseInput addon should render properly 1`] = ` +
+
+
+
+
+ + Addon Before + +
+
+
+ + + + + + +
+
+ +
+
+
+
+
+
+
+
+
+
+ + + + + + +
+
+ +
+
+
+ + Addon After + +
+
+
+
+
+`; + +exports[`baseInput prefix should render properly 1`] = ` +
+
+ + + Prefix + + +
+
+ + + + + + +
+
+ +
+
+
+
+`; diff --git a/tests/baseInput.test.tsx b/tests/baseInput.test.tsx new file mode 100644 index 00000000..0890d7be --- /dev/null +++ b/tests/baseInput.test.tsx @@ -0,0 +1,26 @@ +import { render } from '@testing-library/react'; +import InputNumber from '../src'; + +describe('baseInput', () => { + it('prefix should render properly', () => { + const prefix = Prefix; + + const { container } = render(); + expect(container).toMatchSnapshot(); + }); + + it('addon should render properly', () => { + const addonBefore = Addon Before; + const addonAfter = Addon After; + + const { container } = render( +
+ +
+
+ +
, + ); + expect(container).toMatchSnapshot(); + }); +});