From 59517febc19cc584451fc0f1d998ecdc48c14fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E8=89=B3=E5=85=B5?= Date: Wed, 2 Sep 2026 14:25:43 +0800 Subject: [PATCH] refactor: use renderable guards --- package.json | 2 +- src/BaseInput.tsx | 16 +++++++++------- src/utils/commonUtils.ts | 11 +++++++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 56087b2..622efc7 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ }, "dependencies": { "@rc-component/resize-observer": "^1.1.1", - "@rc-component/util": "^1.11.1", + "@rc-component/util": "^1.13.0", "clsx": "^2.1.1" }, "devDependencies": { diff --git a/src/BaseInput.tsx b/src/BaseInput.tsx index 02a4398..29d309b 100644 --- a/src/BaseInput.tsx +++ b/src/BaseInput.tsx @@ -1,4 +1,5 @@ import { clsx } from 'clsx'; +import { isReactRenderable } from '@rc-component/util'; import type { ReactElement, ReactNode } from 'react'; import React, { cloneElement, useRef } from 'react'; import type { BaseInputProps } from './interface'; @@ -81,7 +82,8 @@ const BaseInput = React.forwardRef((props, ref) => { !(typeof allowClear === 'object' && allowClear.disabled); const clearIconCls = `${prefixCls}-clear-icon`; const iconNode = - typeof allowClear === 'object' && allowClear?.clearIcon + typeof allowClear === 'object' && + isReactRenderable(allowClear?.clearIcon) ? allowClear.clearIcon : '✖'; @@ -99,7 +101,7 @@ const BaseInput = React.forwardRef((props, ref) => { clearIconCls, { [`${clearIconCls}-hidden`]: !needClear, - [`${clearIconCls}-has-suffix`]: !!suffix, + [`${clearIconCls}-has-suffix`]: isReactRenderable(suffix), }, classNames?.clear, )} @@ -119,14 +121,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, + isReactRenderable(suffix) && allowClear && value, }, classes?.affixWrapper, classNames?.affixWrapper, classNames?.variant, ); - const suffixNode = (suffix || allowClear) && ( + const suffixNode = (isReactRenderable(suffix) || allowClear) && ( ((props, ref) => { {...dataAttrs?.affixWrapper} ref={containerRef} > - {prefix && ( + {isReactRenderable(prefix) && ( ((props, ref) => { element = ( - {addonBefore && ( + {isReactRenderable(addonBefore) && ( {addonBefore} )} {element} - {addonAfter && ( + {isReactRenderable(addonAfter) && ( {addonAfter} diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 0ab162c..ec7d49e 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -1,12 +1,19 @@ import type React from 'react'; +import { isReactRenderable } from '@rc-component/util'; import type { BaseInputProps, InputProps } from '../interface'; export function hasAddon(props: BaseInputProps | InputProps) { - return !!(props.addonBefore || props.addonAfter); + return ( + isReactRenderable(props.addonBefore) || isReactRenderable(props.addonAfter) + ); } export function hasPrefixSuffix(props: BaseInputProps | InputProps) { - return !!(props.prefix || props.suffix || props.allowClear); + return ( + isReactRenderable(props.prefix) || + isReactRenderable(props.suffix) || + Boolean(props.allowClear) + ); } // TODO: It's better to use `Proxy` replace the `element.value`. But we still need support IE11.