Skip to content

Commit

Permalink
feat: classNames support variant
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc committed Dec 15, 2023
1 parent 374d975 commit dae6063
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/BaseInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ const BaseInput: FC<BaseInputProps> = (props) => {
}
};

const hasAffix = hasPrefixSuffix(props);

let element: ReactElement = cloneElement(inputElement, {
value,
className:
clsx(inputElement.props.className, !hasAffix && classNames?.variant) ||
null,
});

// ================== Prefix & Suffix ================== //
Expand Down Expand Up @@ -92,6 +97,7 @@ const BaseInput: FC<BaseInputProps> = (props) => {
},
classes?.affixWrapper,
classNames?.affixWrapper,
classNames?.variant,
);

const suffixNode = (suffix || allowClear) && (
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface CommonInputProps {
suffix?: string;
groupWrapper?: string;
wrapper?: string;
variant?: string;
};
styles?: {
affixWrapper?: CSSProperties;
Expand Down
31 changes: 31 additions & 0 deletions tests/BaseInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,35 @@ describe('BaseInput', () => {

expect(container.firstChild).toHaveClass('rc-input-group-wrapper-disabled');
});

it('variant cls', () => {
const { container, rerender } = render(
<BaseInput
prefixCls="rc-input"
prefix="$"
classNames={{ variant: 'test-variant' }}
disabled
>
<input />
</BaseInput>,
);

expect(container.querySelector('.rc-input-affix-wrapper')).toHaveClass(
'test-variant',
);
expect(container.querySelector('input')).not.toHaveClass('test-variant');

rerender(
<BaseInput
prefixCls="rc-input"
classNames={{ variant: 'test-variant' }}
disabled
>
<input />
</BaseInput>,
);

expect(container.querySelector('.rc-input-affix-wrapper')).toBeFalsy();
expect(container.querySelector('input')).toHaveClass('test-variant');
});
});

0 comments on commit dae6063

Please sign in to comment.