Skip to content

Commit 0d664d8

Browse files
committed
bugfix: added verification for the column type being passed, before, it was checking if the value could be converted to number even if it was a number surrounded by quotes, this would not work because the developer could intentionally pass a string to represent a number so the conversion would fail and not filter properly, now it only converts if it’s necessary for MRT to find the corresponding original values if they were not transformed in accessorFn
1 parent 1a35c3f commit 0d664d8

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/package/checkbox/autocomplete-checkbox.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { Clear as ClearIcon } from "@mui/icons-material";
1414

1515
import { checkedIcon, icon } from "./constants";
1616
import { filterFalsy, sortOptions } from "../utils";
17-
import { TAutocompleteCheckboxProps, TRenderOptions } from "../types";
17+
import {
18+
TAutocompleteCheckboxProps,
19+
TColumnType,
20+
TRenderOptions,
21+
} from "../types";
1822
import { CUSTOM_EVENT_CLEAN_AUTOCOMPLETE } from "../constants";
1923

2024
const AutocompleteCheckboxInner = <T extends Record<string, any>>(
@@ -39,12 +43,15 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
3943

4044
const [filters, setFilters] = useState<string[]>([]);
4145
const [autocompleteOpen, setAutocompleteOpen] = useState(false);
46+
const [columnType, setColumnType] = useState<TColumnType>(null);
4247

4348
const autocompleteOptions = useMemo(() => {
4449
const rowsObject = header?.getContext()?.table.getFilteredRowModel();
4550

4651
const noFalsyValues = filterFalsy(options);
4752

53+
setColumnType(typeof noFalsyValues[0]);
54+
4855
const uniqueOptions = [
4956
...new Set(noFalsyValues.map((option) => String(option))),
5057
];
@@ -112,6 +119,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
112119
if (customEvent.detail.message === CUSTOM_EVENT_CLEAN_AUTOCOMPLETE) {
113120
setFilters([]);
114121
setAutocompleteOpen(false);
122+
setColumnType(null);
115123
}
116124
};
117125

@@ -149,9 +157,13 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
149157
const handleApplyFilter = <T extends Record<string, any>>(
150158
header: MRT_Header<T>
151159
) => {
152-
const handlingNumberValue = filters.map((x) =>
153-
Number.isFinite(+x) ? Number(x) : x
154-
);
160+
const handlingNumberValue = filters.map((x) => {
161+
const isNumber = columnType === "number";
162+
163+
if (isNumber) return Number(x);
164+
165+
return x;
166+
});
155167

156168
const newFilterValueState =
157169
handlingNumberValue.length > 0 ? handlingNumberValue : undefined;

src/package/types/column-type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type TColumnType = string | number | boolean | null;

src/package/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./autocomplete-checkbox-props";
22
export * from "./render-options";
33
export * from "./sort-exclusive";
4+
export * from "./column-type";

0 commit comments

Comments
 (0)