Commit 2c7e660d59928fbf9c12f3595427f154262c9c3a
Merge branch 'feature-PrivatePocket' into 'dev'
fix: 修改隐私钱包添加弹窗查询 See merge request !40
Showing
1 changed file
with
22 additions
and
6 deletions
src/pages/Prepaid/PrivatePocket/components/AddPrivatePocketModal.tsx
@@ -5,7 +5,7 @@ import { | @@ -5,7 +5,7 @@ import { | ||
5 | } from '@/services'; | 5 | } from '@/services'; |
6 | import { getUserInfo } from '@/utils/user'; | 6 | import { getUserInfo } from '@/utils/user'; |
7 | import { Form, Modal, Select, message } from 'antd'; | 7 | import { Form, Modal, Select, message } from 'antd'; |
8 | -import React, { useEffect, useState } from 'react'; | 8 | +import React, { useEffect, useRef, useState } from 'react'; |
9 | 9 | ||
10 | interface AddPrivatePocketModalProps { | 10 | interface AddPrivatePocketModalProps { |
11 | visible: boolean; | 11 | visible: boolean; |
@@ -39,6 +39,7 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | @@ -39,6 +39,7 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | ||
39 | const [searchKeywords, setSearchKeywords] = useState<string>(''); | 39 | const [searchKeywords, setSearchKeywords] = useState<string>(''); |
40 | const userInfo = getUserInfo(); | 40 | const userInfo = getUserInfo(); |
41 | const userName = userInfo?.username || ''; | 41 | const userName = userInfo?.username || ''; |
42 | + const searchTimerRef = useRef<any>(null); | ||
42 | 43 | ||
43 | const fetchSalesCodeList = async () => { | 44 | const fetchSalesCodeList = async () => { |
44 | try { | 45 | try { |
@@ -55,6 +56,10 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | @@ -55,6 +56,10 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | ||
55 | // Define a function to fetch user list that can be called from multiple places | 56 | // Define a function to fetch user list that can be called from multiple places |
56 | const fetchUserList = async (keywords: string) => { | 57 | const fetchUserList = async (keywords: string) => { |
57 | try { | 58 | try { |
59 | + if (!keywords || keywords.trim() === '') { | ||
60 | + return []; | ||
61 | + } | ||
62 | + | ||
58 | const res = await postCanrdApiUserList({ | 63 | const res = await postCanrdApiUserList({ |
59 | data: { keywords: keywords, pageSize: 1000000 }, | 64 | data: { keywords: keywords, pageSize: 1000000 }, |
60 | }); | 65 | }); |
@@ -109,6 +114,21 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | @@ -109,6 +114,21 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | ||
109 | } | 114 | } |
110 | }; | 115 | }; |
111 | 116 | ||
117 | + // Function to handle search with debounce | ||
118 | + const handleSearch = (value: string) => { | ||
119 | + setSearchKeywords(value); | ||
120 | + | ||
121 | + // Clear any existing timer | ||
122 | + if (searchTimerRef.current) { | ||
123 | + clearTimeout(searchTimerRef.current); | ||
124 | + } | ||
125 | + | ||
126 | + // Set a new timer to fetch users after 1000ms of inactivity | ||
127 | + searchTimerRef.current = setTimeout(() => { | ||
128 | + fetchUserList(value); | ||
129 | + }, 1000); | ||
130 | + }; | ||
131 | + | ||
112 | return ( | 132 | return ( |
113 | <Modal | 133 | <Modal |
114 | title="添加隐私钱包" | 134 | title="添加隐私钱包" |
@@ -131,11 +151,7 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | @@ -131,11 +151,7 @@ const AddPrivatePocketModal: React.FC<AddPrivatePocketModalProps> = ({ | ||
131 | defaultActiveFirstOption={false} | 151 | defaultActiveFirstOption={false} |
132 | notFoundContent={null} | 152 | notFoundContent={null} |
133 | filterOption={false} // Disable built-in filtering to use server-side search | 153 | filterOption={false} // Disable built-in filtering to use server-side search |
134 | - onSearch={async (value) => { | ||
135 | - setSearchKeywords(value); | ||
136 | - // Immediately fetch results when searching | ||
137 | - await fetchUserList(value); | ||
138 | - }} | 154 | + onSearch={handleSearch} // Use the debounced search handler |
139 | onChange={(value) => { | 155 | onChange={(value) => { |
140 | // When a value is selected, find the corresponding phone from userList | 156 | // When a value is selected, find the corresponding phone from userList |
141 | // or use the value directly if it's a custom entry | 157 | // or use the value directly if it's a custom entry |