From f84c21f829b5170d4e92930d34ab0be61ba01131 Mon Sep 17 00:00:00 2001 From: AmirReza Jamali Date: Mon, 13 Oct 2025 12:26:56 +0330 Subject: [PATCH] feat(form): Add clearable functionality to Select component This commit introduces a new `clearable` prop to the `Select` component, allowing users to easily reset its value. When the `clearable` prop is set to true, a clear button (an 'X' icon) is displayed when an option is selected. Clicking this button resets the select input to its initial placeholder state and triggers an optional `onClear` callback. To support this, the component's internal state management was refactored to control the input's value. Additionally, the `AdminListFilters` component has been updated to: - Utilize the new clearable select for the status filter. - Consolidate multiple search fields (email, username, full_name) into a single generic "search" input. - Add explicit "Reset" and "Apply" buttons for better filter management. --- src/components/FormElements/select.tsx | 39 +++- .../Tables/admins/AdminListFilters.tsx | 86 ++++++--- src/components/Tables/admins/index.tsx | 167 ++++++++++-------- .../admins/useAdminListFilterPresenter.ts | 10 -- .../Tables/admins/useAdminsPresenter.ts | 32 +++- src/hooks/queries/index.ts | 16 +- src/hooks/queries/useAdminListQuery.ts | 11 ++ src/utils/shared.ts | 12 ++ 8 files changed, 257 insertions(+), 116 deletions(-) delete mode 100644 src/components/Tables/admins/useAdminListFilterPresenter.ts create mode 100644 src/hooks/queries/useAdminListQuery.ts diff --git a/src/components/FormElements/select.tsx b/src/components/FormElements/select.tsx index 22f482f..f3a3ddc 100644 --- a/src/components/FormElements/select.tsx +++ b/src/components/FormElements/select.tsx @@ -22,6 +22,8 @@ type PropsType = { register?: UseFormRegister; errors?: FieldErrors; required?: boolean; + clearable?: boolean; + onClear?: () => void; } & ( | { placeholder?: string; defaultValue: string } | { placeholder: string; defaultValue?: string } @@ -42,13 +44,22 @@ export function Select({ onChange, onBlur, ref, + clearable = false, + onClear, ...props }: PropsType) { const id = useId(); const [isOptionSelected, setIsOptionSelected] = useState(!!defaultValue); + const [value, setValue] = useState(defaultValue || ""); const error = errors && errors[name]; + const handleClear = () => { + setValue(""); + setIsOptionSelected(false); + onClear?.(); + }; + return (
diff --git a/src/components/Tables/admins/AdminListFilters.tsx b/src/components/Tables/admins/AdminListFilters.tsx index edd3f84..c871356 100644 --- a/src/components/Tables/admins/AdminListFilters.tsx +++ b/src/components/Tables/admins/AdminListFilters.tsx @@ -1,57 +1,95 @@ import InputGroup from "@/components/FormElements/InputGroup"; import { Select } from "@/components/FormElements/select"; -import { AdminRoles, NotificationChannels } from "@/constants/enums"; +import { AdminRoles, AdminStatus } from "@/constants/enums"; import { getEnumAsArray } from "@/utils/shared"; -import useAdminListFilterPresenter from "./useAdminListFilterPresenter"; +import { + FieldValues, + UseFormHandleSubmit, + UseFormRegister, +} from "react-hook-form"; -interface IProps {} +interface AdminListFiltersProps { + filterRegister: UseFormRegister; + setIsFilterModalOpen: (isOpen: boolean) => void; + isMutating: number; + handleFilterSubmit: UseFormHandleSubmit; + search: (data: any) => void; +} -const AdminListFilters = ({}: IProps) => { - const { register } = useAdminListFilterPresenter(); +const AdminListFilters = ({ + filterRegister, + setIsFilterModalOpen, + isMutating, + handleFilterSubmit, + search, +}: AdminListFiltersProps) => { return ( -
+ + {/* + /> */}