diff --git a/src/BaseInput.tsx b/src/BaseInput.tsx index 02a4398..a06a578 100644 --- a/src/BaseInput.tsx +++ b/src/BaseInput.tsx @@ -2,7 +2,7 @@ import { clsx } from 'clsx'; import type { ReactElement, ReactNode } from 'react'; import React, { cloneElement, useRef } from 'react'; import type { BaseInputProps } from './interface'; -import { hasAddon, hasPrefixSuffix } from './utils/commonUtils'; +import { hasAddon, hasContent, hasPrefixSuffix } from './utils/commonUtils'; export interface HolderRef { /** Provider holder ref. Will return `null` if not wrap anything */ @@ -99,7 +99,7 @@ const BaseInput = React.forwardRef((props, ref) => { clearIconCls, { [`${clearIconCls}-hidden`]: !needClear, - [`${clearIconCls}-has-suffix`]: !!suffix, + [`${clearIconCls}-has-suffix`]: hasContent(suffix), }, classNames?.clear, )} @@ -119,14 +119,14 @@ const BaseInput = React.forwardRef((props, ref) => { [`${affixWrapperPrefixCls}-focused`]: focused, // Not used, but keep it [`${affixWrapperPrefixCls}-readonly`]: readOnly, [`${affixWrapperPrefixCls}-input-with-clear-btn`]: - suffix && allowClear && value, + hasContent(suffix) && allowClear && value, }, classes?.affixWrapper, classNames?.affixWrapper, classNames?.variant, ); - const suffixNode = (suffix || allowClear) && ( + const suffixNode = (hasContent(suffix) || allowClear) && ( ((props, ref) => { {...dataAttrs?.affixWrapper} ref={containerRef} > - {prefix && ( + {hasContent(prefix) && ( ((props, ref) => { element = ( - {addonBefore && ( + {hasContent(addonBefore) && ( {addonBefore} )} {element} - {addonAfter && ( + {hasContent(addonAfter) && ( {addonAfter} diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 0ab162c..e472fb5 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -1,12 +1,18 @@ import type React from 'react'; import type { BaseInputProps, InputProps } from '../interface'; +export function hasContent(value: React.ReactNode) { + return !!value || value === 0; +} + export function hasAddon(props: BaseInputProps | InputProps) { - return !!(props.addonBefore || props.addonAfter); + return hasContent(props.addonBefore) || hasContent(props.addonAfter); } export function hasPrefixSuffix(props: BaseInputProps | InputProps) { - return !!(props.prefix || props.suffix || props.allowClear); + return ( + hasContent(props.prefix) || hasContent(props.suffix) || !!props.allowClear + ); } // TODO: It's better to use `Proxy` replace the `element.value`. But we still need support IE11. diff --git a/tests/BaseInput.test.tsx b/tests/BaseInput.test.tsx index 5fdb7f5..f4a0b8e 100644 --- a/tests/BaseInput.test.tsx +++ b/tests/BaseInput.test.tsx @@ -46,6 +46,45 @@ describe('BaseInput', () => { expect(container).toMatchSnapshot(); }); + it('should render numeric zero affixes and addons', () => { + const { container, rerender } = render( + + + , + ); + + expect(container.querySelector('.rc-input-prefix')).toHaveTextContent('0'); + expect(container.querySelector('.rc-input-suffix')).toHaveTextContent('0'); + expect(container.querySelectorAll('.rc-input-group-addon')).toHaveLength(2); + expect( + container.querySelector('.rc-input-group-wrapper'), + ).toHaveTextContent('0000'); + + rerender( + + + , + ); + + expect( + container.querySelector('.rc-input-affix-wrapper'), + ).not.toBeInTheDocument(); + expect( + container.querySelector('.rc-input-group-wrapper'), + ).not.toBeInTheDocument(); + }); + describe('allowClear should work', () => { const onChange = jest.fn(); const onBlur = jest.fn();