FEAT => 本次更新项目为:将设备选择组件中的属性名称从 selectedDevices 更改为 selectedOptions,以统一命名规范并提升可读性

This commit is contained in:
超级老白兔
2025-08-08 11:36:05 +08:00
parent 08d851ec81
commit 98b3d0dff7
3 changed files with 44 additions and 68 deletions

View File

@@ -5,3 +5,23 @@ export interface FriendSelectionItem {
avatar: string;
[key: string]: any;
}
// 组件属性接口
export interface FriendSelectionProps {
selectedOptions: FriendSelectionItem[];
onSelect: (friends: FriendSelectionItem[]) => void;
deviceIds?: string[];
enableDeviceFilter?: boolean;
placeholder?: string;
className?: string;
visible?: boolean; // 新增
onVisibleChange?: (visible: boolean) => void; // 新增
selectedListMaxHeight?: number;
showInput?: boolean;
showSelectedList?: boolean;
readonly?: boolean;
onConfirm?: (
selectedIds: number[],
selectedItems: FriendSelectionItem[],
) => void; // 新增
}

View File

@@ -7,38 +7,11 @@ import style from "./index.module.scss";
import Layout from "@/components/Layout/Layout";
import PopupHeader from "@/components/PopuLayout/header";
import PopupFooter from "@/components/PopuLayout/footer";
// 微信好友接口类型
interface WechatFriend {
id: string;
nickname: string;
wechatId: string;
avatar: string;
customer: string;
}
// 组件属性接口
interface FriendSelectionProps {
selectedFriends: string[];
onSelect: (friends: string[]) => void;
onSelectDetail?: (friends: WechatFriend[]) => void;
deviceIds?: string[];
enableDeviceFilter?: boolean;
placeholder?: string;
className?: string;
visible?: boolean; // 新增
onVisibleChange?: (visible: boolean) => void; // 新增
selectedListMaxHeight?: number;
showInput?: boolean;
showSelectedList?: boolean;
readonly?: boolean;
onConfirm?: (selectedIds: string[], selectedItems: WechatFriend[]) => void; // 新增
}
import { FriendSelectionProps, FriendSelectionItem } from "./data";
export default function FriendSelection({
selectedFriends,
selectedOptions,
onSelect,
onSelectDetail,
deviceIds = [],
enableDeviceFilter = true,
placeholder = "选择微信好友",
@@ -52,7 +25,7 @@ export default function FriendSelection({
onConfirm,
}: FriendSelectionProps) {
const [popupVisible, setPopupVisible] = useState(false);
const [friends, setFriends] = useState<WechatFriend[]>([]);
const [friends, setFriends] = useState<FriendSelectionItem[]>([]);
const [searchQuery, setSearchQuery] = useState("");
const [currentPage, setCurrentPage] = useState(1);
const [totalPages, setTotalPages] = useState(1);
@@ -125,54 +98,37 @@ export default function FriendSelection({
};
// 处理好友选择
const handleFriendToggle = (friendId: string) => {
const handleFriendToggle = (friend: FriendSelectionItem) => {
if (readonly) return;
const newSelectedFriends = selectedFriends.includes(friendId)
? selectedFriends.filter(id => id !== friendId)
: [...selectedFriends, friendId];
const newSelectedFriends = selectedOptions
.map(v => v.id)
.includes(friend.id)
? selectedOptions.filter(v => v.id !== friend.id)
: selectedOptions.concat(friend);
onSelect(newSelectedFriends);
// 如果有 onSelectDetail 回调,传递完整的好友对象
if (onSelectDetail) {
const selectedFriendObjs = friends.filter(friend =>
newSelectedFriends.includes(friend.id),
);
onSelectDetail(selectedFriendObjs);
}
};
// 获取显示文本
const getDisplayText = () => {
if (selectedFriends.length === 0) return "";
return `已选择 ${selectedFriends.length} 个好友`;
if (selectedOptions.length === 0) return "";
return `已选择 ${selectedOptions.length} 个好友`;
};
// 获取已选好友详细信息
const selectedFriendObjs = [
...friends.filter(friend => selectedFriends.includes(friend.id)),
...selectedFriends
.filter(id => !friends.some(friend => friend.id === id))
.map(id => ({
id,
nickname: id,
wechatId: id,
avatar: "",
customer: "",
})),
];
// 删除已选好友
const handleRemoveFriend = (id: string) => {
const handleRemoveFriend = (id: number) => {
if (readonly) return;
onSelect(selectedFriends.filter(d => d !== id));
onSelect(selectedOptions.filter(v => v.id !== id));
};
// 确认选择
const handleConfirm = () => {
if (onConfirm) {
onConfirm(selectedFriends, selectedFriendObjs);
onConfirm(
selectedOptions.map(v => v.id),
selectedOptions,
);
}
setRealVisible(false);
};
@@ -198,7 +154,7 @@ export default function FriendSelection({
</div>
)}
{/* 已选好友列表窗口 */}
{showSelectedList && selectedFriendObjs.length > 0 && (
{showSelectedList && selectedOptions.length > 0 && (
<div
className={style.selectedListWindow}
style={{
@@ -210,7 +166,7 @@ export default function FriendSelection({
background: "#fff",
}}
>
{selectedFriendObjs.map(friend => (
{selectedOptions.map(friend => (
<div
key={friend.id}
className={style.selectedListRow}
@@ -280,7 +236,7 @@ export default function FriendSelection({
currentPage={currentPage}
totalPages={totalPages}
loading={loading}
selectedCount={selectedFriends.length}
selectedCount={selectedOptions.length}
onPageChange={setCurrentPage}
onCancel={() => setRealVisible(false)}
onConfirm={handleConfirm}
@@ -298,17 +254,17 @@ export default function FriendSelection({
<label
key={friend.id}
className={style.friendItem}
onClick={() => !readonly && handleFriendToggle(friend.id)}
onClick={() => !readonly && handleFriendToggle(friend)}
>
<div className={style.radioWrapper}>
<div
className={
selectedFriends.includes(friend.id)
selectedOptions.map(v => v.id).includes(friend.id)
? style.radioSelected
: style.radioUnselected
}
>
{selectedFriends.includes(friend.id) && (
{selectedOptions.map(v => v.id).includes(friend.id) && (
<div className={style.radioDot}></div>
)}
</div>

View File

@@ -48,7 +48,7 @@ const ComponentTest: React.FC = () => {
<div style={{ padding: "16px 0" }}>
<h3 style={{ marginBottom: 16 }}>FriendSelection </h3>
<FriendSelection
selectedFriends={selectedFriendsOptions}
selectedOptions={selectedFriendsOptions}
onSelect={setSelectedFriendsOptions}
placeholder="请选择微信好友"
showSelectedList={true}