el.xwx.moe/components/InputSelect/CollectionSelection.tsx

58 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-03-22 18:11:54 -05:00
import useCollectionStore from "@/store/collections";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
2023-06-24 16:54:35 -05:00
import Select from "react-select";
import { styles } from "./styles";
import { Options } from "./types";
2023-03-28 02:31:50 -05:00
type Props = {
onChange: any;
defaultValue:
| {
label: string;
2023-05-27 11:29:39 -05:00
value?: number;
2023-03-28 02:31:50 -05:00
}
| undefined;
};
export default function CollectionSelection({ onChange, defaultValue }: Props) {
2023-03-22 18:11:54 -05:00
const { collections } = useCollectionStore();
const router = useRouter();
const [options, setOptions] = useState<Options[]>([]);
const collectionId = Number(router.query.id);
const activeCollection = collections.find((e) => {
return e.id === collectionId;
});
2023-03-28 02:31:50 -05:00
if (activeCollection && !defaultValue) {
defaultValue = {
2023-02-24 22:22:33 -06:00
value: activeCollection?.id,
label: activeCollection?.name,
};
}
useEffect(() => {
const formatedCollections = collections.map((e) => {
2023-03-28 02:31:50 -05:00
return { value: e.id, label: e.name, ownerId: e.ownerId };
});
setOptions(formatedCollections);
}, [collections]);
return (
2023-06-24 16:54:35 -05:00
<Select
isClearable
2023-08-14 22:25:25 -05:00
className="react-select-container"
classNamePrefix="react-select"
onChange={onChange}
options={options}
styles={styles}
2023-03-28 02:31:50 -05:00
defaultValue={defaultValue}
2023-05-01 15:00:23 -05:00
// menuPosition="fixed"
/>
);
}