Feature/gasht rahdari cartable
This commit is contained in:
@@ -4,11 +4,12 @@ import useRequest from "@/lib/hooks/useRequest";
|
||||
import { debounce } from "@mui/material/utils";
|
||||
import { GET_CAR_LIST_SEARCH } from "@/core/utils/routes";
|
||||
|
||||
const CarCode = ({ carCode, setCarCode, inputValueDefault = "", error }) => {
|
||||
const CarCode = ({ carCode, setCarCode, inputValueDefault = "", error, multiple = false }) => {
|
||||
const [inputValue, setInputValue] = useState(inputValueDefault);
|
||||
const [options, setOptions] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const requestServer = useRequest();
|
||||
|
||||
const fetchCarCodes = (inputValue, controller) => {
|
||||
const debouncer = debounce((query) => {
|
||||
if (!query || query?.length < 3) {
|
||||
@@ -40,20 +41,32 @@ const CarCode = ({ carCode, setCarCode, inputValueDefault = "", error }) => {
|
||||
|
||||
return (
|
||||
<Autocomplete
|
||||
multiple={multiple}
|
||||
value={carCode}
|
||||
size="small"
|
||||
onChange={(event, newValue) => {
|
||||
setCarCode(newValue || null); // Set an empty string if no value is selected
|
||||
setCarCode(multiple ? newValue || [] : newValue || null);
|
||||
}}
|
||||
inputValue={inputValue}
|
||||
onInputChange={(event, newInputValue) => {
|
||||
setInputValue(newInputValue || ""); // Prevent inputValue from being null or undefined
|
||||
}}
|
||||
options={options}
|
||||
getOptionLabel={(option) => (typeof option === "string" ? option : option.machine_code)}
|
||||
getOptionLabel={(option) => {
|
||||
if (typeof option === "string") return option;
|
||||
const { machine_code, car_name, plak_number } = option;
|
||||
let label = `${machine_code || ""}`;
|
||||
if (car_name) {
|
||||
label += ` | ${car_name}`;
|
||||
}
|
||||
if (plak_number) {
|
||||
label += ` | ${plak_number}`;
|
||||
}
|
||||
return label;
|
||||
}}
|
||||
isOptionEqualToValue={(option, value) => {
|
||||
if (!value) return false; // Handle null/undefined values
|
||||
if (value === "") return option.machine_code === ""; // Handle empty string case
|
||||
if (!value) return false;
|
||||
if (value === "") return option.machine_code === "";
|
||||
return option.machine_code === (value?.machine_code || value);
|
||||
}}
|
||||
loading={loading}
|
||||
@@ -65,8 +78,8 @@ const CarCode = ({ carCode, setCarCode, inputValueDefault = "", error }) => {
|
||||
{...params}
|
||||
label="کد خودرو"
|
||||
fullWidth
|
||||
error={!!error} // نمایش خطا
|
||||
helperText={error?.message} // پیام خطا
|
||||
error={!!error}
|
||||
helperText={error?.message}
|
||||
InputProps={{
|
||||
...params.InputProps,
|
||||
endAdornment: (
|
||||
|
||||
Reference in New Issue
Block a user