Skip to content

Commit 507a041

Browse files
authored
refactor: use renderable guards (#203)
1 parent 52086bb commit 507a041

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"dependencies": {
4747
"@rc-component/resize-observer": "^1.1.1",
48-
"@rc-component/util": "^1.11.1",
48+
"@rc-component/util": "^1.13.0",
4949
"clsx": "^2.1.1"
5050
},
5151
"devDependencies": {

src/BaseInput.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { clsx } from 'clsx';
2+
import { isReactRenderable } from '@rc-component/util';
23
import type { ReactElement, ReactNode } from 'react';
34
import React, { cloneElement, useRef } from 'react';
45
import type { BaseInputProps } from './interface';
@@ -81,7 +82,8 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
8182
!(typeof allowClear === 'object' && allowClear.disabled);
8283
const clearIconCls = `${prefixCls}-clear-icon`;
8384
const iconNode =
84-
typeof allowClear === 'object' && allowClear?.clearIcon
85+
typeof allowClear === 'object' &&
86+
isReactRenderable(allowClear?.clearIcon)
8587
? allowClear.clearIcon
8688
: '✖';
8789

@@ -99,7 +101,7 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
99101
clearIconCls,
100102
{
101103
[`${clearIconCls}-hidden`]: !needClear,
102-
[`${clearIconCls}-has-suffix`]: !!suffix,
104+
[`${clearIconCls}-has-suffix`]: isReactRenderable(suffix),
103105
},
104106
classNames?.clear,
105107
)}
@@ -119,14 +121,14 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
119121
[`${affixWrapperPrefixCls}-focused`]: focused, // Not used, but keep it
120122
[`${affixWrapperPrefixCls}-readonly`]: readOnly,
121123
[`${affixWrapperPrefixCls}-input-with-clear-btn`]:
122-
suffix && allowClear && value,
124+
isReactRenderable(suffix) && allowClear && value,
123125
},
124126
classes?.affixWrapper,
125127
classNames?.affixWrapper,
126128
classNames?.variant,
127129
);
128130

129-
const suffixNode = (suffix || allowClear) && (
131+
const suffixNode = (isReactRenderable(suffix) || allowClear) && (
130132
<span
131133
className={clsx(`${prefixCls}-suffix`, classNames?.suffix)}
132134
style={styles?.suffix}
@@ -144,7 +146,7 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
144146
{...dataAttrs?.affixWrapper}
145147
ref={containerRef}
146148
>
147-
{prefix && (
149+
{isReactRenderable(prefix) && (
148150
<span
149151
className={clsx(`${prefixCls}-prefix`, classNames?.prefix)}
150152
style={styles?.prefix}
@@ -185,13 +187,13 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
185187
element = (
186188
<GroupWrapperComponent className={mergedGroupClassName} ref={groupRef}>
187189
<WrapperComponent className={mergedWrapperClassName}>
188-
{addonBefore && (
190+
{isReactRenderable(addonBefore) && (
189191
<GroupAddonComponent className={addonCls}>
190192
{addonBefore}
191193
</GroupAddonComponent>
192194
)}
193195
{element}
194-
{addonAfter && (
196+
{isReactRenderable(addonAfter) && (
195197
<GroupAddonComponent className={addonCls}>
196198
{addonAfter}
197199
</GroupAddonComponent>

src/utils/commonUtils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import type React from 'react';
2+
import { isReactRenderable } from '@rc-component/util';
23
import type { BaseInputProps, InputProps } from '../interface';
34

45
export function hasAddon(props: BaseInputProps | InputProps) {
5-
return !!(props.addonBefore || props.addonAfter);
6+
return (
7+
isReactRenderable(props.addonBefore) || isReactRenderable(props.addonAfter)
8+
);
69
}
710

811
export function hasPrefixSuffix(props: BaseInputProps | InputProps) {
9-
return !!(props.prefix || props.suffix || props.allowClear);
12+
return (
13+
isReactRenderable(props.prefix) ||
14+
isReactRenderable(props.suffix) ||
15+
Boolean(props.allowClear)
16+
);
1017
}
1118

1219
// TODO: It's better to use `Proxy` replace the `element.value`. But we still need support IE11.

0 commit comments

Comments
 (0)