@bsol-oss/react-datatable5 13.0.1-beta.0 → 13.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -5651,6 +5651,69 @@ const defaultRenderDisplay = (item) => {
|
|
|
5651
5651
|
return JSON.stringify(item);
|
|
5652
5652
|
};
|
|
5653
5653
|
|
|
5654
|
+
/**
|
|
5655
|
+
* Load initial values for IdPicker fields into idMap
|
|
5656
|
+
* Uses customQueryFn if available, otherwise falls back to getTableData
|
|
5657
|
+
*
|
|
5658
|
+
* @param params - Configuration for loading initial values
|
|
5659
|
+
* @returns Promise with fetched data and idMap
|
|
5660
|
+
*/
|
|
5661
|
+
const loadInitialValues = async ({ ids, foreign_key, serverUrl, setIdMap, }) => {
|
|
5662
|
+
if (!ids || ids.length === 0) {
|
|
5663
|
+
return { data: { data: [], count: 0 }, idMap: {} };
|
|
5664
|
+
}
|
|
5665
|
+
const { table, column: column_ref, customQueryFn } = foreign_key;
|
|
5666
|
+
// Filter out IDs that are already in idMap (optional optimization)
|
|
5667
|
+
// For now, we'll fetch all requested IDs to ensure consistency
|
|
5668
|
+
if (customQueryFn) {
|
|
5669
|
+
const { data, idMap: returnedIdMap } = await customQueryFn({
|
|
5670
|
+
searching: '',
|
|
5671
|
+
limit: ids.length,
|
|
5672
|
+
offset: 0,
|
|
5673
|
+
where: [
|
|
5674
|
+
{
|
|
5675
|
+
id: column_ref,
|
|
5676
|
+
value: ids.length === 1 ? ids[0] : ids, // CustomQueryFn accepts string | string[]
|
|
5677
|
+
},
|
|
5678
|
+
],
|
|
5679
|
+
});
|
|
5680
|
+
// Update idMap with returned values
|
|
5681
|
+
if (returnedIdMap && Object.keys(returnedIdMap).length > 0) {
|
|
5682
|
+
setIdMap((state) => {
|
|
5683
|
+
return { ...state, ...returnedIdMap };
|
|
5684
|
+
});
|
|
5685
|
+
}
|
|
5686
|
+
return { data, idMap: returnedIdMap || {} };
|
|
5687
|
+
}
|
|
5688
|
+
// Fallback to default getTableData
|
|
5689
|
+
const data = await getTableData({
|
|
5690
|
+
serverUrl,
|
|
5691
|
+
searching: '',
|
|
5692
|
+
in_table: table,
|
|
5693
|
+
limit: ids.length,
|
|
5694
|
+
offset: 0,
|
|
5695
|
+
where: [
|
|
5696
|
+
{
|
|
5697
|
+
id: column_ref,
|
|
5698
|
+
value: ids, // Always pass as array
|
|
5699
|
+
},
|
|
5700
|
+
],
|
|
5701
|
+
});
|
|
5702
|
+
// Build idMap from fetched data
|
|
5703
|
+
const newMap = Object.fromEntries((data ?? { data: [] }).data.map((item) => {
|
|
5704
|
+
return [
|
|
5705
|
+
item[column_ref],
|
|
5706
|
+
{
|
|
5707
|
+
...item,
|
|
5708
|
+
},
|
|
5709
|
+
];
|
|
5710
|
+
}));
|
|
5711
|
+
// Update idMap state
|
|
5712
|
+
setIdMap((state) => {
|
|
5713
|
+
return { ...state, ...newMap };
|
|
5714
|
+
});
|
|
5715
|
+
return { data: data, idMap: newMap };
|
|
5716
|
+
};
|
|
5654
5717
|
const useIdPickerData = ({ column, schema, prefix, isMultiple, }) => {
|
|
5655
5718
|
const { watch, getValues, formState: { errors }, setValue, } = reactHookForm.useFormContext();
|
|
5656
5719
|
const { serverUrl, idMap, setIdMap, idPickerLabels, insideDialog } = useSchemaContext();
|
|
@@ -5707,48 +5770,14 @@ const useIdPickerData = ({ column, schema, prefix, isMultiple, }) => {
|
|
|
5707
5770
|
if (missingIds.length === 0) {
|
|
5708
5771
|
return { data: [], count: 0 };
|
|
5709
5772
|
}
|
|
5710
|
-
|
|
5711
|
-
|
|
5712
|
-
|
|
5713
|
-
|
|
5714
|
-
offset: 0,
|
|
5715
|
-
where: [
|
|
5716
|
-
{
|
|
5717
|
-
id: column_ref,
|
|
5718
|
-
value: missingIds.length === 1 ? missingIds[0] : missingIds,
|
|
5719
|
-
},
|
|
5720
|
-
],
|
|
5721
|
-
});
|
|
5722
|
-
setIdMap((state) => {
|
|
5723
|
-
return { ...state, ...idMap };
|
|
5724
|
-
});
|
|
5725
|
-
return data;
|
|
5726
|
-
}
|
|
5727
|
-
const data = await getTableData({
|
|
5773
|
+
// Use the reusable loadInitialValues function
|
|
5774
|
+
const result = await loadInitialValues({
|
|
5775
|
+
ids: missingIds,
|
|
5776
|
+
foreign_key: foreign_key,
|
|
5728
5777
|
serverUrl,
|
|
5729
|
-
|
|
5730
|
-
in_table: table,
|
|
5731
|
-
limit: missingIds.length,
|
|
5732
|
-
offset: 0,
|
|
5733
|
-
where: [
|
|
5734
|
-
{
|
|
5735
|
-
id: column_ref,
|
|
5736
|
-
value: missingIds.length === 1 ? missingIds[0] : missingIds,
|
|
5737
|
-
},
|
|
5738
|
-
],
|
|
5739
|
-
});
|
|
5740
|
-
const newMap = Object.fromEntries((data ?? { data: [] }).data.map((item) => {
|
|
5741
|
-
return [
|
|
5742
|
-
item[column_ref],
|
|
5743
|
-
{
|
|
5744
|
-
...item,
|
|
5745
|
-
},
|
|
5746
|
-
];
|
|
5747
|
-
}));
|
|
5748
|
-
setIdMap((state) => {
|
|
5749
|
-
return { ...state, ...newMap };
|
|
5778
|
+
setIdMap,
|
|
5750
5779
|
});
|
|
5751
|
-
return data;
|
|
5780
|
+
return result.data;
|
|
5752
5781
|
},
|
|
5753
5782
|
enabled: missingIds.length > 0, // Only fetch if there are missing IDs
|
|
5754
5783
|
staleTime: 300000,
|
package/dist/index.mjs
CHANGED
|
@@ -5631,6 +5631,69 @@ const defaultRenderDisplay = (item) => {
|
|
|
5631
5631
|
return JSON.stringify(item);
|
|
5632
5632
|
};
|
|
5633
5633
|
|
|
5634
|
+
/**
|
|
5635
|
+
* Load initial values for IdPicker fields into idMap
|
|
5636
|
+
* Uses customQueryFn if available, otherwise falls back to getTableData
|
|
5637
|
+
*
|
|
5638
|
+
* @param params - Configuration for loading initial values
|
|
5639
|
+
* @returns Promise with fetched data and idMap
|
|
5640
|
+
*/
|
|
5641
|
+
const loadInitialValues = async ({ ids, foreign_key, serverUrl, setIdMap, }) => {
|
|
5642
|
+
if (!ids || ids.length === 0) {
|
|
5643
|
+
return { data: { data: [], count: 0 }, idMap: {} };
|
|
5644
|
+
}
|
|
5645
|
+
const { table, column: column_ref, customQueryFn } = foreign_key;
|
|
5646
|
+
// Filter out IDs that are already in idMap (optional optimization)
|
|
5647
|
+
// For now, we'll fetch all requested IDs to ensure consistency
|
|
5648
|
+
if (customQueryFn) {
|
|
5649
|
+
const { data, idMap: returnedIdMap } = await customQueryFn({
|
|
5650
|
+
searching: '',
|
|
5651
|
+
limit: ids.length,
|
|
5652
|
+
offset: 0,
|
|
5653
|
+
where: [
|
|
5654
|
+
{
|
|
5655
|
+
id: column_ref,
|
|
5656
|
+
value: ids.length === 1 ? ids[0] : ids, // CustomQueryFn accepts string | string[]
|
|
5657
|
+
},
|
|
5658
|
+
],
|
|
5659
|
+
});
|
|
5660
|
+
// Update idMap with returned values
|
|
5661
|
+
if (returnedIdMap && Object.keys(returnedIdMap).length > 0) {
|
|
5662
|
+
setIdMap((state) => {
|
|
5663
|
+
return { ...state, ...returnedIdMap };
|
|
5664
|
+
});
|
|
5665
|
+
}
|
|
5666
|
+
return { data, idMap: returnedIdMap || {} };
|
|
5667
|
+
}
|
|
5668
|
+
// Fallback to default getTableData
|
|
5669
|
+
const data = await getTableData({
|
|
5670
|
+
serverUrl,
|
|
5671
|
+
searching: '',
|
|
5672
|
+
in_table: table,
|
|
5673
|
+
limit: ids.length,
|
|
5674
|
+
offset: 0,
|
|
5675
|
+
where: [
|
|
5676
|
+
{
|
|
5677
|
+
id: column_ref,
|
|
5678
|
+
value: ids, // Always pass as array
|
|
5679
|
+
},
|
|
5680
|
+
],
|
|
5681
|
+
});
|
|
5682
|
+
// Build idMap from fetched data
|
|
5683
|
+
const newMap = Object.fromEntries((data ?? { data: [] }).data.map((item) => {
|
|
5684
|
+
return [
|
|
5685
|
+
item[column_ref],
|
|
5686
|
+
{
|
|
5687
|
+
...item,
|
|
5688
|
+
},
|
|
5689
|
+
];
|
|
5690
|
+
}));
|
|
5691
|
+
// Update idMap state
|
|
5692
|
+
setIdMap((state) => {
|
|
5693
|
+
return { ...state, ...newMap };
|
|
5694
|
+
});
|
|
5695
|
+
return { data: data, idMap: newMap };
|
|
5696
|
+
};
|
|
5634
5697
|
const useIdPickerData = ({ column, schema, prefix, isMultiple, }) => {
|
|
5635
5698
|
const { watch, getValues, formState: { errors }, setValue, } = useFormContext();
|
|
5636
5699
|
const { serverUrl, idMap, setIdMap, idPickerLabels, insideDialog } = useSchemaContext();
|
|
@@ -5687,48 +5750,14 @@ const useIdPickerData = ({ column, schema, prefix, isMultiple, }) => {
|
|
|
5687
5750
|
if (missingIds.length === 0) {
|
|
5688
5751
|
return { data: [], count: 0 };
|
|
5689
5752
|
}
|
|
5690
|
-
|
|
5691
|
-
|
|
5692
|
-
|
|
5693
|
-
|
|
5694
|
-
offset: 0,
|
|
5695
|
-
where: [
|
|
5696
|
-
{
|
|
5697
|
-
id: column_ref,
|
|
5698
|
-
value: missingIds.length === 1 ? missingIds[0] : missingIds,
|
|
5699
|
-
},
|
|
5700
|
-
],
|
|
5701
|
-
});
|
|
5702
|
-
setIdMap((state) => {
|
|
5703
|
-
return { ...state, ...idMap };
|
|
5704
|
-
});
|
|
5705
|
-
return data;
|
|
5706
|
-
}
|
|
5707
|
-
const data = await getTableData({
|
|
5753
|
+
// Use the reusable loadInitialValues function
|
|
5754
|
+
const result = await loadInitialValues({
|
|
5755
|
+
ids: missingIds,
|
|
5756
|
+
foreign_key: foreign_key,
|
|
5708
5757
|
serverUrl,
|
|
5709
|
-
|
|
5710
|
-
in_table: table,
|
|
5711
|
-
limit: missingIds.length,
|
|
5712
|
-
offset: 0,
|
|
5713
|
-
where: [
|
|
5714
|
-
{
|
|
5715
|
-
id: column_ref,
|
|
5716
|
-
value: missingIds.length === 1 ? missingIds[0] : missingIds,
|
|
5717
|
-
},
|
|
5718
|
-
],
|
|
5719
|
-
});
|
|
5720
|
-
const newMap = Object.fromEntries((data ?? { data: [] }).data.map((item) => {
|
|
5721
|
-
return [
|
|
5722
|
-
item[column_ref],
|
|
5723
|
-
{
|
|
5724
|
-
...item,
|
|
5725
|
-
},
|
|
5726
|
-
];
|
|
5727
|
-
}));
|
|
5728
|
-
setIdMap((state) => {
|
|
5729
|
-
return { ...state, ...newMap };
|
|
5758
|
+
setIdMap,
|
|
5730
5759
|
});
|
|
5731
|
-
return data;
|
|
5760
|
+
return result.data;
|
|
5732
5761
|
},
|
|
5733
5762
|
enabled: missingIds.length > 0, // Only fetch if there are missing IDs
|
|
5734
5763
|
staleTime: 300000,
|
|
@@ -1,8 +1,30 @@
|
|
|
1
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ForeignKeyProps } from './StringInputField';
|
|
2
3
|
import { CustomJSONSchema7 } from '../types/CustomJSONSchema7';
|
|
3
4
|
export interface RecordType {
|
|
4
5
|
[key: string]: any;
|
|
5
6
|
}
|
|
7
|
+
export interface LoadInitialValuesParams {
|
|
8
|
+
ids: string[];
|
|
9
|
+
foreign_key: ForeignKeyProps;
|
|
10
|
+
serverUrl: string;
|
|
11
|
+
setIdMap: React.Dispatch<React.SetStateAction<Record<string, object>>>;
|
|
12
|
+
}
|
|
13
|
+
export interface LoadInitialValuesResult {
|
|
14
|
+
data: {
|
|
15
|
+
data: RecordType[];
|
|
16
|
+
count: number;
|
|
17
|
+
};
|
|
18
|
+
idMap: Record<string, object>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Load initial values for IdPicker fields into idMap
|
|
22
|
+
* Uses customQueryFn if available, otherwise falls back to getTableData
|
|
23
|
+
*
|
|
24
|
+
* @param params - Configuration for loading initial values
|
|
25
|
+
* @returns Promise with fetched data and idMap
|
|
26
|
+
*/
|
|
27
|
+
export declare const loadInitialValues: ({ ids, foreign_key, serverUrl, setIdMap, }: LoadInitialValuesParams) => Promise<LoadInitialValuesResult>;
|
|
6
28
|
export interface UseIdPickerDataProps {
|
|
7
29
|
column: string;
|
|
8
30
|
schema: CustomJSONSchema7;
|