-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/grid filter continued #269
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
47e69af
df4fa23
15b581c
7ff283b
75760ee
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 |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * */ | ||
|
|
||
| import React, { useEffect, useRef, useState } from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import T from "i18n-react/dist/i18n-react"; | ||
| import Autocomplete from "@mui/material/Autocomplete"; | ||
| import TextField from "@mui/material/TextField"; | ||
| import CircularProgress from "@mui/material/CircularProgress"; | ||
| import { DEBOUNCE_WAIT_250 } from "../../../../../utils/constants"; | ||
|
|
||
| const defaultFormatOption = (item) => ({ | ||
| value: item.id, | ||
| label: item.name | ||
| }); | ||
|
|
||
| const optionShape = PropTypes.shape({ | ||
| value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
| label: PropTypes.string, | ||
| raw: PropTypes.object | ||
| }); | ||
|
|
||
| const AsyncSelectInput = ({ | ||
| id, | ||
| value, | ||
| label, | ||
| placeholder, | ||
| disabled, | ||
| multiple, | ||
| queryFunction, | ||
| formatOption, | ||
| debounceWait, | ||
| minSearchLength, | ||
| onChange, | ||
| ...rest | ||
| }) => { | ||
| const [options, setOptions] = useState([]); | ||
| const [loading, setLoading] = useState(false); | ||
| const debounceRef = useRef(null); | ||
|
|
||
| // Filter.jsx passes `options` generically to every ValueInput type (meant | ||
| // for the sync `select` type); this type fetches its own, so it's stripped | ||
| // out here rather than spread onto the Autocomplete below. | ||
| const { options: _staleOptions, ...autocompleteProps } = rest; | ||
|
|
||
| const fetchOptions = (searchTerm) => { | ||
| if (searchTerm && searchTerm.length < minSearchLength) { | ||
| setOptions([]); | ||
| return; | ||
| } | ||
| setLoading(true); | ||
| queryFunction(searchTerm, (rawResults) => { | ||
| setOptions((rawResults || []).map((item) => ({ ...formatOption(item), raw: item }))); | ||
| setLoading(false); | ||
| }); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| fetchOptions(""); | ||
| return () => { | ||
| if (debounceRef.current) clearTimeout(debounceRef.current); | ||
| }; | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []); | ||
|
Comment on lines
+56
to
+74
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. Guard against stale/out-of-order async responses when typing quickly.
💡 Suggested fix@@
const [options, setOptions] = useState([]);
const [loading, setLoading] = useState(false);
const debounceRef = useRef(null);
+ const lastRequestIdRef = useRef(0);
+ const isMountedRef = useRef(true);
@@
const fetchOptions = (searchTerm) => {
+ const requestId = ++lastRequestIdRef.current;
if (searchTerm && searchTerm.length < minSearchLength) {
setOptions([]);
+ setLoading(false);
return;
}
setLoading(true);
queryFunction(searchTerm, (rawResults) => {
+ if (!isMountedRef.current || requestId !== lastRequestIdRef.current) return;
setOptions((rawResults || []).map((item) => ({ ...formatOption(item), raw: item })));
setLoading(false);
});
};
@@
useEffect(() => {
fetchOptions("");
return () => {
+ isMountedRef.current = false;
if (debounceRef.current) clearTimeout(debounceRef.current);
};Also applies to: 76-80 🤖 Prompt for AI Agents |
||
|
|
||
| const handleInputChange = (event, newInputValue, reason) => { | ||
| if (reason !== "input") return; | ||
| if (debounceRef.current) clearTimeout(debounceRef.current); | ||
| debounceRef.current = setTimeout(() => fetchOptions(newInputValue), debounceWait); | ||
| }; | ||
|
|
||
| const handleChange = (event, selected) => { | ||
| onChange({ target: { value: multiple ? selected || [] : selected || null } }); | ||
| }; | ||
|
|
||
| // Filter.jsx's single-value default is "" (not null); treat it as empty. | ||
| const normalizedValue = multiple ? value || [] : value || null; | ||
| const finalPlaceholder = | ||
| placeholder || T.translate("grid_filter.placeholders.async"); | ||
|
|
||
| return ( | ||
| <Autocomplete | ||
| id={id} | ||
| options={options} | ||
| value={normalizedValue} | ||
| onChange={handleChange} | ||
| onInputChange={handleInputChange} | ||
| loading={loading} | ||
| multiple={multiple} | ||
| disabled={disabled} | ||
| fullWidth | ||
| size="small" | ||
| getOptionLabel={(option) => option?.label || ""} | ||
| isOptionEqualToValue={(option, val) => option.value === val.value} | ||
| renderInput={(params) => ( | ||
| <TextField | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...params} | ||
| label={label} | ||
| placeholder={finalPlaceholder} | ||
| slotProps={{ | ||
| input: { | ||
| ...params.InputProps, | ||
| endAdornment: ( | ||
| <> | ||
| {loading && <CircularProgress color="inherit" size={16} />} | ||
| {params.InputProps?.endAdornment} | ||
| </> | ||
| ) | ||
| } | ||
| }} | ||
| /> | ||
| )} | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...autocompleteProps} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| AsyncSelectInput.propTypes = { | ||
| id: PropTypes.string.isRequired, | ||
| value: PropTypes.oneOfType([optionShape, PropTypes.arrayOf(optionShape), PropTypes.string]), | ||
| label: PropTypes.string, | ||
| placeholder: PropTypes.string, | ||
| disabled: PropTypes.bool, | ||
| multiple: PropTypes.bool, | ||
| queryFunction: PropTypes.func.isRequired, | ||
| formatOption: PropTypes.func, | ||
| debounceWait: PropTypes.number, | ||
| minSearchLength: PropTypes.number, | ||
| onChange: PropTypes.func.isRequired | ||
| }; | ||
|
|
||
| AsyncSelectInput.defaultProps = { | ||
| value: null, | ||
| label: "", | ||
| placeholder: "", | ||
| disabled: false, | ||
| multiple: false, | ||
| formatOption: defaultFormatOption, | ||
| debounceWait: DEBOUNCE_WAIT_250, | ||
| minSearchLength: 0 | ||
| }; | ||
|
|
||
| export default AsyncSelectInput; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * */ | ||
|
|
||
| import React from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import T from "i18n-react/dist/i18n-react"; | ||
| import AsyncSelectInput from "./AsyncSelectInput"; | ||
| import { queryCompanies } from "../../../../../utils/query-actions"; | ||
|
|
||
| const defaultFormatOption = (company) => ({ | ||
| value: company.id, | ||
| label: company.name | ||
| }); | ||
|
|
||
| const CompanySelectInput = ({ queryFunction, placeholder, ...rest }) => ( | ||
| <AsyncSelectInput | ||
| queryFunction={queryFunction || queryCompanies} | ||
| placeholder={placeholder || T.translate("grid_filter.placeholders.company")} | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...rest} | ||
| /> | ||
| ); | ||
|
|
||
| CompanySelectInput.propTypes = { | ||
| queryFunction: PropTypes.func, | ||
| placeholder: PropTypes.string | ||
| }; | ||
|
|
||
| CompanySelectInput.defaultProps = { | ||
| queryFunction: null, | ||
| formatOption: defaultFormatOption | ||
| }; | ||
|
|
||
| export default CompanySelectInput; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
@mui/x-date-pickersin peerDependencies.The new DateTimeInput component uses
DateTimePickerfrom@mui/x-date-pickers, but this package is only listed in devDependencies (line 30), not in peerDependencies. Since this is a component library that exports GridFilter to consuming applications,@mui/x-date-pickersmust be declared as a peer dependency.Without this declaration, consuming applications that use GridFilter with the new
datetimevalue type will encounter runtime errors when DateTimeInput attempts to import DateTimePicker, and developers won't receive clear guidance from package.json about the missing dependency.📦 Proposed fix to add missing peer dependency
"peerDependencies": { "`@emotion/react`": "^11.11.4", "`@emotion/styled`": "^11.11.5", "`@mui/icons-material`": "^6.4.3", "`@mui/material`": "^6.4.3", + "`@mui/x-date-pickers`": "^7.26.0", "`@react-pdf/renderer`": "^3.1.11",🤖 Prompt for AI Agents