staff_data/apps/web/src/components/models/staff/staff-select.tsx

85 lines
1.7 KiB
TypeScript
Raw Normal View History

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;
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,
}: 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 },
take: 30,
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"] =
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
}