Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,21 @@ export const getColorFormatters = memoizeOne(
(
columnConfig: ConditionalFormattingConfig[] | undefined,
data: DataRecord[],
theme?: Record<string, any>,
alpha?: boolean,
) =>
columnConfig?.reduce(
(acc: ColorFormatters, config: ConditionalFormattingConfig) => {
let resolvedColorScheme = config.colorScheme;
if (
theme &&
typeof config.colorScheme === 'string' &&
config.colorScheme.startsWith('color') &&
theme[config.colorScheme]
) {
resolvedColorScheme = theme[config.colorScheme] as string;
}

if (
config?.column !== undefined &&
(config?.operator === Comparator.None ||
Expand All @@ -257,7 +268,7 @@ export const getColorFormatters = memoizeOne(
acc.push({
column: config?.column,
getColorFromValue: getColorFunction(
config,
{ ...config, colorScheme: resolvedColorScheme },
data.map(row => row[config.column!] as number),
alpha,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,29 @@ test('handles missing theme gracefully', () => {
// Should still render without crashing
expect(screen.getByTestId('ag-grid-react')).toBeInTheDocument();
});

test('merges theme overrides with default theme parameters', () => {
const themeOverrides = {
fontSize: 16,
headerBackgroundColor: '#custom-color',
};

render(
<ThemedAgGridReact
rowData={mockRowData}
columnDefs={mockColumnDefs}
themeOverrides={themeOverrides}
/>,
);

const agGrid = screen.getByTestId('ag-grid-react');
const theme = JSON.parse(agGrid.getAttribute('data-theme') || '{}');

// Custom overrides should be applied
expect(theme.fontSize).toBe(16);
expect(theme.headerBackgroundColor).toBe('#custom-color');

// Default theme parameters should still be present
expect(theme.foregroundColor).toBeDefined();
expect(theme.borderColor).toBeDefined();
});
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ export {
// Re-export AgGridReact for ref types
export { AgGridReact } from 'ag-grid-react';

// Export the setup function for AG-Grid modules
export { setupAGGridModules } from './setupAGGridModules';
// Export the setup function and default modules for AG-Grid
export { setupAGGridModules, defaultModules } from './setupAGGridModules';
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 { ModuleRegistry } from 'ag-grid-community';
import { setupAGGridModules, defaultModules } from './setupAGGridModules';

jest.mock('ag-grid-community', () => ({
ModuleRegistry: {
registerModules: jest.fn(),
},
ColumnAutoSizeModule: { moduleName: 'ColumnAutoSizeModule' },
ColumnHoverModule: { moduleName: 'ColumnHoverModule' },
RowAutoHeightModule: { moduleName: 'RowAutoHeightModule' },
RowStyleModule: { moduleName: 'RowStyleModule' },
PaginationModule: { moduleName: 'PaginationModule' },
CellStyleModule: { moduleName: 'CellStyleModule' },
TextFilterModule: { moduleName: 'TextFilterModule' },
NumberFilterModule: { moduleName: 'NumberFilterModule' },
DateFilterModule: { moduleName: 'DateFilterModule' },
ExternalFilterModule: { moduleName: 'ExternalFilterModule' },
CsvExportModule: { moduleName: 'CsvExportModule' },
ColumnApiModule: { moduleName: 'ColumnApiModule' },
RowApiModule: { moduleName: 'RowApiModule' },
CellApiModule: { moduleName: 'CellApiModule' },
RenderApiModule: { moduleName: 'RenderApiModule' },
ClientSideRowModelModule: { moduleName: 'ClientSideRowModelModule' },
CustomFilterModule: { moduleName: 'CustomFilterModule' },
}));

beforeEach(() => {
jest.clearAllMocks();
});

test('defaultModules exports an array of AG Grid modules', () => {
expect(Array.isArray(defaultModules)).toBe(true);
expect(defaultModules.length).toBeGreaterThan(0);

// Verify it contains expected modules
const moduleNames = defaultModules.map((m: any) => m.moduleName);
expect(moduleNames).toContain('ColumnAutoSizeModule');
expect(moduleNames).toContain('PaginationModule');
expect(moduleNames).toContain('ClientSideRowModelModule');
});

test('setupAGGridModules registers default modules when called without arguments', () => {
setupAGGridModules();

expect(ModuleRegistry.registerModules).toHaveBeenCalledTimes(1);
expect(ModuleRegistry.registerModules).toHaveBeenCalledWith(defaultModules);
});

test('setupAGGridModules registers default + additional modules when provided', () => {
const mockEnterpriseModule1 = { moduleName: 'MultiFilterModule' };
const mockEnterpriseModule2 = { moduleName: 'PivotModule' };
const additionalModules = [mockEnterpriseModule1, mockEnterpriseModule2];

setupAGGridModules(additionalModules);

expect(ModuleRegistry.registerModules).toHaveBeenCalledTimes(1);

const registeredModules = (ModuleRegistry.registerModules as jest.Mock).mock
.calls[0][0];

// Should contain all default modules
defaultModules.forEach(module => {
expect(registeredModules).toContain(module);
});

// Should contain additional modules
expect(registeredModules).toContain(mockEnterpriseModule1);
expect(registeredModules).toContain(mockEnterpriseModule2);

// Total length should be default + additional
expect(registeredModules.length).toBe(
defaultModules.length + additionalModules.length,
);
});

test('setupAGGridModules handles empty additional modules array', () => {
setupAGGridModules([]);

expect(ModuleRegistry.registerModules).toHaveBeenCalledTimes(1);
expect(ModuleRegistry.registerModules).toHaveBeenCalledWith(defaultModules);
});

test('setupAGGridModules does not mutate defaultModules array', () => {
const originalLength = defaultModules.length;
const mockEnterpriseModule = { moduleName: 'EnterpriseModule' };

setupAGGridModules([mockEnterpriseModule]);

// defaultModules should remain unchanged
expect(defaultModules.length).toBe(originalLength);
expect(defaultModules).not.toContain(mockEnterpriseModule);
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import {
ModuleRegistry,
type Module,
ColumnAutoSizeModule,
ColumnHoverModule,
RowAutoHeightModule,
Expand All @@ -39,27 +40,29 @@ import {
} from 'ag-grid-community';

/**
* Registers the AG-Grid modules required for Superset's table functionality.
* This should be called once during application initialization.
* Default AG Grid modules that are registered by default.
* These modules provide core AG Grid functionality.
*/
export const setupAGGridModules = () => {
ModuleRegistry.registerModules([
ColumnAutoSizeModule,
ColumnHoverModule,
RowAutoHeightModule,
RowStyleModule,
PaginationModule,
CellStyleModule,
TextFilterModule,
NumberFilterModule,
DateFilterModule,
ExternalFilterModule,
CsvExportModule,
ColumnApiModule,
RowApiModule,
CellApiModule,
RenderApiModule,
ClientSideRowModelModule,
CustomFilterModule,
]);
export const defaultModules: Module[] = [
ColumnAutoSizeModule,
ColumnHoverModule,
RowAutoHeightModule,
RowStyleModule,
PaginationModule,
CellStyleModule,
TextFilterModule,
NumberFilterModule,
DateFilterModule,
ExternalFilterModule,
CsvExportModule,
ColumnApiModule,
RowApiModule,
CellApiModule,
RenderApiModule,
ClientSideRowModelModule,
CustomFilterModule,
];

export const setupAGGridModules = (additionalModules: Module[] = []) => {
ModuleRegistry.registerModules([...defaultModules, ...additionalModules]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export {
ThemedAgGridReact,
type ThemedAgGridReactProps,
setupAGGridModules,
defaultModules,
} from './ThemedAgGridReact';
export {
CodeEditor,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ function isPositiveNumber(value: string | number | null | undefined) {
);
}

const calculateDifferences = (
originalValue: number,
comparisonValue: number,
) => {
const valueDifference = originalValue - comparisonValue;
let percentDifferenceNum;
if (!originalValue && !comparisonValue) {
percentDifferenceNum = 0;
} else if (!originalValue || !comparisonValue) {
percentDifferenceNum = originalValue ? 1 : -1;
} else {
percentDifferenceNum =
(originalValue - comparisonValue) / Math.abs(comparisonValue);
}
return { valueDifference, percentDifferenceNum };
};

const processComparisonTotals = (
comparisonSuffix: string,
totals?: DataRecord[],
Expand Down Expand Up @@ -149,23 +166,6 @@ const getComparisonColFormatter = (
return formatter;
};

const calculateDifferences = (
originalValue: number,
comparisonValue: number,
) => {
const valueDifference = originalValue - comparisonValue;
let percentDifferenceNum;
if (!originalValue && !comparisonValue) {
percentDifferenceNum = 0;
} else if (!originalValue || !comparisonValue) {
percentDifferenceNum = originalValue ? 1 : -1;
} else {
percentDifferenceNum =
(originalValue - comparisonValue) / Math.abs(comparisonValue);
}
return { valueDifference, percentDifferenceNum };
};

const processComparisonDataRecords = memoizeOne(
function processComparisonDataRecords(
originalData: DataRecord[] | undefined,
Expand Down Expand Up @@ -471,6 +471,7 @@ const transformProps = (
filterState,
hooks: { setDataMask = () => {} },
emitCrossFilters,
theme,
} = chartProps;

const {
Expand All @@ -494,6 +495,36 @@ const transformProps = (

const allowRearrangeColumns = true;

// Calculate time comparison settings early since they're used in multiple places
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we add any early usages? I am ok with this but don't understand why it's changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an eslint fix to not use a function before it's define

const isUsingTimeComparison =
!isEmpty(time_compare) &&
queryMode === QueryMode.Aggregate &&
comparison_type === ComparisonType.Values &&
isFeatureEnabled(FeatureFlag.TableV2TimeComparisonEnabled);

const nonCustomNorInheritShifts = ensureIsArray(formData.time_compare).filter(
(shift: string) => shift !== 'custom' && shift !== 'inherit',
);
const customOrInheritShifts = ensureIsArray(formData.time_compare).filter(
(shift: string) => shift === 'custom' || shift === 'inherit',
);

let timeOffsets: string[] = [];

if (isUsingTimeComparison && !isEmpty(nonCustomNorInheritShifts)) {
timeOffsets = nonCustomNorInheritShifts;
}

// Shifts for custom or inherit time comparison
if (isUsingTimeComparison && !isEmpty(customOrInheritShifts)) {
if (customOrInheritShifts.includes('custom')) {
timeOffsets = timeOffsets.concat([formData.start_date_offset]);
}
if (customOrInheritShifts.includes('inherit')) {
timeOffsets = timeOffsets.concat(['inherit']);
}
}

const calculateBasicStyle = (
percentDifferenceNum: number,
colorOption: ColorSchemeEnum,
Expand Down Expand Up @@ -606,12 +637,6 @@ const transformProps = (
: undefined;
};

const isUsingTimeComparison =
!isEmpty(time_compare) &&
queryMode === QueryMode.Aggregate &&
comparison_type === ComparisonType.Values &&
isFeatureEnabled(FeatureFlag.TableV2TimeComparisonEnabled);

let hasServerPageLengthChanged = false;

const pageLengthFromMap = serverPageLengthMap.get(slice_id);
Expand All @@ -624,28 +649,6 @@ const transformProps = (

const timeGrain = extractTimegrain(formData);

const nonCustomNorInheritShifts = ensureIsArray(formData.time_compare).filter(
(shift: string) => shift !== 'custom' && shift !== 'inherit',
);
const customOrInheritShifts = ensureIsArray(formData.time_compare).filter(
(shift: string) => shift === 'custom' || shift === 'inherit',
);

let timeOffsets: string[] = [];

if (isUsingTimeComparison && !isEmpty(nonCustomNorInheritShifts)) {
timeOffsets = nonCustomNorInheritShifts;
}

// Shifts for custom or inherit time comparison
if (isUsingTimeComparison && !isEmpty(customOrInheritShifts)) {
if (customOrInheritShifts.includes('custom')) {
timeOffsets = timeOffsets.concat([formData.start_date_offset]);
}
if (customOrInheritShifts.includes('inherit')) {
timeOffsets = timeOffsets.concat(['inherit']);
}
}
const comparisonSuffix = isUsingTimeComparison
? ensureIsArray(timeOffsets)[0]
: '';
Expand Down Expand Up @@ -685,7 +688,7 @@ const transformProps = (
const basicColorFormatters =
comparisonColorEnabled && getBasicColorFormatter(baseQuery?.data, columns);
const columnColorFormatters =
getColorFormatters(conditionalFormatting, passedData) ?? [];
getColorFormatters(conditionalFormatting, passedData, theme) ?? [];

const basicColorColumnFormatters = getBasicColorFormatterForColumn(
baseQuery?.data,
Expand Down
Loading
Loading