2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2021-12-14 15:50:22 +00:00
|
|
|
import { isValidElement } from '../_util/props-util';
|
2020-12-09 08:07:27 +00:00
|
|
|
import { cloneElement } from '../_util/vnode';
|
2019-04-07 09:19:18 +00:00
|
|
|
import Input from './Input';
|
2020-03-26 03:53:51 +00:00
|
|
|
import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
|
|
|
|
import EyeInvisibleOutlined from '@ant-design/icons-vue/EyeInvisibleOutlined';
|
2021-12-14 15:50:22 +00:00
|
|
|
import type { InputProps } from './inputProps';
|
2019-04-07 09:19:18 +00:00
|
|
|
import inputProps from './inputProps';
|
2021-12-14 15:50:22 +00:00
|
|
|
import { computed, defineComponent, ref } from 'vue';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import omit from '../_util/omit';
|
2019-04-07 09:19:18 +00:00
|
|
|
|
|
|
|
const ActionMap = {
|
2020-06-27 06:02:30 +00:00
|
|
|
click: 'onClick',
|
|
|
|
hover: 'onMouseover',
|
2019-04-07 09:19:18 +00:00
|
|
|
};
|
2021-12-14 15:50:22 +00:00
|
|
|
const defaultIconRender = (visible: boolean) =>
|
|
|
|
visible ? <EyeOutlined /> : <EyeInvisibleOutlined />;
|
2020-10-16 06:24:14 +00:00
|
|
|
export default defineComponent({
|
2019-04-07 09:19:18 +00:00
|
|
|
name: 'AInputPassword',
|
2020-01-18 09:06:09 +00:00
|
|
|
inheritAttrs: false,
|
2019-04-07 09:19:18 +00:00
|
|
|
props: {
|
2022-03-26 14:52:54 +00:00
|
|
|
...inputProps(),
|
|
|
|
prefixCls: String,
|
|
|
|
inputPrefixCls: String,
|
2022-04-19 05:51:22 +00:00
|
|
|
action: { type: String, default: 'click' },
|
2022-03-26 14:52:54 +00:00
|
|
|
visibilityToggle: { type: Boolean, default: true },
|
|
|
|
iconRender: Function,
|
2020-10-16 06:24:14 +00:00
|
|
|
},
|
2021-12-14 15:50:22 +00:00
|
|
|
setup(props, { slots, attrs, expose }) {
|
|
|
|
const visible = ref(false);
|
|
|
|
const onVisibleChange = () => {
|
|
|
|
const { disabled } = props;
|
|
|
|
if (disabled) {
|
2020-03-07 11:45:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-14 15:50:22 +00:00
|
|
|
visible.value = !visible.value;
|
|
|
|
};
|
|
|
|
const inputRef = ref();
|
|
|
|
const focus = () => {
|
|
|
|
inputRef.value?.focus();
|
|
|
|
};
|
|
|
|
const blur = () => {
|
|
|
|
inputRef.value?.blur();
|
|
|
|
};
|
|
|
|
expose({
|
|
|
|
focus,
|
|
|
|
blur,
|
|
|
|
});
|
|
|
|
const getIcon = (prefixCls: string) => {
|
|
|
|
const { action, iconRender = slots.iconRender || defaultIconRender } = props;
|
|
|
|
const iconTrigger = ActionMap[action!] || '';
|
|
|
|
const icon = iconRender(visible.value);
|
2019-04-07 09:19:18 +00:00
|
|
|
const iconProps = {
|
2021-12-14 15:50:22 +00:00
|
|
|
[iconTrigger]: onVisibleChange,
|
|
|
|
class: `${prefixCls}-icon`,
|
|
|
|
key: 'passwordIcon',
|
|
|
|
onMousedown: (e: MouseEvent) => {
|
2020-06-27 06:02:30 +00:00
|
|
|
// Prevent focused state lost
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15173
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2021-12-14 15:50:22 +00:00
|
|
|
onMouseup: (e: MouseEvent) => {
|
|
|
|
// Prevent caret position change
|
|
|
|
// https://github.com/ant-design/ant-design/issues/23524
|
2020-06-27 06:02:30 +00:00
|
|
|
e.preventDefault();
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
|
|
|
};
|
2021-12-14 15:50:22 +00:00
|
|
|
return cloneElement(isValidElement(icon) ? icon : <span>{icon}</span>, iconProps);
|
|
|
|
};
|
|
|
|
const { prefixCls, getPrefixCls } = useConfigInject('input-password', props);
|
|
|
|
const inputPrefixCls = computed(() => getPrefixCls('input', props.inputPrefixCls));
|
|
|
|
const renderPassword = () => {
|
|
|
|
const { size, visibilityToggle, ...restProps } = props;
|
2021-07-30 08:10:58 +00:00
|
|
|
|
2021-12-14 15:50:22 +00:00
|
|
|
const suffixIcon = visibilityToggle && getIcon(prefixCls.value);
|
|
|
|
const inputClassName = classNames(prefixCls.value, attrs.class, {
|
|
|
|
[`${prefixCls.value}-${size}`]: !!size,
|
|
|
|
});
|
2021-07-30 08:10:58 +00:00
|
|
|
|
2021-12-14 15:50:22 +00:00
|
|
|
const omittedProps = {
|
2021-12-15 14:27:07 +00:00
|
|
|
...omit(restProps, ['suffix', 'iconRender', 'action']),
|
2021-12-14 15:50:22 +00:00
|
|
|
...attrs,
|
|
|
|
type: visible.value ? 'text' : 'password',
|
|
|
|
class: inputClassName,
|
|
|
|
prefixCls: inputPrefixCls.value,
|
|
|
|
suffix: suffixIcon,
|
|
|
|
} as InputProps;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
omittedProps.size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Input ref={inputRef} {...omittedProps} v-slots={slots} />;
|
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
return renderPassword();
|
2019-10-17 09:59:23 +00:00
|
|
|
};
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
});
|