Skip to content

Commit 72f2c26

Browse files
Merge pull request #12 from felipehimself/refactor/improving-performance
Refactor
2 parents e841602 + 6488283 commit 72f2c26

File tree

5 files changed

+19
-27
lines changed

5 files changed

+19
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const App = () => {
143143

144144
## ⚡Performance Tips
145145

146-
The `AutocompleteCheckbox` component is memoized using React.memo. Although it's not mandatory, to benefit from this, make sure to wrap the `options` prop in a useMemo to prevent unnecessary re-renders.
146+
The `AutocompleteCheckbox` component is memoized using React.memo. Although it's not mandatory, to benefit from this, you would have to wrap the `options` prop in a useMemo to prevent unnecessary re-renders.
147147

148148

149149
## ♻️ Reusability

src/package/checkbox/autocomplete-checkbox.tsx

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

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

2420
const AutocompleteCheckboxInner = <T extends Record<string, any>>(
@@ -27,7 +23,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
2723
const {
2824
placeHolder = "",
2925
filterButtonText = "Filter",
30-
cleanButtonText = "Clear",
26+
cleanButtonText = "Clean",
3127
noOptionsText = "No options",
3228
textFieldProps = {},
3329
checkboxProps = {},
@@ -43,14 +39,13 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
4339

4440
const [filters, setFilters] = useState<string[]>([]);
4541
const [autocompleteOpen, setAutocompleteOpen] = useState(false);
46-
const [columnType, setColumnType] = useState<TColumnType>(null);
4742

48-
const autocompleteOptions = useMemo(() => {
43+
const autocompleteOptionsAndType = useMemo(() => {
4944
const rowsObject = header?.getContext()?.table.getFilteredRowModel();
5045

5146
const noFalsyValues = filterFalsy(options);
5247

53-
setColumnType(typeof noFalsyValues[0]);
48+
const optionType = typeof noFalsyValues[0];
5449

5550
const uniqueOptions = [
5651
...new Set(noFalsyValues.map((option) => String(option))),
@@ -79,7 +74,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
7974
});
8075
});
8176

82-
return optionsMapped;
77+
return { options: optionsMapped, optionsType: optionType };
8378
}, [options, header, sortAscending, sortDescending]);
8479

8580
useEffect(() => {
@@ -119,7 +114,6 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
119114
if (customEvent.detail.message === CUSTOM_EVENT_CLEAN_AUTOCOMPLETE) {
120115
setFilters([]);
121116
setAutocompleteOpen(false);
122-
setColumnType(null);
123117
}
124118
};
125119

@@ -157,12 +151,12 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
157151
const handleApplyFilter = <T extends Record<string, any>>(
158152
header: MRT_Header<T>
159153
) => {
160-
const handlingNumberValue = filters.map((x) => {
161-
const isNumber = columnType === "number";
154+
const handlingNumberValue = filters.map((filter) => {
155+
const isNumber = autocompleteOptionsAndType.optionsType === "number";
162156

163-
if (isNumber) return Number(x);
157+
if (isNumber) return Number(filter);
164158

165-
return x;
159+
return filter;
166160
});
167161

168162
const newFilterValueState =
@@ -183,7 +177,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
183177
});
184178
};
185179

186-
const handleClearColumn = () => {
180+
const handleCleanColumn = () => {
187181
header.column.setFilterValue(undefined);
188182
setFilters([]);
189183
setAutocompleteOpen(false);
@@ -195,7 +189,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
195189
}
196190
};
197191

198-
const handleClearCheckBoxesSelection = () => {
192+
const handleCleanCheckBoxesSelection = () => {
199193
setFilters([]);
200194
};
201195

@@ -241,21 +235,21 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
241235
multiple
242236
value={filters}
243237
disableCloseOnSelect
244-
options={autocompleteOptions}
238+
options={autocompleteOptionsAndType.options}
245239
open={autocompleteOpen}
246240
onOpen={handleOpenAutocomplete}
247241
onClose={handleCloseAutoComplete}
248242
onChange={(_e, _newValue, reason, details) => {
249243
if (reason === "removeOption") return removeFromFilter(details!.option);
250-
if (reason === "clear") return handleClearColumn();
244+
if (reason === "clear") return handleCleanColumn();
251245
if (reason === "selectOption")
252246
return handleSelectOptions(details!.option);
253247
}}
254248
getOptionLabel={(option) => option}
255249
renderOption={(props, option, { selected: _selected }) => {
256250
const { key: _key, ...rest } = props;
257251

258-
const curentIdx = (props as TRenderOptions)["data-option-index"];
252+
const currentIndex = (props as TRenderOption)["data-option-index"];
259253

260254
return (
261255
<>
@@ -268,7 +262,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
268262
/>
269263
{option}
270264
</li>
271-
{curentIdx === autocompleteOptions.length - 1 && (
265+
{currentIndex === autocompleteOptionsAndType.options.length - 1 && (
272266
<>
273267
<Divider />
274268
<Stack
@@ -280,7 +274,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
280274
>
281275
<Button
282276
{...cleanButtonProps}
283-
onClick={handleClearCheckBoxesSelection}
277+
onClick={handleCleanCheckBoxesSelection}
284278
variant={cleanButtonProps?.variant || "text"}
285279
color={cleanButtonProps?.color || "warning"}
286280
size={cleanButtonProps?.size || "small"}
@@ -318,7 +312,7 @@ const AutocompleteCheckboxInner = <T extends Record<string, any>>(
318312
endAdornment: (
319313
<IconButton
320314
sx={{ mr: "-1.875rem", mt: "-0.281rem" }}
321-
onClick={handleClearColumn}
315+
onClick={handleCleanColumn}
322316
>
323317
<ClearIcon fontSize="small" />
324318
</IconButton>

src/package/types/column-type.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/package/types/index.ts

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

src/package/types/render-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type TRenderOptions = {
1+
export type TRenderOption = {
22
key: string;
33
tabIndex: number;
44
role: string;

0 commit comments

Comments
 (0)