Skip to content

Commit 8e6fab3

Browse files
committed
feat(input): modify visibilityToggle feature
1 parent cacbde3 commit 8e6fab3

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

components/input/Password.tsx

+36-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@ import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
66
import EyeInvisibleOutlined from '@ant-design/icons-vue/EyeInvisibleOutlined';
77
import type { InputProps } from './inputProps';
88
import inputProps from './inputProps';
9-
import { computed, defineComponent, shallowRef } from 'vue';
9+
import type { PropType, Ref, ShallowRef } from 'vue';
10+
import { computed, defineComponent, shallowRef, watch } from 'vue';
1011
import useConfigInject from '../config-provider/hooks/useConfigInject';
1112
import omit from '../_util/omit';
1213

14+
type VisibilityToggleProps =
15+
| boolean
16+
| {
17+
visible: Ref<boolean>;
18+
onVisibleChange: (visible: boolean) => void;
19+
};
20+
1321
const ActionMap = {
1422
click: 'onClick',
1523
hover: 'onMouseover',
1624
};
25+
1726
const defaultIconRender = (visible: boolean) =>
1827
visible ? <EyeOutlined /> : <EyeInvisibleOutlined />;
28+
1929
export default defineComponent({
2030
compatConfig: { MODE: 3 },
2131
name: 'AInputPassword',
@@ -25,18 +35,38 @@ export default defineComponent({
2535
prefixCls: String,
2636
inputPrefixCls: String,
2737
action: { type: String, default: 'click' },
28-
visibilityToggle: { type: Boolean, default: true },
38+
visibilityToggle: {
39+
type: [Boolean, Object] as PropType<VisibilityToggleProps>,
40+
default: true,
41+
},
2942
iconRender: Function,
3043
},
3144
setup(props, { slots, attrs, expose }) {
32-
const visible = shallowRef(false);
45+
let visible: ShallowRef<boolean> = undefined;
46+
47+
if (typeof props.visibilityToggle === 'boolean') {
48+
visible = shallowRef(false);
49+
} else {
50+
visible = shallowRef(props.visibilityToggle.visible);
51+
// eslint-disable-next-line vue/no-setup-props-destructure
52+
const { visibilityToggle } = props;
53+
function _onVisibleChange(visible: boolean) {
54+
visibilityToggle.onVisibleChange(visible);
55+
}
56+
57+
watch(visible, () => {
58+
_onVisibleChange(visible.value);
59+
});
60+
}
61+
3362
const onVisibleChange = () => {
3463
const { disabled } = props;
3564
if (disabled) {
3665
return;
3766
}
3867
visible.value = !visible.value;
3968
};
69+
4070
const inputRef = shallowRef();
4171
const focus = () => {
4272
inputRef.value?.focus();
@@ -74,7 +104,9 @@ export default defineComponent({
74104
const renderPassword = () => {
75105
const { size, visibilityToggle, ...restProps } = props;
76106

77-
const suffixIcon = visibilityToggle && getIcon(prefixCls.value);
107+
const suffixIcon =
108+
(typeof visibilityToggle === 'boolean' ? visibilityToggle : true) &&
109+
getIcon(prefixCls.value);
78110
const inputClassName = classNames(prefixCls.value, attrs.class, {
79111
[`${prefixCls.value}-${size}`]: !!size,
80112
});

components/input/demo/password-input.vue

+31-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,39 @@ Input type of password.
1616

1717
</docs>
1818
<template>
19-
<a-input-password v-model:value="value" placeholder="input password" />
19+
<a-space direction="vertical" size="middle" style="width: 100%">
20+
<a-input-password v-model:value="value" placeholder="input password" />
21+
<a-input-password v-model:value="value2" placeholder="input password">
22+
<template #iconRender="visible">
23+
<EyeTwoTone v-if="visible"></EyeTwoTone>
24+
<EyeInvisibleOutlined v-else></EyeInvisibleOutlined>
25+
</template>
26+
</a-input-password>
27+
<a-space>
28+
<a-input-password
29+
v-model:value="value3"
30+
placeholder="input password"
31+
:visibility-toggle="toggle"
32+
/>
33+
<a-button @click="visible = !visible">{{ visible ? 'Hide' : 'Show' }}</a-button>
34+
</a-space>
35+
</a-space>
2036
</template>
2137
<script lang="ts" setup>
2238
import { ref } from 'vue';
39+
import { EyeTwoTone, EyeInvisibleOutlined } from '@ant-design/icons-vue';
2340
const value = ref<string>('');
41+
const value2 = ref<string>('');
42+
const value3 = ref<string>('');
43+
44+
const visible = ref<boolean>(false);
45+
const onVisibleChange = (_visible: boolean) => {
46+
visible.value = _visible;
47+
console.log(_visible);
48+
};
49+
50+
const toggle = {
51+
visible,
52+
onVisibleChange,
53+
};
2454
</script>

components/input/index.en-US.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Supports all props of `Input`.
9494

9595
#### Input.Password (Added in 1.14.0)
9696

97-
| Property | Description | Type | Default |
98-
| ---------------- | -------------------------- | ------- | ------- |
99-
| visibilityToggle | Whether show toggle button | boolean | true |
97+
| Property | Description | Type | Default |
98+
| --- | --- | --- | --- |
99+
| iconRender | Custom toggle button | slot | - |
100+
| visibilityToggle | Whether show toggle button or control password visible | boolean \| VisibilityToggleProps | true |

components/input/index.zh-CN.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*sBqqTatJ-AkAAA
9595

9696
#### Input.Password (1.14.0 中新增)
9797

98-
| 参数 | 说明 | 类型 | 默认值 |
99-
| ---------------- | ---------------- | ------- | ------ |
100-
| visibilityToggle | 是否显示切换按钮 | boolean | true |
98+
| 参数 | 说明 | 类型 | 默认值 |
99+
| ---------------- | -------------------------------- | -------------------------------- | ------ |
100+
| iconRender | 自定义切换按钮 | slot | - |
101+
| visibilityToggle | 是否显示切换按钮或者控制密码显隐 | boolean \| VisibilityToggleProps | true |

0 commit comments

Comments
 (0)