Skip to content
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

fix(input): show password is invalid #1464

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 0 additions & 9 deletions src/form/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ exports[`Form > Form horizontalVue demo works fine 1`] = `
<div
class="t-input__wrap--suffix-icon"
>

<svg
class="t-icon t-icon-browse-off"
data-v-799be585=""
fill="none"
height="1em"
viewBox="0 0 26 24"
Expand All @@ -152,7 +150,6 @@ exports[`Form > Form horizontalVue demo works fine 1`] = `
fill="currentColor"
/>
</svg>

</div>
</div>
<!---->
Expand Down Expand Up @@ -1825,10 +1822,8 @@ exports[`Form > Form mobileVue demo works fine 1`] = `
<div
class="t-input__wrap--suffix-icon"
>

<svg
class="t-icon t-icon-browse-off"
data-v-799be585=""
fill="none"
height="1em"
viewBox="0 0 26 24"
Expand All @@ -1839,7 +1834,6 @@ exports[`Form > Form mobileVue demo works fine 1`] = `
fill="currentColor"
/>
</svg>

</div>
</div>
<!---->
Expand Down Expand Up @@ -3379,10 +3373,8 @@ exports[`Form > Form verticalVue demo works fine 1`] = `
<div
class="t-input__wrap--suffix-icon"
>

<svg
class="t-icon t-icon-browse-off"
data-v-713b33d2=""
fill="none"
height="1em"
viewBox="0 0 26 24"
Expand All @@ -3393,7 +3385,6 @@ exports[`Form > Form verticalVue demo works fine 1`] = `
fill="currentColor"
/>
</svg>

</div>
</div>
<!---->
Expand Down
4 changes: 0 additions & 4 deletions src/input/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,6 @@ exports[`Input > Input mobileVue demo works fine 1`] = `
<div
class="t-input__wrap--suffix-icon"
>

<svg
class="t-icon t-icon-browse-off"
fill="none"
Expand All @@ -1175,7 +1174,6 @@ exports[`Input > Input mobileVue demo works fine 1`] = `
fill="currentColor"
/>
</svg>

</div>
</div>
<!---->
Expand Down Expand Up @@ -2122,7 +2120,6 @@ exports[`Input > Input specialVue demo works fine 1`] = `
<div
class="t-input__wrap--suffix-icon"
>

<svg
class="t-icon t-icon-browse-off"
fill="none"
Expand All @@ -2135,7 +2132,6 @@ exports[`Input > Input specialVue demo works fine 1`] = `
fill="currentColor"
/>
</svg>

</div>
</div>
<!---->
Expand Down
14 changes: 14 additions & 0 deletions src/input/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ describe('Input.vue', async () => {
expect(input.element.getAttribute('type')).toBe('number');
});

it(': type=password', async () => {
const wrapper = mount(<Input label="标题" type="password" />);
wrapper.find('.t-icon-browse-off').trigger('click');
await wrapper.vm.$nextTick();
expect(wrapper.find('.t-icon-browse').exists()).toBeTruthy();
const attrDom = wrapper.find('input');
expect(attrDom.attributes('type')).toBe('text');
wrapper.find('.t-icon-browse').trigger('click');
await wrapper.vm.$nextTick();
expect(wrapper.find('.t-icon-browse-off').exists()).toBeTruthy();
const attrDom1 = wrapper.find('input');
expect(attrDom1.attributes('type')).toBe('password');
});

it(': onBlur', async () => {
const onBlur = vi.fn();
const wrapper = mount(<Input label="标题" onBlur={onBlur} />);
Expand Down
35 changes: 29 additions & 6 deletions src/input/input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PropType, ref, computed, defineComponent, toRefs, nextTick, watch } from 'vue';
import { CloseCircleFilledIcon as TCloseCircleFilledIcon } from 'tdesign-icons-vue-next';
import {
BrowseIcon as TBrowseIcon,
BrowseOffIcon as TBrowseOffIcon,
CloseCircleFilledIcon as TCloseCircleFilledIcon,
} from 'tdesign-icons-vue-next';
import { useFocus } from '@vueuse/core';
import config from '../config';
import InputProps from './props';
Expand All @@ -10,10 +14,9 @@ import { usePrefixClass } from '../hooks/useClass';
import { useTNodeJSX } from '../hooks/tnode';

const { prefix } = config;
const name = `${prefix}-input`;

export default defineComponent({
name,
name: `${prefix}-input`,
props: {
...InputProps,
labelAlign: {
Expand Down Expand Up @@ -41,7 +44,7 @@ export default defineComponent({
const [innerValue] = useDefault<string, TdInputProps>(props, context.emit, 'value', 'change');

const status = props.status || 'default';

const renderType = ref(props.type);
const { focused } = useFocus(inputRef, { initialValue: props.autofocus });

const inputClasses = computed(() => [
Expand Down Expand Up @@ -124,6 +127,10 @@ export default defineComponent({
inputValueChangeHandle(e);
};

const handlePwdIconClick = () => {
renderType.value = renderType.value === 'password' ? 'text' : 'password';
};

watch(autofocus, (autofocus, prevAutofocus) => {
if (autofocus === true) {
nextTick(() => {
Expand All @@ -132,6 +139,14 @@ export default defineComponent({
}
});

watch(
() => props.type,
(v) => {
renderType.value = v;
},
{ immediate: true },
);

return () => {
const readerPrefix = () => {
const prefixIcon = readerTNodeJSX('prefixIcon');
Expand Down Expand Up @@ -163,7 +178,15 @@ export default defineComponent({
};

const readerSuffixIcon = () => {
const suffixIcon = readerTNodeJSX('suffixIcon');
let suffixIcon = readerTNodeJSX('suffixIcon');
if (props.type === 'password') {
if (renderType.value === 'password') {
suffixIcon = <TBrowseOffIcon onClick={handlePwdIconClick} />;
} else if (renderType.value === 'text') {
suffixIcon = <TBrowseIcon onClick={handlePwdIconClick} />;
}
}

if (!suffixIcon) {
return null;
}
Expand All @@ -188,7 +211,7 @@ export default defineComponent({
value={innerValue.value}
name={props.name}
class={inputClasses.value}
type={props.type}
type={renderType.value}
disabled={isDisabled.value}
autocomplete={props.autocomplete ? 'On' : 'Off'}
placeholder={props.placeholder}
Expand Down
Loading