feat[input.password]: Input.Password support suffix property (#54342)

This commit is contained in:
QdabuliuQ
2025-07-12 10:36:46 +08:00
committed by GitHub
parent 6e9e879a68
commit bf37b20527
6 changed files with 151 additions and 6 deletions

View File

@@ -25,6 +25,10 @@ export interface PasswordProps extends InputProps {
readonly inputPrefixCls?: string;
readonly action?: 'click' | 'hover';
visibilityToggle?: boolean | VisibilityToggle;
/**
* @since 5.27.0
*/
suffix?: React.ReactNode;
iconRender?: (visible: boolean) => React.ReactNode;
}
@@ -41,6 +45,7 @@ const Password = React.forwardRef<InputRef, PasswordProps>((props, ref) => {
action = 'click',
visibilityToggle = true,
iconRender = defaultIconRender,
suffix,
} = props;
// ===================== Disabled =====================
@@ -126,7 +131,12 @@ const Password = React.forwardRef<InputRef, PasswordProps>((props, ref) => {
type: visible ? 'text' : 'password',
className: inputClassName,
prefixCls: inputPrefixCls,
suffix: suffixIcon,
suffix: (
<>
{suffixIcon}
{suffix}
</>
),
};
if (size) {

View File

@@ -7,6 +7,7 @@ import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
import Password from '../Password';
import { LockOutlined } from '@ant-design/icons';
describe('Input.Password', () => {
focusTest(Input.Password, { refFocus: true });
@@ -153,4 +154,16 @@ describe('Input.Password', () => {
fireEvent.click(container.querySelector('.ant-input-password-icon')!);
expect(handlePasswordVisibleChange).toHaveBeenCalledTimes(2);
});
it('should support suffix', () => {
const { container } = render(<Input.Password suffix={<LockOutlined />} />);
expect(container.querySelector('.anticon')).toBeTruthy();
});
it('should support custom icon by suffix', () => {
const { container } = render(
<Input.Password suffix={<div className="custom-icon">custom icon</div>} />,
);
expect(container.querySelector('.custom-icon')).toBeTruthy();
});
});

View File

@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`renders components/input/demo/addon.tsx extend context correctly 1`] = `
<div
@@ -11300,6 +11300,64 @@ Array [
RMB
</span>
</span>,
<br />,
<br />,
<span
class="ant-input-affix-wrapper ant-input-outlined ant-input-password"
>
<input
class="ant-input"
placeholder="input password support suffix"
type="password"
value=""
/>
<span
class="ant-input-suffix"
>
<span
aria-label="eye-invisible"
class="anticon anticon-eye-invisible ant-input-password-icon"
role="img"
tabindex="-1"
>
<svg
aria-hidden="true"
data-icon="eye-invisible"
fill="currentColor"
focusable="false"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M942.2 486.2Q889.47 375.11 816.7 305l-50.88 50.88C807.31 395.53 843.45 447.4 874.7 512 791.5 684.2 673.4 766 512 766q-72.67 0-133.87-22.38L323 798.75Q408 838 512 838q288.3 0 430.2-300.3a60.29 60.29 0 000-51.5zm-63.57-320.64L836 122.88a8 8 0 00-11.32 0L715.31 232.2Q624.86 186 512 186q-288.3 0-430.2 300.3a60.3 60.3 0 000 51.5q56.69 119.4 136.5 191.41L112.48 835a8 8 0 000 11.31L155.17 889a8 8 0 0011.31 0l712.15-712.12a8 8 0 000-11.32zM149.3 512C232.6 339.8 350.7 258 512 258c54.54 0 104.13 9.36 149.12 28.39l-70.3 70.3a176 176 0 00-238.13 238.13l-83.42 83.42C223.1 637.49 183.3 582.28 149.3 512zm246.7 0a112.11 112.11 0 01146.2-106.69L401.31 546.2A112 112 0 01396 512z"
/>
<path
d="M508 624c-3.46 0-6.87-.16-10.25-.47l-52.82 52.82a176.09 176.09 0 00227.42-227.42l-52.82 52.82c.31 3.38.47 6.79.47 10.25a111.94 111.94 0 01-112 112z"
/>
</svg>
</span>
<span
aria-label="lock"
class="anticon anticon-lock"
role="img"
>
<svg
aria-hidden="true"
data-icon="lock"
fill="currentColor"
focusable="false"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M832 464h-68V240c0-70.7-57.3-128-128-128H388c-70.7 0-128 57.3-128 128v224h-68c-17.7 0-32 14.3-32 32v384c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V496c0-17.7-14.3-32-32-32zM332 240c0-30.9 25.1-56 56-56h248c30.9 0 56 25.1 56 56v224H332V240zm460 600H232V536h560v304zM484 701v53c0 4.4 3.6 8 8 8h40c4.4 0 8-3.6 8-8v-53a48.01 48.01 0 10-56 0z"
/>
</svg>
</span>
</span>
</span>,
]
`;

View File

@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`renders components/input/demo/addon.tsx correctly 1`] = `
<div
@@ -4622,6 +4622,64 @@ Array [
RMB
</span>
</span>,
<br />,
<br />,
<span
class="ant-input-affix-wrapper ant-input-outlined ant-input-password"
>
<input
class="ant-input"
placeholder="input password support suffix"
type="password"
value=""
/>
<span
class="ant-input-suffix"
>
<span
aria-label="eye-invisible"
class="anticon anticon-eye-invisible ant-input-password-icon"
role="img"
tabindex="-1"
>
<svg
aria-hidden="true"
data-icon="eye-invisible"
fill="currentColor"
focusable="false"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M942.2 486.2Q889.47 375.11 816.7 305l-50.88 50.88C807.31 395.53 843.45 447.4 874.7 512 791.5 684.2 673.4 766 512 766q-72.67 0-133.87-22.38L323 798.75Q408 838 512 838q288.3 0 430.2-300.3a60.29 60.29 0 000-51.5zm-63.57-320.64L836 122.88a8 8 0 00-11.32 0L715.31 232.2Q624.86 186 512 186q-288.3 0-430.2 300.3a60.3 60.3 0 000 51.5q56.69 119.4 136.5 191.41L112.48 835a8 8 0 000 11.31L155.17 889a8 8 0 0011.31 0l712.15-712.12a8 8 0 000-11.32zM149.3 512C232.6 339.8 350.7 258 512 258c54.54 0 104.13 9.36 149.12 28.39l-70.3 70.3a176 176 0 00-238.13 238.13l-83.42 83.42C223.1 637.49 183.3 582.28 149.3 512zm246.7 0a112.11 112.11 0 01146.2-106.69L401.31 546.2A112 112 0 01396 512z"
/>
<path
d="M508 624c-3.46 0-6.87-.16-10.25-.47l-52.82 52.82a176.09 176.09 0 00227.42-227.42l-52.82 52.82c.31 3.38.47 6.79.47 10.25a111.94 111.94 0 01-112 112z"
/>
</svg>
</span>
<span
aria-label="lock"
class="anticon anticon-lock"
role="img"
>
<svg
aria-hidden="true"
data-icon="lock"
fill="currentColor"
focusable="false"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M832 464h-68V240c0-70.7-57.3-128-128-128H388c-70.7 0-128 57.3-128 128v224h-68c-17.7 0-32 14.3-32 32v384c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V496c0-17.7-14.3-32-32-32zM332 240c0-30.9 25.1-56 56-56h248c30.9 0 56 25.1 56 56v224H332V240zm460 600H232V536h560v304zM484 701v53c0 4.4 3.6 8 8 8h40c4.4 0 8-3.6 8-8v-53a48.01 48.01 0 10-56 0z"
/>
</svg>
</span>
</span>
</span>,
]
`;

View File

@@ -1,7 +1,7 @@
## zh-CN
在输入框上添加前缀或后缀图标。
在输入框上添加前缀或后缀图标。注意Input.Password 的 `suffix` 属性在 `>=5.27.0` 版本支持。
## en-US
Add a prefix or suffix icons inside input.
Add a prefix or suffix icons inside input. Note: The `suffix` prop for Input.Password is supported starting from version `5.27.0`.

View File

@@ -1,5 +1,5 @@
import React from 'react';
import { InfoCircleOutlined, UserOutlined } from '@ant-design/icons';
import { InfoCircleOutlined, UserOutlined, LockOutlined } from '@ant-design/icons';
import { Input, Tooltip } from 'antd';
const App: React.FC = () => (
@@ -19,6 +19,12 @@ const App: React.FC = () => (
<br />
<br />
<Input prefix="¥" suffix="RMB" disabled />
<br />
<br />
<Input.Password
suffix={<LockOutlined />} // `suffix` available since `5.27.0`
placeholder="input password support suffix"
/>
</>
);