// import React, { useState } from 'react'; // import { Form, Select, Input } from 'antd'; // import axios from 'axios'; // const { Option } = Select; // // 假数据 // const fakePostsData = Array.from({ length: 15 }, (_, index) => ({ // id: index + 1, // type: 'Lecture', // name: `Lecture ${index + 1}`, // description: `This is lecture number ${index + 1}`, // })); // // 模拟获取数据的函数 // async function fetchPosts(query = '') { // // 在实际应用中,这里应该是一个真实的API调用 // return fakePostsData.filter(post => // post.name.toLowerCase().includes(query.toLowerCase()) || // post.description.toLowerCase().includes(query.toLowerCase()) // ); // } // const PostSelector = ({ value, onChange }) => { // const [posts, setPosts] = useState([]); // const [searchValue, setSearchValue] = useState(''); // const handleSearch = async (query) => { // setSearchValue(query); // const result = await fetchPosts(query); // setPosts(result); // }; // const handlePostChange = (selectedIds) => { // onChange(selectedIds); // 更新父组件的状态 // }; // const renderOption = (post) => ( // // ); // return ( // // ); // }; // const PostForm = () => { // const [form] = Form.useForm(); // const onFinish = (values) => { // console.log('Received values of form: ', values); // }; // return ( //
// // form.setFieldsValue({ postIds: selectedIds })} // /> // // // // //
// ); // }; // export default PostForm; import { api } from "@nice/client"; import { Button, Select } from "antd"; import { Lecture, lectureDetailSelect, postDetailSelect, postUnDetailSelect, Prisma, } from "@nice/common"; import { useMemo, useState } from "react"; import PostSelectOption from "./PostSelectOption"; import { DefaultArgs } from "@prisma/client/runtime/library"; import { safeOR } from "@nice/utils"; export default function PostSelect({ value, onChange, placeholder = "请选择课时", params = { where: {}, select: {} }, className, }: { value?: string | string[]; onChange?: (value: string | string[]) => void; placeholder?: string; params?: { where?: Prisma.PostWhereInput; select?: Prisma.PostSelect; }; className?: string; }) { const [searchValue, setSearch] = useState(""); const searchCondition: Prisma.PostWhereInput = useMemo(() => { const containTextCondition: Prisma.StringNullableFilter = { contains: searchValue, mode: "insensitive" as Prisma.QueryMode, // 使用类型断言 }; return searchValue ? { OR: [ { title: containTextCondition }, { content: containTextCondition }, ], } : {}; }, [searchValue]); // 核心条件生成逻辑 const idCondition: Prisma.PostWhereInput = useMemo(() => { if (value === undefined) return {}; // 无值时返回空对象 // 字符串类型增强判断 if (typeof value === "string") { // 如果明确需要支持逗号分隔字符串 return { id: value }; } if (Array.isArray(value)) { return value.length > 0 ? { id: { in: value } } : {}; // 空数组不注入条件 } return {}; }, [value]); const { data: lectures, isLoading, }: { data: Lecture[]; isLoading: boolean } = api.post.findMany.useQuery({ where: safeOR([ { ...idCondition }, { ...searchCondition, ...(params?.where || {}) }, ]), select: { ...postDetailSelect, ...(params?.select || {}) }, take: 15, }); const options = useMemo(() => { return (lectures || []).map((lecture, index) => { return { value: lecture.id, label: , tag: lecture?.title, }; }); }, [lectures, isLoading]); const tagRender = (props) => { // 根据 value 找到对应的 option const option = options.find((opt) => opt.value === props.value); // 使用自定义的展示内容(这里假设你的 option 中有 customDisplay 字段) return {option?.tag}; }; return (
); }