Skip to content

Commit

Permalink
Fix selector
Browse files Browse the repository at this point in the history
  • Loading branch information
cregourd committed Jul 1, 2024
1 parent eaa2a9f commit 8b50415
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import clsx from "clsx";
import DoubleArrow from "../../../assets/icons/DoubleArrow";
import { useForm } from "../../../context/FormContext";
import { useI18n } from "../../../context/I18nContext";
import useCloseOnOutsideClick from "../../../hooks/useCloseOnOutsideClick";
import { Enumeration, Field, ModelName } from "../../../types";
import Button from "../../radix/Button";
import { Selector } from "../Selector";
Expand All @@ -24,9 +23,6 @@ type Props = {
const MultiSelectWidget = (props: Props) => {
const formContext = useForm();
const { formData, onChange, options, name, schema } = props;
const containerRef = useCloseOnOutsideClick<HTMLDivElement>(() =>
formContext.setOpen(false, name)
);
const { t } = useI18n();
const fieldOptions =
formContext.options?.model?.[formContext.resource!]?.edit?.fields?.[
Expand Down Expand Up @@ -67,6 +63,9 @@ const MultiSelectWidget = (props: Props) => {
formContext.toggleOpen(name);
}
}}
onClick={(e) => {
e.stopPropagation();
}}
>
{!(props.required && selectedValues.length === 0) && (
<option value={JSON.stringify(selectedValues)} />
Expand All @@ -75,7 +74,7 @@ const MultiSelectWidget = (props: Props) => {
);

return (
<div className="relative" ref={containerRef}>
<div className="relative">
{displayMode === "select" && (
<div
className={clsx(
Expand Down
20 changes: 14 additions & 6 deletions packages/next-admin/src/components/inputs/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useMemo } from "react";
import DoubleArrow from "../../assets/icons/DoubleArrow";
import { useConfig } from "../../context/ConfigContext";
import { useForm } from "../../context/FormContext";
import useCloseOnOutsideClick from "../../hooks/useCloseOnOutsideClick";
import { Enumeration } from "../../types";
import { slugify } from "../../utils/tools";
import { Selector } from "./Selector";
Expand All @@ -28,9 +27,6 @@ const SelectWidget = ({
const enumOptions = options.enumOptions?.map(
(option: any) => option.value as Enumeration
);
const containerRef = useCloseOnOutsideClick<HTMLDivElement>(() => {
formContext.setOpen(false, name);
});

const { basePath } = useConfig();

Expand All @@ -44,7 +40,7 @@ const SelectWidget = ({
}, [value]);

return (
<div className="relative" ref={containerRef}>
<div className="relative">
<div
className={clsx(
"ring-nextadmin-border-default dark:ring-dark-nextadmin-border-strong dark:bg-dark-nextadmin-background-subtle flex w-full cursor-default justify-between rounded-md px-3 py-2 text-sm placeholder-gray-500 shadow-sm ring-1",
Expand All @@ -65,6 +61,9 @@ const SelectWidget = ({
formContext.toggleOpen(name);
}
}}
onClick={(e) => {
e.stopPropagation();
}}
>
<option value={value?.value} />
</select>
Expand Down Expand Up @@ -97,7 +96,16 @@ const SelectWidget = ({
</button>
)}
{!disabled && (
<div className="flex items-center">
<div
className="flex items-center"
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
if (!disabled) {
formContext.toggleOpen(name);
}
}}
>
<DoubleArrow />
</div>
)}
Expand Down
8 changes: 7 additions & 1 deletion packages/next-admin/src/components/inputs/Selector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Transition } from "@headlessui/react";
import debounce from "lodash/debounce";
import { ChangeEvent, createRef, useEffect, useRef, useState } from "react";
import { useForm } from "../../context/FormContext";
import { useI18n } from "../../context/I18nContext";
import useCloseOnOutsideClick from "../../hooks/useCloseOnOutsideClick";
import useSearchPaginatedResource from "../../hooks/useSearchPaginatedResource";
import { Enumeration } from "../../types";
import LoaderRow from "../LoaderRow";
Expand All @@ -21,6 +23,11 @@ export const Selector = ({
options,
selectedOptions,
}: SelectorProps) => {
const formContext = useForm();
const containerRef = useCloseOnOutsideClick<HTMLDivElement>(() =>
formContext.setOpen(false, name)
);

const currentQuery = useRef("");
const searchInput = createRef<HTMLInputElement>();
const { t } = useI18n();
Expand Down Expand Up @@ -76,7 +83,6 @@ export const Selector = ({
currentQuery.current = e.target.value;
runSearch(currentQuery.current, true);
}, 300);
const containerRef = useRef<HTMLDivElement>(null);

const onScroll = () => {
// No need to do an infinite scroll for enums
Expand Down

0 comments on commit 8b50415

Please sign in to comment.