fix: default value if mode single or multiple

This commit is contained in:
Firman Syah 2026-06-29 10:14:38 +07:00
parent 401eb26458
commit 77110e1e9a
3 changed files with 1887 additions and 1847 deletions

3672
dist/index.js vendored

File diff suppressed because it is too large Load Diff

View File

@ -14,15 +14,14 @@ const Testing = () => {
Authorization: `Bearer ${token}`,
}}
>
{/* <ListVirtual
<ListVirtual
id="company-select"
label="Company"
name="name"
url="/company"
defaultValue={"442"}
defaultValue={442}
size="small"
multiple
/> */}
/>
<ListVirtual
label="License Plate"
name="license_plate"
@ -35,7 +34,7 @@ const Testing = () => {
}}
size="small"
filter={[
{name:"company_id",value: "740","logic_operator":"=","operator":"AND"}
{name:"company_id",value: "442","logic_operator":"=","operator":"AND"}
]}
/>
</ApiProvider>

View File

@ -121,51 +121,58 @@ export default function ListVirtual({
}
}, [open]);
const fetchById = async (id) => {
const fetchDefaultValue = async (value) => {
setLoading(true);
const payload = new FilterTableBuilder()
.setPaging(0, 1)
.equal("id", id);
const payload = new FilterTableBuilder().setPaging(0, lengthData);
if (multiple) {
payload.in("id", value); // value = [1,2,3]
} else {
payload.equal("id", value); // value = 1
}
try {
const res = await apiRequest.search(
url,
payload.build(),
{
headers: headersRequest
headers: headersRequest,
},
isApiGo,
);
const data = res.data?.[0];
const data = res.data || [];
if (data) {
setSelected(data);
}
setSelected(multiple ? data : data[0] || null);
} catch (err) {
console.error(err);
}
} finally {
setLoading(false);
}
};
useEffect(() => {
if (!defaultValue) return;
if (defaultValue == null) return;
// Already object(s)
if (typeof defaultValue === "object") {
if (multiple) {
if (Array.isArray(defaultValue)) {
if (
Array.isArray(defaultValue) &&
defaultValue.length &&
typeof defaultValue[0] === "object"
) {
setSelected(defaultValue);
} else {
setSelected([defaultValue]);
return;
}
} else {
if (Array.isArray(defaultValue)) {
setSelected(defaultValue[0] || null);
} else {
} else if (!Array.isArray(defaultValue)) {
setSelected(defaultValue);
return;
}
}
fetchDefaultValue(defaultValue);
}, [defaultValue, multiple]);
const handleSelect = (row) => {