Skip to content

feat: input.password add iconRender #3320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions components/input/Password.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classNames from '../_util/classNames';
import { getComponent, getOptionProps } from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
import Input from './Input';
import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
import EyeInvisibleOutlined from '@ant-design/icons-vue/EyeInvisibleOutlined';
Expand All @@ -23,6 +24,9 @@ export default defineComponent({
inputPrefixCls: PropTypes.string.def('ant-input'),
action: PropTypes.string.def('click'),
visibilityToggle: PropTypes.looseBool.def(true),
iconRender: PropTypes.func.def((visible: boolean) =>
visible ? <EyeOutlined /> : <EyeInvisibleOutlined />,
),
},
setup() {
return {
Expand Down Expand Up @@ -53,8 +57,9 @@ export default defineComponent({
});
},
getIcon() {
const { prefixCls, action } = this.$props;
const { prefixCls, action, iconRender = () => null } = this.$props;
const iconTrigger = ActionMap[action] || '';
const icon = iconRender(this.visible);
const iconProps = {
[iconTrigger]: this.onVisibleChange,
onMousedown: (e: Event) => {
Expand All @@ -70,11 +75,7 @@ export default defineComponent({
class: `${prefixCls}-icon`,
key: 'passwordIcon',
};
return this.visible ? (
<EyeOutlined {...iconProps} />
) : (
<EyeInvisibleOutlined {...iconProps} />
);
return cloneElement(icon, iconProps);
},
},
render() {
Expand Down
17 changes: 17 additions & 0 deletions components/input/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { asyncExpect } from '@/tests/utils';
import Input from '..';
// import Form from '../../form';
import focusTest from '../../../tests/shared/focusTest';
import { WifiOutlined, SyncOutlined } from '@ant-design/icons-vue';

const { TextArea, Password } = Input;

Expand Down Expand Up @@ -131,3 +132,19 @@ describe('Input.Search', () => {
}, 100);
});
});

describe('Input.Password', () => {
it('should support iconRender', async () => {
const wrapper = mount(Input.Password, {
props: { iconRender: visible => (visible ? <SyncOutlined /> : <WifiOutlined />) },
sync: false,
});
await asyncExpect(() => {
expect(wrapper.findAll('.anticon-wifi').length).toBe(1);
wrapper.find('.anticon-wifi').trigger('click');
}, 100);
await asyncExpect(() => {
expect(wrapper.findAll('.anticon-sync').length).toBe(1);
}, 100);
});
});