-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add new mui formik color picker component #231
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
base: main
Are you sure you want to change the base?
Changes from all commits
3f4737c
c31bbc0
30a7871
ab8685a
ae683f6
5241fff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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, | ||||||||||||||||||||||||
|
|
@@ -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([]); | ||||||||||||||||||||||||
|
|
@@ -58,7 +67,7 @@ const MuiFormikAsyncAutocomplete = ({ | |||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||
| if (searchTerm) { | ||||||||||||||||||||||||
| if (!localFilter && searchTerm) { | ||||||||||||||||||||||||
| const delayDebounce = setTimeout(() => { | ||||||||||||||||||||||||
| fetchOptions(searchTerm); | ||||||||||||||||||||||||
| }, DEBOUNCE_WAIT_250); | ||||||||||||||||||||||||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make local filtering null-safe for option labels. This path calls Suggested fix ? (options, { inputValue }) =>
options.filter((opt) =>
- opt.label.toLowerCase().includes(inputValue.toLowerCase())
+ (opt.label ?? "")
+ .toString()
+ .toLowerCase()
+ .includes((inputValue ?? "").toLowerCase())
)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| : undefined | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| renderInput={(params) => ( | ||||||||||||||||||||||||
| <TextField | ||||||||||||||||||||||||
| {...params} | ||||||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||||||
| 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"); | ||
|
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} | ||
|
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} | ||
| /> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ); | ||
| }; | ||
|
|
||
| MuiFormikColorInput.propTypes = { | ||
| name: PropTypes.string.isRequired | ||
| }; | ||
|
|
||
| export default MuiFormikColorInput; | ||
Uh oh!
There was an error while loading. Please reload this page.