Skip to content

Commit 7d5540c

Browse files
committed
refactor: theme type logic
1 parent 876300c commit 7d5540c

File tree

6 files changed

+19
-43
lines changed

6 files changed

+19
-43
lines changed

src/chart_types/xy_chart/store/chart_state.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ChartStore } from './chart_state';
2020

2121
describe('Chart Store', () => {
2222
let store = new ChartStore();
23+
store.chartTheme = LIGHT_THEME;
2324

2425
const SPEC_ID = getSpecId('spec_1');
2526
const AXIS_ID = getAxisId('axis_1');
@@ -67,6 +68,7 @@ describe('Chart Store', () => {
6768
};
6869
beforeEach(() => {
6970
store = new ChartStore();
71+
store.chartTheme = LIGHT_THEME;
7072
store.updateParentDimensions(600, 600, 0, 0);
7173
store.computeChart();
7274
});

src/chart_types/xy_chart/store/chart_state.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
Rotation,
4747
} from '../utils/specs';
4848
import { formatTooltip, getSeriesTooltipValues } from '../tooltip/tooltip';
49-
import { LIGHT_THEME } from '../../../utils/themes/light_theme';
5049
import { mergeWithDefaultAnnotationLine, mergeWithDefaultAnnotationRect, Theme } from '../../../utils/themes/theme';
5150
import { compareByValueAsc } from '../../../utils/commons';
5251
import { computeChartDimensions } from '../utils/dimensions';
@@ -150,7 +149,10 @@ export class ChartStore {
150149

151150
chartRotation: Rotation = 0; // updated from jsx
152151
chartRendering: Rendering = 'canvas'; // updated from jsx
153-
chartTheme: Theme = LIGHT_THEME; // updated from jsx
152+
/**
153+
* Chart theme to be set from Settings.tsx
154+
*/
155+
chartTheme!: Theme;
154156
axesSpecs: Map<AxisId, AxisSpec> = new Map(); // readed from jsx
155157
axesTicksDimensions: Map<AxisId, AxisTicksDimensions> = new Map(); // computed
156158
axesPositions: Map<AxisId, Dimensions> = new Map(); // computed

src/specs/settings.test.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { mount } from 'enzyme';
22
import * as React from 'react';
33
import { Position, Rendering, Rotation } from '../chart_types/xy_chart/utils/specs';
44
import { DARK_THEME } from '../utils/themes/dark_theme';
5-
import { LIGHT_THEME } from '../utils/themes/light_theme';
65
import { TooltipType } from '../chart_types/xy_chart/utils/interactions';
76
import { ChartStore } from '../chart_types/xy_chart/store/chart_state';
87
import { DEFAULT_TOOLTIP_SNAP, DEFAULT_TOOLTIP_TYPE, SettingsComponent, SettingSpecProps } from './settings';
9-
import { PartialTheme, BaseThemeTypes } from '../utils/themes/theme';
8+
import { PartialTheme } from '../utils/themes/theme';
109

1110
describe('Settings spec component', () => {
1211
test('should update store on mount if spec has a chart store', () => {
@@ -56,7 +55,7 @@ describe('Settings spec component', () => {
5655
test('should set chart properties on chart store', () => {
5756
const chartStore = new ChartStore();
5857

59-
expect(chartStore.chartTheme).toEqual(LIGHT_THEME);
58+
expect(chartStore.chartTheme).toBeUndefined();
6059
expect(chartStore.chartRotation).toBe(0);
6160
expect(chartStore.chartRendering).toBe('canvas');
6261
expect(chartStore.animateData).toBe(false);
@@ -163,11 +162,11 @@ describe('Settings spec component', () => {
163162
},
164163
};
165164

166-
expect(chartStore.chartTheme).toEqual(LIGHT_THEME);
165+
expect(chartStore.chartTheme).toBeUndefined();
167166

168167
const updatedProps: SettingSpecProps = {
169168
theme: partialTheme,
170-
baseThemeType: BaseThemeTypes.Dark,
169+
baseTheme: DARK_THEME,
171170
rotation: 90 as Rotation,
172171
rendering: 'svg' as Rendering,
173172
animateData: true,

src/specs/settings.tsx

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { PureComponent } from 'react';
22
import { inject } from 'mobx-react';
33

44
import { DomainRange, Position, Rendering, Rotation } from '../chart_types/xy_chart/utils/specs';
5-
import { LIGHT_THEME } from '../utils/themes/light_theme';
6-
import { DARK_THEME } from '../utils/themes/dark_theme';
7-
import { BaseThemeType, mergeWithDefaultTheme, PartialTheme, Theme, BaseThemeTypes } from '../utils/themes/theme';
5+
import { mergeWithDefaultTheme, PartialTheme, Theme } from '../utils/themes/theme';
86
import { Domain } from '../utils/domain';
97
import { TooltipType, TooltipValueFormatter } from '../chart_types/xy_chart/utils/interactions';
108
import {
@@ -16,6 +14,7 @@ import {
1614
CursorUpdateListener,
1715
} from '../chart_types/xy_chart/store/chart_state';
1816
import { ScaleTypes } from '../utils/scales/scales';
17+
import { LIGHT_THEME } from '../utils/themes/light_theme';
1918

2019
export const DEFAULT_TOOLTIP_TYPE = TooltipType.VerticalCursor;
2120
export const DEFAULT_TOOLTIP_SNAP = true;
@@ -26,11 +25,6 @@ interface TooltipProps {
2625
headerFormatter?: TooltipValueFormatter;
2726
}
2827

29-
const THEMES: { [type: string]: Theme } = {
30-
[BaseThemeTypes.Light]: LIGHT_THEME,
31-
[BaseThemeTypes.Dark]: DARK_THEME,
32-
};
33-
3428
/**
3529
* Event used to syncronize cursors between Charts.
3630
*
@@ -61,16 +55,10 @@ export interface SettingSpecProps {
6155
* Full or partial theme to be merged with base
6256
*/
6357
theme?: Theme | PartialTheme;
64-
/**
65-
* Theme type used to get base theme
66-
*
67-
* @default `BaseThemeType.Light`
68-
*/
69-
baseThemeType?: BaseThemeType;
7058
/**
7159
* Full default theme to use as base
7260
*
73-
* @overrides `baseThemeType`
61+
* @default `LIGHT_THEME`
7462
*/
7563
baseTheme?: Theme;
7664
rendering: Rendering;
@@ -95,17 +83,15 @@ export interface SettingSpecProps {
9583
xDomain?: Domain | DomainRange;
9684
}
9785

98-
function getTheme(baseThemeType: BaseThemeType, theme?: Theme | PartialTheme, baseTheme?: Theme): Theme {
99-
const base = baseTheme ? baseTheme : THEMES[baseThemeType];
100-
86+
function getTheme(baseTheme?: Theme, theme?: Theme | PartialTheme): Theme {
87+
const base = baseTheme ? baseTheme : LIGHT_THEME;
10188
return theme ? mergeWithDefaultTheme(theme, base) : base;
10289
}
10390

10491
function updateChartStore(props: SettingSpecProps) {
10592
const {
10693
chartStore,
10794
theme,
108-
baseThemeType = BaseThemeTypes.Light,
10995
baseTheme,
11096
rotation,
11197
rendering,
@@ -132,7 +118,7 @@ function updateChartStore(props: SettingSpecProps) {
132118
return;
133119
}
134120

135-
chartStore.chartTheme = getTheme(baseThemeType, theme, baseTheme);
121+
chartStore.chartTheme = getTheme(baseTheme, theme);
136122
chartStore.chartRotation = rotation;
137123
chartStore.chartRendering = rendering;
138124
chartStore.animateData = animateData;
@@ -194,7 +180,6 @@ export class SettingsComponent extends PureComponent<SettingSpecProps> {
194180
animateData: true,
195181
showLegend: false,
196182
debug: false,
197-
baseThemeType: BaseThemeTypes.Light,
198183
tooltip: {
199184
type: DEFAULT_TOOLTIP_TYPE,
200185
snap: DEFAULT_TOOLTIP_SNAP,

src/utils/themes/theme.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ export interface Theme {
116116

117117
export type PartialTheme = RecursivePartial<Theme>;
118118

119-
export const BaseThemeTypes = Object.freeze({
120-
Light: 'light' as 'light',
121-
Dark: 'dark' as 'dark',
122-
});
123-
124-
export type BaseThemeType = typeof BaseThemeTypes.Dark | typeof BaseThemeTypes.Light;
125-
126119
export type DisplayValueStyle = TextStyle & {
127120
offsetX: number;
128121
offsetY: number;

stories/styling.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import {
1919
Position,
2020
ScaleType,
2121
Settings,
22-
BaseThemeTypes,
2322
LineSeriesStyle,
2423
TooltipType,
2524
RecursivePartial,
2625
Theme,
2726
LIGHT_THEME,
27+
DARK_THEME,
2828
} from '../src/';
2929
import * as TestDatasets from '../src/utils/data_samples/test_dataset';
3030
import { palettes } from '../src/utils/themes/colors';
@@ -393,7 +393,7 @@ storiesOf('Stylings', module)
393393
<Chart className={className}>
394394
<Settings
395395
theme={theme}
396-
baseThemeType={darkmode ? 'dark' : 'light'}
396+
baseTheme={darkmode ? DARK_THEME : LIGHT_THEME}
397397
debug={boolean('debug', false)}
398398
showLegend={true}
399399
legendPosition={Position.Right}
@@ -455,12 +455,7 @@ storiesOf('Stylings', module)
455455

456456
return (
457457
<Chart className="story-chart">
458-
<Settings
459-
showLegend
460-
theme={customPartialTheme}
461-
baseThemeType={BaseThemeTypes.Light}
462-
legendPosition={Position.Right}
463-
/>
458+
<Settings showLegend theme={customPartialTheme} baseTheme={LIGHT_THEME} legendPosition={Position.Right} />
464459
<Axis id={getAxisId('bottom')} position={Position.Bottom} title="Bottom axis" showOverlappingTicks={true} />
465460
<Axis
466461
id={getAxisId('left2')}

0 commit comments

Comments
 (0)