Skip to content

Commit

Permalink
use isOptionEqualToValue method on autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
syedsalehinipg committed Nov 13, 2023
1 parent 1696ad6 commit 3878d79
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from "react";

import { AutocompleteProps, KeyValueOption } from "./Autocomplete.types";
import {
Box,
Checkbox,
Expand All @@ -10,6 +9,10 @@ import {
} from "@mui/material";
import { CheckBox, CheckBoxOutlineBlank } from "@mui/icons-material";

import { AutocompleteProps } from "./Autocomplete.types";
import { KeyValueOption } from "../Common.types";
import { isKeyValueOption } from "../utils/common";

export default function Autocomplete<
Value extends KeyValueOption | string,
Multiple extends boolean | undefined
Expand Down Expand Up @@ -55,6 +58,22 @@ export default function Autocomplete<
clearIcon={multiple ? null : undefined}
disabled={disabled}
size={size}
isOptionEqualToValue={(option, value) => {
// if the option is a key value pair, compare option.value to value
if (isKeyValueOption(option)) {
if (option.value === value) {
return true;
}
} else {
// if the option is not a key value then compare option to value
if (option === value) {
return true;
}
}

// if the option is not a match, return false
return false;
}}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Autocomplete/Autocomplete.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
TextFieldProps
} from "@mui/material";

export type KeyValueOption = { key: string | number; value: string };
import { KeyValueOption } from "../Common.types";

export type AutocompleteProps<
Value extends string | KeyValueOption,
Expand Down
3 changes: 3 additions & 0 deletions src/Common.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export type Label = {
description?: string;
name: string;
};

// KeyValueOption type
export type KeyValueOption = { key: string | number; value: string };
4 changes: 3 additions & 1 deletion src/LabelSelector/LabelSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ function LabelSelector({
}
noOptionsText="No labels found"
getOptionLabel={option => option.name}
isOptionEqualToValue={(option, value) => option._id === value._id}
isOptionEqualToValue={(option, value) => {
return option._id === value._id ?? false;
}}
value={value || null}
/>
<EditLabelDialog
Expand Down
6 changes: 6 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { KeyValueOption } from "../Common.types";

// This function is used to check if an object is of type KeyValueOption
export function isKeyValueOption<T>(obj: T): obj is T & KeyValueOption {
return obj && typeof obj === "object" && "key" in obj && "value" in obj;
}

0 comments on commit 3878d79

Please sign in to comment.