Skip to content

Commit ebbe8d4

Browse files
authored
feat: classNames support variant (#57)
* feat: classNames support variant * chore: code clean
1 parent 374d975 commit ebbe8d4

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/BaseInput.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ const BaseInput: FC<BaseInputProps> = (props) => {
4545
}
4646
};
4747

48+
const hasAffix = hasPrefixSuffix(props);
49+
4850
let element: ReactElement = cloneElement(inputElement, {
4951
value,
52+
className:
53+
clsx(inputElement.props.className, !hasAffix && classNames?.variant) ||
54+
null,
5055
});
5156

5257
// ================== Prefix & Suffix ================== //
53-
if (hasPrefixSuffix(props)) {
58+
if (hasAffix) {
5459
// ================== Clear Icon ================== //
5560
let clearIcon: ReactNode = null;
5661
if (allowClear) {
@@ -92,6 +97,7 @@ const BaseInput: FC<BaseInputProps> = (props) => {
9297
},
9398
classes?.affixWrapper,
9499
classNames?.affixWrapper,
100+
classNames?.variant,
95101
);
96102

97103
const suffixNode = (suffix || allowClear) && (

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface CommonInputProps {
2626
suffix?: string;
2727
groupWrapper?: string;
2828
wrapper?: string;
29+
variant?: string;
2930
};
3031
styles?: {
3132
affixWrapper?: CSSProperties;

tests/BaseInput.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,35 @@ describe('BaseInput', () => {
236236

237237
expect(container.firstChild).toHaveClass('rc-input-group-wrapper-disabled');
238238
});
239+
240+
it('variant cls', () => {
241+
const { container, rerender } = render(
242+
<BaseInput
243+
prefixCls="rc-input"
244+
prefix="$"
245+
classNames={{ variant: 'test-variant' }}
246+
disabled
247+
>
248+
<input />
249+
</BaseInput>,
250+
);
251+
252+
expect(container.querySelector('.rc-input-affix-wrapper')).toHaveClass(
253+
'test-variant',
254+
);
255+
expect(container.querySelector('input')).not.toHaveClass('test-variant');
256+
257+
rerender(
258+
<BaseInput
259+
prefixCls="rc-input"
260+
classNames={{ variant: 'test-variant' }}
261+
disabled
262+
>
263+
<input />
264+
</BaseInput>,
265+
);
266+
267+
expect(container.querySelector('.rc-input-affix-wrapper')).toBeFalsy();
268+
expect(container.querySelector('input')).toHaveClass('test-variant');
269+
});
239270
});

0 commit comments

Comments
 (0)