import { Checkbox, Input } from 'antd'; import React, { useCallback, useState } from 'react'; import { getCachedUserInfo, t, useAppSelector, useAsync, } from 'tailchat-shared'; import _take from 'lodash/take'; import _without from 'lodash/without'; import { Avatar } from './Avatar'; interface FriendPickerProps { /** * 排除的用户id * 在选择好友时会进行过滤 */ withoutUserIds?: string[]; /** * 是否包含搜索框 * 默认为 true */ withSearch?: boolean; selectedIds: string[]; onChange: (selectedIds: string[]) => void; } export const FriendPicker: React.FC = React.memo((props) => { const { withoutUserIds = [], withSearch = true, selectedIds, onChange, } = props; const [searchValue, setSearchValue] = useState(''); const friendIds = useAppSelector((state) => state.user.friends.filter((id) => !withoutUserIds.includes(id)) ); const { value: friendInfoList = [] } = useAsync(() => { return Promise.all(friendIds.map((id) => getCachedUserInfo(id))); }, [friendIds.join(',')]); const handleSelectUser = useCallback( (userId: string, isSelected: boolean) => { if (isSelected === true) { // 添加 if (selectedIds.includes(userId)) { return; } typeof onChange === 'function' && onChange([...selectedIds, userId]); } else { // 移除 typeof onChange === 'function' && onChange(_without(selectedIds, userId)); } }, [selectedIds, onChange] ); return (
{withSearch && ( setSearchValue(e.target.value)} /> )}
{t('已选择 {{num}} 项', { num: selectedIds.length, })}
{_take( friendInfoList.filter((info) => info.nickname.includes(searchValue)), 5 ).map((info) => { return (
handleSelectUser(info._id, e.target.checked)} >
{info.nickname}
); })}
); }); FriendPicker.displayName = 'FriendPicker';