Skip to content

Commit 834fd9b

Browse files
authored
allow set forced theme prop for component using ThemeManager (#742)
1 parent f62b137 commit 834fd9b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/commons/__tests__/modifiers.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ describe('Modifiers', () => {
329329
});
330330

331331
describe('getThemeProps', () => {
332+
beforeEach(() => {
333+
ThemeManager.setComponentTheme('SampleComponent', undefined);
334+
ThemeManager.setComponentForcedTheme('SampleComponent', undefined);
335+
});
336+
332337
it('should return props values from the Theme Manager if were defined', () => {
333338
ThemeManager.setComponentTheme('SampleComponent', {prop1: 'themeValue'});
334339
expect(uut.getThemeProps.call(SampleComponent, {})).toEqual({prop1: 'themeValue'});
@@ -349,5 +354,10 @@ describe('Modifiers', () => {
349354
expect(uut.getThemeProps.call(SampleComponent, {test: true})).toEqual({prop1: 'yes', test: true});
350355
expect(uut.getThemeProps.call(SampleComponent, {test: false})).toEqual({prop1: 'no', test: false});
351356
});
357+
358+
it('should prioritize forced theme props over user props', () => {
359+
ThemeManager.setComponentForcedTheme('SampleComponent', props => ({foo: 'forced'}));
360+
expect(uut.getThemeProps.call(SampleComponent, {foo: 'user-value', other: 'other'})).toEqual({foo: 'forced', other: 'other'});
361+
});
352362
});
353363
});

src/commons/modifiers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,21 @@ export function extractOwnProps(props: Dictionary<any>, ignoreProps: string[]) {
272272
export function getThemeProps(props = this.props, context = this.context) {
273273
//@ts-ignore
274274
const componentName = this.displayName || this.constructor.displayName || this.constructor.name;
275+
275276
let themeProps;
276277
if (_.isFunction(ThemeManager.components[componentName])) {
277278
themeProps = ThemeManager.components[componentName](props, context);
278279
} else {
279280
themeProps = ThemeManager.components[componentName];
280281
}
281-
return {...themeProps, ...props};
282+
283+
let forcedThemeProps;
284+
if (_.isFunction(ThemeManager.forcedThemeComponents[componentName])) {
285+
forcedThemeProps = ThemeManager.forcedThemeComponents[componentName](props, context);
286+
} else {
287+
forcedThemeProps = ThemeManager.forcedThemeComponents[componentName];
288+
}
289+
return {...themeProps, ...props, ...forcedThemeProps};
282290
}
283291

284292
export function generateModifiersStyle(options = {

src/style/themeManager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export class ThemeManager {
2020
} as Extendable
2121
};
2222

23+
forcedTheme = {
24+
components: {} as Extendable
25+
}
26+
2327
setTheme(overrides: Dictionary<string>) {
2428
this.theme = _.merge(this.theme, overrides);
2529
}
@@ -49,10 +53,22 @@ export class ThemeManager {
4953
}
5054
}
5155

56+
setComponentForcedTheme(componentName: string, overrides: Dictionary<string> | Function) {
57+
if (_.isFunction(overrides)) {
58+
this.forcedTheme.components[componentName] = overrides;
59+
} else {
60+
this.forcedTheme.components[componentName] = _.cloneDeep(overrides);
61+
}
62+
}
63+
5264
get components() {
5365
return this.theme.components;
5466
}
5567

68+
get forcedThemeComponents() {
69+
return this.forcedTheme.components;
70+
}
71+
5672
get primaryColor() {
5773
return this.theme.primaryColor;
5874
}

0 commit comments

Comments
 (0)