diff --git a/packages/next-admin/src/components/inputs/SelectWidget.tsx b/packages/next-admin/src/components/inputs/SelectWidget.tsx index e67b3829..29409077 100644 --- a/packages/next-admin/src/components/inputs/SelectWidget.tsx +++ b/packages/next-admin/src/components/inputs/SelectWidget.tsx @@ -8,11 +8,9 @@ import * as Select from "@radix-ui/react-select"; import { WidgetProps } from "@rjsf/utils"; import clsx from "clsx"; import Link from "next/link"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import DoubleArrow from "../../assets/icons/DoubleArrow"; import { useConfig } from "../../context/ConfigContext"; -import useClickOutside from "../../hooks/useCloseOnOutsideClick"; -import { useDisclosure } from "../../hooks/useDisclosure"; import { Enumeration } from "../../types"; import { slugify } from "../../utils/tools"; import { Selector } from "./Selector"; @@ -32,112 +30,99 @@ const SelectWidget = ({ placeholder, schema }: SelectWidgetProps) => { - const { isOpen, onToggle, onClose } = useDisclosure(); - const containerRef = useClickOutside(() => { - onClose(); - }); const [enumOptions, setEnumOptions] = useState( options?.enumOptions || [] ); - const [selectedOption, setSelectedOption] = useState(() => { + const addEnumOptions = useCallback((options: Enumeration[]) => { + setEnumOptions((prev) => [ + ...prev, + ...options.filter((option) => !prev.some((p) => p.value === option.value)) + ]); + }, []); + + const [selectedOption, setSelectedOption] = useState(); + + + useEffect(() => { if (typeof value === "string") { - return enumOptions.find((option) => option.value === value) || null; + setSelectedOption(enumOptions.find((option) => String(option.value) === value) || null); + } else { + setSelectedOption(value || null); } - return value || null; - }); + }, []); - const { basePath } = useConfig(); + useEffect(() => { + console.log("selectedOption", name, selectedOption); + }, [selectedOption]); - const handleChange = useCallback( - (value: Enumeration | null) => { - if (!enumOptions.some((option) => option.value === value?.value)) { - value && setEnumOptions((prev) => [...(prev ?? []), value]); - } - setSelectedOption(value); - onChange(value?.value); - onClose(); - }, - [onChange, onClose] - ); + const { basePath } = useConfig(); const hasValue = useMemo(() => { return Object.keys(selectedOption || {}).length > 0; }, [selectedOption]); return ( -
- { - const selectedEnum = enumOptions.find(option => option.value === value); - if (selectedEnum) { - handleChange(selectedEnum); - } - }} - open={isOpen} - onOpenChange={(open) => { - if (open) { - onToggle(); - } else { - onClose(); - } - }} - value={selectedOption?.value} + { + console.log("value", value); + const option = enumOptions.find((option) => String(option.value) === value); + console.log("option", option); + setSelectedOption(option || null); + }} + > + - - - {selectedOption?.label} - -
- {hasValue && schema.relation && ( - - - - - - )} - {hasValue && !disabled && ( - { - e.preventDefault(); - handleChange(null); - }} - > - - - )} - {!disabled && ( - + {selectedOption?.label} + +
+ {hasValue && schema.relation && ( + + - - - )} -
- - - -
+ + + + )} + {hasValue && !disabled && ( + { + e.preventDefault(); + onChange(null); + }} + > + + + )} + {!disabled && ( + + + + )} +
+ + + ); }; diff --git a/packages/next-admin/src/components/inputs/Selector.tsx b/packages/next-admin/src/components/inputs/Selector.tsx index a0565be4..aa861129 100644 --- a/packages/next-admin/src/components/inputs/Selector.tsx +++ b/packages/next-admin/src/components/inputs/Selector.tsx @@ -15,13 +15,13 @@ import LoaderRow from "../LoaderRow"; export type SelectorProps = { open?: boolean; name: string; - onChange: (value: Enumeration | null) => void; options?: Enumeration[]; selectedOptions?: Enumeration[]; + setEnumOptions: (options: Enumeration[]) => void; }; export const Selector = forwardRef( - ({ open, name, onChange, options, selectedOptions }, ref) => { + ({ open, name, options, selectedOptions, setEnumOptions }, ref) => { const currentQuery = useRef(""); const searchInput = useRef(null); const { t } = useI18n(); @@ -36,6 +36,11 @@ export const Selector = forwardRef( fieldName: name, initialOptions: options, }); + + useEffect(() => { + setEnumOptions(allOptions); + }, [allOptions]); + const [optionsLeft, setOptionsLeft] = useState(() => { return allOptions.filter( (item) => !selectedOptions?.some((option) => option.value === item.value) @@ -108,51 +113,52 @@ export const Selector = forwardRef( }; return ( - - -
-
-
- + + + +
+
+
+ +
+ {optionsLeft && + optionsLeft.length > 0 && + optionsLeft?.map((option, index: number) => ( + + {option.label} + + ))} + {isPending && } + {optionsLeft && optionsLeft.length === 0 && !isPending && ( +
+ No results found +
+ )}
- {optionsLeft && - optionsLeft.length > 0 && - optionsLeft?.map((option, index: number) => ( - { - event.preventDefault(); - onChange(option); - }} - > - {option.label} - - ))} - {isPending && } - {optionsLeft && optionsLeft.length === 0 && !isPending && ( -
- No results found -
- )} -
- - + + + ); } ); \ No newline at end of file diff --git a/packages/next-admin/src/types.ts b/packages/next-admin/src/types.ts index adf957fc..0ae59861 100644 --- a/packages/next-admin/src/types.ts +++ b/packages/next-admin/src/types.ts @@ -813,7 +813,7 @@ export type Select = { export type Enumeration = { label: string; - value: string; + value: string | number; data?: any; };