Skip to content

Commit

Permalink
refactor: move clearIcon into allowClear object (#12)
Browse files Browse the repository at this point in the history
* refactor: move clearIcon into allowClear object

* chore: code clean

* test: update test
  • Loading branch information
MadCcc authored Mar 7, 2022
1 parent 96782af commit 279b2e8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
10 changes: 2 additions & 8 deletions docs/examples/allow-clear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ const Demo: FC = () => {

return (
<div>
<Input
prefixCls="rc-input"
allowClear
clearIcon="✖"
placeholder="uncontrolled"
/>
<Input prefixCls="rc-input" allowClear placeholder="uncontrolled" />
<br />
<br />
<Input
prefixCls="rc-input"
allowClear
clearIcon="✖"
allowClear={{ clearIcon: '✖' }}
onChange={handleChange}
value={value}
placeholder="controlled"
Expand Down
8 changes: 6 additions & 2 deletions src/BaseInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const BaseInput: FC<BaseInputProps> = (props) => {
focused,
triggerFocus,
allowClear,
clearIcon,
value,
handleReset,
hidden,
Expand All @@ -43,6 +42,11 @@ const BaseInput: FC<BaseInputProps> = (props) => {
}
const needClear = !disabled && !readOnly && value;
const clearIconCls = `${prefixCls}-clear-icon`;
const iconNode =
typeof allowClear === 'object' && allowClear?.clearIcon
? allowClear.clearIcon
: '✖';

return (
<span
onClick={handleReset}
Expand All @@ -56,7 +60,7 @@ const BaseInput: FC<BaseInputProps> = (props) => {
role="button"
tabIndex={-1}
>
{clearIcon}
{iconNode}
</span>
);
};
Expand Down
1 change: 0 additions & 1 deletion src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
'inputClassName',
'wrapperClassName',
'htmlSize',
'clearIcon',
]);
return (
<input
Expand Down
3 changes: 1 addition & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export interface CommonInputProps {
groupClassName?: string;
wrapperClassName?: string;
inputClassName?: string;
allowClear?: boolean;
clearIcon?: ReactNode;
allowClear?: boolean | { clearIcon?: ReactNode };
}

export interface BaseInputProps extends CommonInputProps {
Expand Down
19 changes: 17 additions & 2 deletions tests/BaseInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ describe('BaseInput', () => {
return (
<BaseInput
prefixCls="rc-input"
allowClear
clearIcon="✖"
allowClear={{ clearIcon: '✖' }}
inputElement={
<input onChange={handleChange} onBlur={onBlur} onFocus={onFocus} />
}
Expand Down Expand Up @@ -100,4 +99,20 @@ describe('BaseInput', () => {
expect(onChange).toHaveBeenCalledTimes(1);
expect(inputEl!.value).toBe('');
});

it('should display clearIcon correctly', () => {
const { container, rerender } = render(
<BaseInput prefixCls="rc-input" inputElement={<input />} allowClear />,
);
const clearIcon = container.querySelector('.rc-input-clear-icon');
expect(clearIcon?.innerHTML).toBe('✖');
rerender(
<BaseInput
prefixCls="rc-input"
inputElement={<input />}
allowClear={{ clearIcon: 'clear' }}
/>,
);
expect(clearIcon?.innerHTML).toBe('clear');
});
});
24 changes: 18 additions & 6 deletions tests/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ exports[`Input allowClear should change type when click 1`] = `
class="rc-input-clear-icon"
role="button"
tabindex="-1"
/>
>
</span>
</span>
</span>
</div>
Expand All @@ -40,7 +42,9 @@ exports[`Input allowClear should change type when click 2`] = `
class="rc-input-clear-icon rc-input-clear-icon-hidden"
role="button"
tabindex="-1"
/>
>
</span>
</span>
</span>
</div>
Expand All @@ -63,7 +67,9 @@ exports[`Input allowClear should not show icon if defaultValue is undefined or e
class="rc-input-clear-icon rc-input-clear-icon-hidden"
role="button"
tabindex="-1"
/>
>
</span>
</span>
</span>
</div>
Expand All @@ -86,7 +92,9 @@ exports[`Input allowClear should not show icon if defaultValue is undefined or e
class="rc-input-clear-icon rc-input-clear-icon-hidden"
role="button"
tabindex="-1"
/>
>
</span>
</span>
</span>
</div>
Expand All @@ -109,7 +117,9 @@ exports[`Input allowClear should not show icon if value is undefined or empty st
class="rc-input-clear-icon rc-input-clear-icon-hidden"
role="button"
tabindex="-1"
/>
>
</span>
</span>
</span>
</div>
Expand All @@ -132,7 +142,9 @@ exports[`Input allowClear should not show icon if value is undefined or empty st
class="rc-input-clear-icon rc-input-clear-icon-hidden"
role="button"
tabindex="-1"
/>
>
</span>
</span>
</span>
</div>
Expand Down

0 comments on commit 279b2e8

Please sign in to comment.