2024-12-30 08:26:40 +08:00
|
|
|
import { useMemo, useState } from "react";
|
|
|
|
import { Button, Select, Spin } from "antd";
|
|
|
|
import type { SelectProps } from "antd";
|
2025-01-06 08:45:23 +08:00
|
|
|
import { api } from "@nice/client";
|
2024-09-10 10:31:24 +08:00
|
|
|
interface StaffSelectProps {
|
2024-12-30 08:26:40 +08:00
|
|
|
value?: string | string[];
|
|
|
|
onChange?: (value: string | string[]) => void;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
multiple?: boolean;
|
|
|
|
domainId?: string;
|
|
|
|
placeholder?: string;
|
2025-03-12 10:02:41 +08:00
|
|
|
staffsMsg?: StaffsMsgProps[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StaffsMsgProps {
|
|
|
|
id: string;
|
|
|
|
showname: string;
|
|
|
|
username: string;
|
2024-09-10 10:31:24 +08:00
|
|
|
}
|
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
export default function StaffSelect({
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
placeholder,
|
|
|
|
style,
|
|
|
|
multiple,
|
|
|
|
domainId,
|
2025-03-12 10:02:41 +08:00
|
|
|
staffsMsg,
|
2024-12-30 08:26:40 +08:00
|
|
|
}: StaffSelectProps) {
|
|
|
|
const [keyword, setQuery] = useState<string>("");
|
|
|
|
|
|
|
|
// Determine ids based on whether value is an array or not
|
|
|
|
const ids = useMemo(() => {
|
|
|
|
return Array.isArray(value) ? value : [];
|
|
|
|
}, [value]);
|
2024-09-10 10:31:24 +08:00
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
// Adjust the query to include ids when they are present
|
|
|
|
const { data, isLoading } = api.staff.findMany.useQuery({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
username: {
|
|
|
|
contains: keyword,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
showname: {
|
|
|
|
contains: keyword,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
in: ids
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
domainId,
|
2024-09-10 10:31:24 +08:00
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
},
|
|
|
|
select: { id: true, showname: true, username: true },
|
2025-03-12 10:02:41 +08:00
|
|
|
//take: 30,
|
2024-12-30 08:26:40 +08:00
|
|
|
orderBy: { order: "asc" }
|
|
|
|
});
|
2024-09-10 10:31:24 +08:00
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
const handleSearch = (value: string) => {
|
|
|
|
setQuery(value);
|
|
|
|
};
|
2024-09-10 10:31:24 +08:00
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
const options: SelectProps["options"] =
|
2025-03-12 10:02:41 +08:00
|
|
|
staffsMsg ?
|
|
|
|
staffsMsg.map((staff)=>{
|
|
|
|
return {
|
|
|
|
value:staff.id,
|
|
|
|
label:staff.showname || staff.username
|
|
|
|
}
|
|
|
|
}) :
|
2024-12-30 08:26:40 +08:00
|
|
|
data?.map((staff: any) => ({
|
|
|
|
value: staff.id,
|
|
|
|
label: staff?.showname || staff?.username,
|
|
|
|
})) || [];
|
2024-09-10 10:31:24 +08:00
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Select
|
|
|
|
allowClear
|
|
|
|
showSearch
|
|
|
|
mode={multiple ? "multiple" : undefined}
|
|
|
|
placeholder={placeholder || "请选择人员"}
|
|
|
|
notFoundContent={isLoading ? <Spin size="small" /> : null}
|
|
|
|
filterOption={false}
|
|
|
|
onSearch={handleSearch}
|
|
|
|
options={options}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
style={{ minWidth: 200, ...style }}
|
|
|
|
/>{" "}
|
|
|
|
</>
|
|
|
|
);
|
2024-09-10 10:31:24 +08:00
|
|
|
}
|