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

59 lines
1.4 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";
import { styles } from "./styles";
import { Options } from "./types";
import CreatableSelect from "react-select/creatable";
2023-03-28 02:31:50 -05:00
type Props = {
onChange: any;
showDefaultValue?: boolean;
2024-02-10 16:23:59 -06:00
defaultValue?:
| {
label: string;
value?: number;
}
| undefined;
2023-03-28 02:31:50 -05:00
};
export default function CollectionSelection({ onChange, defaultValue, showDefaultValue = true }: 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 (
<CreatableSelect
2023-11-28 13:24:52 -06:00
isClearable={false}
2023-08-14 22:25:25 -05:00
className="react-select-container"
classNamePrefix="react-select"
onChange={onChange}
options={options}
styles={styles}
defaultValue={showDefaultValue ? defaultValue : null}
// menuPosition="fixed"
/>
);
}