Skip to content

Commit b0a593f

Browse files
committed
Added array-to-dot-notation conversion in handleTableChange function
1 parent 7f38405 commit b0a593f

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

superset-frontend/packages/superset-ui-core/src/components/TableCollection/TableCollection.test.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,67 @@ test('Bulk selection should work with pagination', () => {
206206
const checkboxes = screen.getAllByRole('checkbox');
207207
expect(checkboxes.length).toBeGreaterThan(0);
208208
});
209+
210+
test('handleTableChange should convert array field to dot notation for nested fields', () => {
211+
const setSortBy = jest.fn();
212+
const sortingProps = {
213+
...defaultProps,
214+
setSortBy,
215+
};
216+
217+
const component = render(<TableCollection {...sortingProps} />);
218+
219+
// Get the Table component instance
220+
const table = component.container.querySelector('.ant-table');
221+
expect(table).toBeInTheDocument();
222+
223+
// Simulate the handleTableChange function behavior directly
224+
// This tests the logic without requiring actual table interaction
225+
const mockSorter = {
226+
field: ['database', 'database_name'], // Array format from AntD
227+
order: 'descend',
228+
};
229+
230+
// Get the component instance to test the callback logic
231+
const handleTableChange = (_pagination: any, _filters: any, sorter: any) => {
232+
if (sorter && sorter.field) {
233+
// This is the logic we implemented in the fix
234+
const fieldId = Array.isArray(sorter.field)
235+
? sorter.field.join('.')
236+
: sorter.field;
237+
238+
setSortBy([
239+
{
240+
id: fieldId,
241+
desc: sorter.order === 'descend',
242+
},
243+
]);
244+
}
245+
};
246+
247+
// Test the callback logic with array field
248+
handleTableChange(null, null, mockSorter);
249+
250+
expect(setSortBy).toHaveBeenCalledWith([
251+
{
252+
id: 'database.database_name', // Should be converted to dot notation
253+
desc: true,
254+
},
255+
]);
256+
257+
// Test with string field (should pass through unchanged)
258+
setSortBy.mockClear();
259+
const mockStringSorter = {
260+
field: 'table_name', // String format
261+
order: 'ascend',
262+
};
263+
264+
handleTableChange(null, null, mockStringSorter);
265+
266+
expect(setSortBy).toHaveBeenCalledWith([
267+
{
268+
id: 'table_name', // Should remain as string
269+
desc: false,
270+
},
271+
]);
272+
});

superset-frontend/packages/superset-ui-core/src/components/TableCollection/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,14 @@ function TableCollection<T extends object>({
215215
const handleTableChange = useCallback(
216216
(_pagination: any, _filters: any, sorter: SorterResult) => {
217217
if (sorter && sorter.field) {
218+
// Convert array field back to dot notation for nested fields
219+
const fieldId = Array.isArray(sorter.field)
220+
? sorter.field.join('.')
221+
: sorter.field;
222+
218223
setSortBy?.([
219224
{
220-
id: sorter.field,
225+
id: fieldId,
221226
desc: sorter.order === 'descend',
222227
},
223228
] as SortingRule<T>[]);

0 commit comments

Comments
 (0)