Skip to content

Commit deedc4a

Browse files
committed
fix delete any in colorpicker
1 parent c7abc98 commit deedc4a

2 files changed

Lines changed: 97 additions & 2 deletions

File tree

superset-frontend/src/explore/components/controls/ColorPickerControl.test.tsx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,80 @@ test('calls onChange with RGB object when resolveThemeTokens is false', async ()
163163

164164
expect(onChange).toHaveBeenCalledWith({ r: 0, g: 150, b: 0, a: 0.2 });
165165
});
166+
167+
test('resolves colorSuccess theme token correctly when matching color is selected', async () => {
168+
const onChange = jest.fn();
169+
170+
jest
171+
.spyOn(require('@apache-superset/core/theme'), 'useTheme')
172+
.mockReturnValue({
173+
colors: {
174+
colorSuccess: 'rgba(82, 196, 26, 1)',
175+
},
176+
});
177+
178+
render(
179+
<ColorPickerControl
180+
{...defaultProps}
181+
onChange={onChange}
182+
resolveThemeTokens
183+
presets={[{ label: 'Theme Tokens', colors: ['colorSuccess'] }]}
184+
/>,
185+
);
186+
187+
const colorPickerTrigger = document.querySelector(
188+
'.ant-color-picker-trigger',
189+
);
190+
expect(colorPickerTrigger).toBeInTheDocument();
191+
await userEvent.click(colorPickerTrigger!);
192+
193+
await waitFor(() => {
194+
expect(
195+
document.querySelector('.ant-color-picker-presets-items'),
196+
).toBeInTheDocument();
197+
});
198+
199+
const successPreset = document.querySelector(
200+
'.ant-color-picker-presets-color [style*="82, 196, 26"]',
201+
) as HTMLElement | null;
202+
203+
expect(successPreset).toBeInTheDocument();
204+
205+
await userEvent.click(successPreset!);
206+
207+
expect(onChange).toHaveBeenCalledWith('colorSuccess');
208+
});
209+
210+
test('handles theme with nested colors object', () => {
211+
jest
212+
.spyOn(require('@apache-superset/core/theme'), 'useTheme')
213+
.mockReturnValue({
214+
colors: { primary: '#007bff' },
215+
});
216+
217+
const { container } = render(<ColorPickerControl {...defaultProps} />);
218+
expect(
219+
container.querySelector('.ant-color-picker-trigger'),
220+
).toBeInTheDocument();
221+
});
222+
223+
test('handles theme without colors field', () => {
224+
jest
225+
.spyOn(require('@apache-superset/core/theme'), 'useTheme')
226+
.mockReturnValue({
227+
primary: '#007bff',
228+
});
229+
230+
const { container } = render(<ColorPickerControl {...defaultProps} />);
231+
expect(
232+
container.querySelector('.ant-color-picker-trigger'),
233+
).toBeInTheDocument();
234+
});
235+
236+
test('handles undefined theme gracefully', () => {
237+
jest
238+
.spyOn(require('@apache-superset/core/theme'), 'useTheme')
239+
.mockReturnValue(undefined);
240+
241+
expect(() => render(<ColorPickerControl {...defaultProps} />)).not.toThrow();
242+
});

superset-frontend/src/explore/components/controls/ColorPickerControl.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
type ColorValue,
2626
} from '@superset-ui/core/components';
2727
import ControlHeader from '../ControlHeader';
28-
import { useTheme } from '@apache-superset/core/theme';
28+
import { useTheme, type SupersetTheme } from '@apache-superset/core/theme';
2929

3030
const SPECIAL_COLORS = {
3131
Red: { r: 150, g: 0, b: 0, a: 0.2 },
@@ -83,6 +83,24 @@ function toDisplayHex(
8383
return rgbaToHex(value).toLowerCase();
8484
}
8585

86+
const extractThemeColors = (
87+
theme: SupersetTheme | undefined | null,
88+
): Record<string, string> => {
89+
if (!theme || typeof theme !== 'object') {
90+
return {};
91+
}
92+
93+
if (
94+
'colors' in theme &&
95+
typeof theme.colors === 'object' &&
96+
theme.colors !== null
97+
) {
98+
return theme.colors as Record<string, string>;
99+
}
100+
101+
return theme as unknown as Record<string, string>;
102+
};
103+
86104
export default function ColorPickerControl({
87105
onChange,
88106
value,
@@ -96,7 +114,7 @@ export default function ColorPickerControl({
96114
const theme = useTheme();
97115

98116
const themeColors = useMemo<Record<string, string>>(
99-
() => (theme as any)?.colors || theme || {},
117+
() => extractThemeColors(theme),
100118
[theme],
101119
);
102120

0 commit comments

Comments
 (0)