Skip to content
Open
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
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export {TotalRow as MuiTotalRow, NotesRow as MuiNotesRow} from './mui/table/extr
export {default as MuiFormikAsyncSelect} from './mui/formik-inputs/mui-formik-async-select'
export {default as MuiFormikCheckboxGroup} from './mui/formik-inputs/mui-formik-checkbox-group'
export {default as MuiFormikCheckbox} from './mui/formik-inputs/mui-formik-checkbox'
export {default as MuiFormikColorInput} from './mui/formik-inputs/mui-formik-color-input'
export {default as MuiFormikDatepicker} from './mui/formik-inputs/mui-formik-datepicker'
export {default as MuiFormikDiscountField} from './mui/formik-inputs/mui-formik-discountfield'
export {default as MuiFormikDropdownCheckbox} from './mui/formik-inputs/mui-formik-dropdown-checkbox'
Expand Down
32 changes: 29 additions & 3 deletions src/components/mui/formik-inputs/mui-formik-async-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ import {
} from "@mui/material";
import { useField } from "formik";
import { DEBOUNCE_WAIT_250 } from "../../../utils/constants";
import PropTypes from "prop-types";

/**
* Async Autocomplete with two modes:
* - Remote (default): fetches options from API on each user input (debounced).
* - Local (localFilter=true): fetches once on mount and filters options client-side.
* Note: localFilter mode assumes stable queryParams (set once on mount).
* If queryParams need to change, remount the component instead.
*/
const MuiFormikAsyncAutocomplete = ({
name,
queryFunction,
Expand All @@ -31,7 +39,8 @@ const MuiFormikAsyncAutocomplete = ({
formatOption = (item) => ({ value: item.id.toString(), label: item.name }),
formatSelectedValue = null,
queryParams = [],
isMulti = false
isMulti = false,
localFilter = false
}) => {
const [field, meta, helpers] = useField(name);
const [options, setOptions] = useState([]);
Expand All @@ -58,7 +67,7 @@ const MuiFormikAsyncAutocomplete = ({
};

useEffect(() => {
Comment thread
tomrndom marked this conversation as resolved.
if (searchTerm) {
if (!localFilter && searchTerm) {
const delayDebounce = setTimeout(() => {
fetchOptions(searchTerm);
}, DEBOUNCE_WAIT_250);
Expand Down Expand Up @@ -99,7 +108,16 @@ const MuiFormikAsyncAutocomplete = ({
fullWidth
getOptionLabel={(option) => option.label || ""}
isOptionEqualToValue={(option, value) => option.value === value.value}
onInputChange={(e, newInput) => setSearchTerm(newInput)}
onInputChange={!localFilter ? (e, newInput) => setSearchTerm(newInput) : undefined}
filterOptions={
// only apply filterOptions for "local" search
localFilter
? (options, { inputValue }) =>
options.filter((opt) =>
opt.label.toLowerCase().includes(inputValue.toLowerCase())
)
Comment on lines +115 to +118
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Make local filtering null-safe for option labels.

This path calls toLowerCase() directly on opt.label; a non-string/empty label will throw at runtime.

Suggested fix
           ? (options, { inputValue }) =>
             options.filter((opt) =>
-              opt.label.toLowerCase().includes(inputValue.toLowerCase())
+              (opt.label ?? "")
+                .toString()
+                .toLowerCase()
+                .includes((inputValue ?? "").toLowerCase())
             )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
? (options, { inputValue }) =>
options.filter((opt) =>
opt.label.toLowerCase().includes(inputValue.toLowerCase())
)
? (options, { inputValue }) =>
options.filter((opt) =>
(opt.label ?? "")
.toString()
.toLowerCase()
.includes((inputValue ?? "").toLowerCase())
)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/components/mui/formik-inputs/mui-formik-async-select.js` around lines 107
- 110, The local filtering callback currently calls opt.label.toLowerCase()
which will throw if opt.label is null/undefined/non-string; update the filter
used in options.filter((opt) => ...) to coerce or guard the label and inputValue
(e.g., use String(opt.label ?? '') or (opt.label || '').toString()) and
similarly normalize inputValue before calling toLowerCase(), then perform the
includes check; ensure you update the inline filter expression where
options.filter is defined to be null-safe for labels.

: undefined
}
renderInput={(params) => (
<TextField
{...params}
Expand Down Expand Up @@ -137,4 +155,12 @@ const MuiFormikAsyncAutocomplete = ({
);
};

MuiFormikAsyncAutocomplete.propTypes = {
name: PropTypes.string.isRequired,
queryFunction: PropTypes.func.isRequired,
formatOption: PropTypes.func,
queryParams: PropTypes.array,
localFilter: PropTypes.bool,
};

export default MuiFormikAsyncAutocomplete;
56 changes: 56 additions & 0 deletions src/components/mui/formik-inputs/mui-formik-color-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { useRef, useState } from "react";
import PropTypes from "prop-types";
import { TextField } from "@mui/material";
import { useField } from "formik";
import { DEBOUNCE_WAIT_150 } from "../../../utils/constants";

const MuiFormikColorInput = ({ name, ...rest }) => {
const [field, meta, helpers] = useField(name);
const [localValue, setLocalValue] = useState(field.value || "#000000");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const debounceRef = useRef(null);

const handleChange = (e) => {
const value = e.target.value;
setLocalValue(value);
if (debounceRef.current) clearTimeout(debounceRef.current);
debounceRef.current = setTimeout(() => {
helpers.setValue(value);
debounceRef.current = null;
}, DEBOUNCE_WAIT_150);
};

const handleBlur = (e) => {
field.onBlur(e);
helpers.setTouched(true);
if (debounceRef.current) {
clearTimeout(debounceRef.current);
debounceRef.current = null;
helpers.setValue(localValue);
}
};

return (
<TextField
type="color"
name={field.name}
value={localValue}
onChange={handleChange}
onBlur={handleBlur}
Comment thread
tomrndom marked this conversation as resolved.
error={meta.touched && Boolean(meta.error)}
helperText={meta.touched && meta.error}
fullWidth
sx={{
"& input[type='color']::-webkit-color-swatch-wrapper": {
padding: "2px"
}
}}
{...rest}
/>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
);
};

MuiFormikColorInput.propTypes = {
name: PropTypes.string.isRequired
};

export default MuiFormikColorInput;
1 change: 1 addition & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const ZERO_INT = 0;

export const CODE_200 = 200;

export const DEBOUNCE_WAIT_150 = 150;
export const DEBOUNCE_WAIT_250 = 250;
export const DEBOUNCE_WAIT = 500;

Expand Down
1 change: 1 addition & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ module.exports = {
'components/mui/formik-inputs/async-select': './src/components/mui/formik-inputs/mui-formik-async-select.js',
'components/mui/formik-inputs/checkbox-group': './src/components/mui/formik-inputs/mui-formik-checkbox-group.js',
'components/mui/formik-inputs/checkbox': './src/components/mui/formik-inputs/mui-formik-checkbox.js',
'components/mui/formik-inputs/color-input': './src/components/mui/formik-inputs/mui-formik-color-input.js',
'components/mui/formik-inputs/datepicker': './src/components/mui/formik-inputs/mui-formik-datepicker.js',
'components/mui/formik-inputs/discount-field': './src/components/mui/formik-inputs/mui-formik-discountfield.js',
'components/mui/formik-inputs/dropdown-checkbox': './src/components/mui/formik-inputs/mui-formik-dropdown-checkbox.js',
Expand Down
Loading