collect-system/apps/web/src/components/models/role/role-select.tsx

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-09-10 10:31:24 +08:00
import { useState } from 'react';
import { Select, Spin } from 'antd';
import type { SelectProps } from 'antd';
2024-12-30 08:26:40 +08:00
import { api } from "@nicestack/client"
2024-09-10 10:31:24 +08:00
interface RoleSelectProps {
value?: string | string[];
onChange?: (value: string | string[]) => void;
style?: React.CSSProperties;
multiple?: boolean;
}
export default function RoleSelect({ value, onChange, style, multiple }: RoleSelectProps) {
const [keyword, setQuery] = useState<string>('');
2024-12-31 15:57:32 +08:00
const { data, isLoading } = api.role.findMany.useQuery({
where: {
OR: [
{ name: { contains: keyword } }
]
}
});
2024-09-10 10:31:24 +08:00
const handleSearch = (value: string) => {
setQuery(value);
};
const options: SelectProps['options'] = data?.map((role: any) => ({
value: role.id,
label: role.name,
})) || [];
return (
<Select
allowClear
showSearch
mode={multiple ? 'multiple' : undefined}
placeholder="请选择角色"
notFoundContent={isLoading ? <Spin size="small" /> : null}
filterOption={false}
onSearch={handleSearch}
options={options}
value={value}
onChange={onChange}
style={{ minWidth: 200, ...style }}
/>
);
}