import { Button, Input, Modal, ModalProps, Select, SelectProps, Spin, Tag } from 'antd' import { memo, ReactNode, useEffect, useRef, useState } from 'react' import { Flexbox } from 'react-layout-kit' import { useStyle } from './style.ts' import { List, ListViewItem } from './List.tsx' import { useTranslation } from '@/i18n.ts' import DepartmentTree from '@/components/department-tree/DepartmentTree.tsx' import { DraggablePanel } from '@/components/draggable-panel' import { useAtom, useAtomValue } from 'jotai' import { userListAtom, userSearchAtom, } from '@/store/user.ts' import { IUser } from '@/types' import EmptyWrap from '@/components/empty/EmptyWrap.tsx' export interface UserSelectProps extends SelectProps { value?: any[] onChange?: (value: any[]) => void //多选 multiple?: boolean, renderValue?: (value: any[], def: ReactNode) => ReactNode } export interface UserModelProps extends Pick { value?: any[] onChange?: (value?: any[]) => void children?: ReactNode //多选 multiple?: boolean renderValue?: (value: any[], def: ReactNode) => ReactNode } export type UserPickerProps = | { type?: 'modal'; /** Props for the modal component */ } & UserModelProps | { type: 'select'; /** Props for the select component */ } & UserSelectProps const UserSelect = memo((props: UserSelectProps) => { return ( ) }) const UserModel = memo(({ multiple, children, value, onChange, ...props }: UserModelProps) => { const { styles, cx } = useStyle() const { t } = useTranslation() const [ innerValue, setValue ] = useState(() => { if (!multiple && !Array.isArray(value)) { return [ value ] } }) const [ , setSearch ] = useAtom(userSearchAtom) const { data: users, isPending } = useAtomValue(userListAtom) const [ open, setOpen ] = useState(false) const selectUserRef = useRef([]) const [ , update ] = useState({}) useEffect(() => { if (value === undefined) return setValue(Array.isArray(value) ? value : [ value ]) }, [ value ]) useEffect(() => { selectUserRef.current = [] innerValue?.forEach(id => { const item = users?.rows?.find(user => user.id === id) if (item) { selectUserRef.current = [ ...selectUserRef.current, item ] } }) update({}) }, [ innerValue ]) useEffect(() => { return () => { selectUserRef.current = [] } }, []) const renderTarget = () => { return setOpen(true)}> {children ?? } } const renderValue = () => { const dom = selectUserRef.current?.map(user => { return { const newVal = innerValue?.filter(val => val !== user.id) setValue(newVal) onChange?.(newVal) }}>{user.name} }) if (props.renderValue) { return props.renderValue(selectUserRef.current, dom) } return dom } return ( <>
{renderTarget()}
{renderValue()}
{ onChange?.(multiple ? innerValue : innerValue?.[0]) setOpen(false) }} onCancel={() => { setOpen(false) }} > { setSearch({ key: value, }) }} placeholder={t('component.UserPicker.placeholder', '输入成员姓名,回车查询')}/> { setSearch({ dept_id: keys?.[0] }) }} /> { setValue(val) }} dataSource={users?.rows ?? []} />
{t('component.UserPicker.selected', '已选({{count}})', { count: selectUserRef.current?.length ?? 0 })}
{ selectUserRef.current?.map(user => { return ( { setValue(innerValue?.filter(id => id !== user.id)) }} key={user.id} user={user!}/>) }) }
) }) const UserPicker = memo(({ type = 'modal', ...props }: UserPickerProps) => { return type === 'modal' ? : }) export default UserPicker