Skip to content

Commit 24a5331

Browse files
authored
feat: PasswordInput 新增清空输入框ref函数 (#58)
1 parent 55aaddf commit 24a5331

File tree

5 files changed

+2234
-2154
lines changed

5 files changed

+2234
-2154
lines changed

packages/rc-ui-lib/src/password-input/PasswordInput.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const PasswordInput = forwardRef<PasswordInputInstance, PasswordInputProps>((pro
6363
onBlur();
6464
nativeInputRef.current?.blur();
6565
},
66+
resetValue: () => {
67+
setInputValue('');
68+
},
6669
}));
6770

6871
const onTouchStart = (event) => {

packages/rc-ui-lib/src/password-input/PropsType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ export interface PasswordInputProps {
3232
export type PasswordInputInstance = {
3333
focus: () => void;
3434
blur: () => void;
35+
/**
36+
* 清空密码
37+
* @returns
38+
*/
39+
resetValue: () => void;
3540
};

packages/rc-ui-lib/src/password-input/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,20 @@ const handleFill = () => {
145145
| onBlur | 输入框失焦回调 | - |
146146
| onFocus | 输入框聚焦回调 | - |
147147

148+
### Ref
149+
150+
| 事件名 | 说明 | 回调参数 |
151+
| ---------- | ---------------------------------------- | -------- |
152+
| focus | 输入框聚焦回调,若是原生输入则会自动聚焦 | - |
153+
| blur | 输入框失焦,若是原生输入则会自动失焦 | - |
154+
| resetValue | 清空输入框 | - |
155+
148156
### 类型定义
149157

150158
组件导出以下类型定义:
151159

152160
```js
153-
import type { PasswordInputProps } from 'rc-ui-lib';
161+
import type { PasswordInputProps, PasswordInputInstance } from 'rc-ui-lib';
154162
```
155163

156164
## 主题定制

packages/rc-ui-lib/src/password-input/demo/index.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import React, { useRef, useState, useEffect } from 'react';
22
import { components } from 'site-mobile-demo';
33
import PasswordInput from '..';
4+
import Button from '../../button';
45
import { NumberKeyboard } from '../..';
56
import './style.less';
67

8+
import type { PasswordInputInstance } from '..';
9+
710
const initialValue = {
811
nativeInput: '123',
912
showInfo: '123',
1013
addGutter: '123',
1114
basicUsage: '123',
1215
removeMask: '123',
1316
customLength: '123',
17+
setValue: '123',
1418
};
1519

1620
export default (): React.ReactNode => {
1721
const { DemoSection, DemoBlock } = components;
1822

19-
const [values] = useState(initialValue);
23+
const [values, setValues] = useState(initialValue);
2024
const [current, setCurrent] = useState(null);
2125

2226
const [errorInfo, setErrorInfo] = useState<string>('');
@@ -27,6 +31,9 @@ export default (): React.ReactNode => {
2731
const addGutterRef = useRef(null);
2832
const removeMaskRef = useRef(null);
2933
const showInfoRef = useRef(null);
34+
const setValueRef = useRef(null);
35+
36+
const psdRef = useRef<PasswordInputInstance>(null);
3037

3138
const refMap = {
3239
showInfo: showInfoRef,
@@ -35,6 +42,7 @@ export default (): React.ReactNode => {
3542
basicUsage: basicUsageRef,
3643
removeMask: removeMaskRef,
3744
customLength: customLengthRef,
45+
setValue: setValueRef,
3846
};
3947

4048
useEffect(() => {
@@ -127,6 +135,27 @@ export default (): React.ReactNode => {
127135
keyboard={<NumberKeyboard />}
128136
/>
129137
</DemoBlock>
138+
<DemoBlock ref={setValueRef} card title="手动清空密码">
139+
<Button
140+
type="primary"
141+
onClick={() => {
142+
psdRef.current?.resetValue();
143+
}}
144+
style={{
145+
margin: 'var(--rc-password-input-margin) var(--rc-padding-md)',
146+
}}
147+
>
148+
清空密码
149+
</Button>
150+
<PasswordInput
151+
ref={psdRef}
152+
value={values.setValue}
153+
mask={false}
154+
focused={current === 'setValue'}
155+
onBlur={handleBlur}
156+
onFocus={() => handleFocus('setValue')}
157+
/>
158+
</DemoBlock>
130159
</DemoSection>
131160
);
132161
};

0 commit comments

Comments
 (0)