Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIMS-1582 Options Filter Fix #2329

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions react-app/src/components/form/AutocompleteFormField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import React from 'react';
import { Autocomplete, SxProps, TextField, Paper, Box, autocompleteClasses } from '@mui/material';
import {
Autocomplete,
SxProps,
TextField,
Paper,
Box,
autocompleteClasses,
FilterOptionsState,
} from '@mui/material';
import { ISelectMenuItem } from './SelectFormField';
import { Controller, useFormContext } from 'react-hook-form';

Expand All @@ -13,6 +21,10 @@ type AutocompleteFormProps = {
disableOptionsFunction?: (option: ISelectMenuItem) => boolean;
disableClearable?: boolean;
defaultValue?: ISelectMenuItem | null;
customOptionsFilter?: (
options: ISelectMenuItem[],
state: FilterOptionsState<ISelectMenuItem>,
) => ISelectMenuItem[];
};

const CustomPaper = (props) => {
Expand All @@ -30,9 +42,17 @@ const AutocompleteFormField = (props: AutocompleteFormProps) => {
allowNestedIndent,
disableClearable,
disableOptionsFunction,
customOptionsFilter,
...rest
} = props;

const defaultOptionsFilter = (
options: ISelectMenuItem[],
state: FilterOptionsState<ISelectMenuItem>,
) => options.filter((item) => item.label.toLowerCase().includes(state.inputValue.toLowerCase()));

const optionsFilter = customOptionsFilter ? customOptionsFilter : defaultOptionsFilter;

return (
<Controller
name={name}
Expand All @@ -48,7 +68,7 @@ const AutocompleteFormField = (props: AutocompleteFormProps) => {
disableClearable={disableClearable}
getOptionLabel={(option: ISelectMenuItem) => option.label}
getOptionDisabled={disableOptionsFunction}
filterOptions={(x) => x}
filterOptions={optionsFilter}
renderOption={(props, option, state, ownerState) => (
<Box
sx={{
Expand All @@ -74,7 +94,7 @@ const AutocompleteFormField = (props: AutocompleteFormProps) => {
/>
)}
onChange={(_, data) => {
onChange(data.value);
if (data) onChange(data.value);
}}
isOptionEqualToValue={(option, value) => option.value === value.value}
value={options.find((option) => option.value === getValues()[name]) ?? null}
Expand Down
9 changes: 3 additions & 6 deletions react-app/src/hooks/api/useGroupedAgenciesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ export const useGroupedAgenciesApi = () => {
children: agency.children.map((child) => child.Id),
});
if (agency.children && agency.children.length > 0) {
// Custom sorting logic for children agencies that contain numeric values
agency.children = agency.children.sort((a: Agency, b: Agency) => {
const numA = parseInt(a.Name.match(/\d+/)?.[0] || '0');
const numB = parseInt(b.Name.match(/\d+/)?.[0] || '0');
return numA - numB;
});
agency.children = agency.children.sort((a: Agency, b: Agency) =>
a.Name.localeCompare(b.Name, undefined, { numeric: true, sensitivity: 'base' }),
);
agency.children.forEach((childAgency) => {
options.push({
label: childAgency.Name,
Expand Down
Loading