Skip to content

Commit

Permalink
fix: fix getting listbox theme (#1156)
Browse files Browse the repository at this point in the history
* fix: fix getting listbox theme

* fix: add base settings and loadTheme func

* chore: add missing file

* chore: update unit tests

* chore: update baseline

* chore: add more words to long title

* chore: update last baseline

* chore: update last baseline, again

---------

Co-authored-by: caele <tsm@qlik.com>
  • Loading branch information
quanho and Caele committed Mar 17, 2023
1 parent e8a17b5 commit ef75133
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 17 deletions.
9 changes: 6 additions & 3 deletions apis/nucleus/src/app-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ const timed = (t, v) =>

const LOAD_THEME_TIMEOUT = 5000;

export default function appTheme({ themes = [], root } = {}) {
export default function appTheme({ themes = [], loadTheme, root } = {}) {
const wrappedTheme = themeFn();

const setTheme = async (themeId) => {
const found = themes.filter((t) => t.id === themeId)[0];
let found = themes.filter((t) => t.id === themeId)[0];
let muiTheme = themeId === 'dark' ? 'dark' : 'light';
if (!found && loadTheme) {
found = { load: loadTheme };
}
if (found && found.load) {
try {
const raw = await Promise.race([found.load(), timed(LOAD_THEME_TIMEOUT, { __timedOut: true })]);
const raw = await Promise.race([found.load(themeId), timed(LOAD_THEME_TIMEOUT, { __timedOut: true })]);
if (raw.__timedOut) {
if (__NEBULA_DEV__) {
console.warn(`Timeout when loading theme '${themeId}'`); // eslint-disable-line no-console
Expand Down
1 change: 1 addition & 0 deletions apis/nucleus/src/components/listbox/ListBoxInline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const Title = styled(Typography)(({ theme }) => ({
color: theme.listBox?.title?.main?.color,
fontSize: theme.listBox?.title?.main?.fontSize,
fontFamily: theme.listBox?.title?.main?.fontFamily,
fontWeight: theme.listBox?.title?.main?.fontWeight || 'bold',
}));

function ListBoxInline({ options, layout }) {
Expand Down
17 changes: 9 additions & 8 deletions apis/nucleus/src/components/listbox/assets/addListboxTheme.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const addListboxTheme = (themeApi) => {
const getListboxStyle = (prop) => themeApi.getStyle('object', '', `listBox.${prop}`);
const getListboxStyle = (path, prop) => themeApi.getStyle('object.listBox', path, prop);

return {
backgroundColor: getListboxStyle('backgroundColor'),
backgroundColor: getListboxStyle('', 'backgroundColor'),
title: {
main: {
color: getListboxStyle('title.main.color'),
fontSize: getListboxStyle('title.main.fontSize'),
fontFamily: getListboxStyle('title.main.fontFamily'),
color: getListboxStyle('title.main', 'color'),
fontSize: getListboxStyle('title.main', 'fontSize'),
fontFamily: getListboxStyle('title.main', 'fontFamily'),
fontWeight: getListboxStyle('title.main', 'fontWeight'),
},
},
content: {
color: getListboxStyle('content.color'),
fontSize: getListboxStyle('content.fontSize'),
fontFamily: getListboxStyle('content.fontFamily'),
color: getListboxStyle('content', 'color'),
fontSize: getListboxStyle('content', 'fontSize'),
fontFamily: getListboxStyle('content', 'fontFamily'),
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default function ListBoxSearch({
sx={[
{
border: 'none',
fontSize: 14,
borderRadius: 0,
backgroundColor: 'transparent',
'& fieldset': {
Expand Down
2 changes: 2 additions & 0 deletions apis/nucleus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const mergeArray = (a1 = [], a2 = []) =>
const mergeConfigs = (base, c) => ({
context: mergeObj(base.context, c.context),
load: c.load || base.load,
loadTheme: c.loadTheme || base.loadTheme,
snapshot: {
...(c.snapshot || base.snapshot),
},
Expand Down Expand Up @@ -175,6 +176,7 @@ function nuked(configuration = {}) {

const appTheme = appThemeFn({
themes: configuration.themes,
loadTheme: configuration.loadTheme,
root,
});
currentContext.themeApi = appTheme.externalAPI;
Expand Down
18 changes: 14 additions & 4 deletions apis/theme/src/__tests__/set-theme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import create from '../set-theme';
import base from '../themes/base.json';
import light from '../themes/light.json';
import dark from '../themes/dark.json';
import baseIn from '../themes/base_inherit.json';

jest.mock('extend');
jest.mock('../themes/base.json', () => ({
Expand Down Expand Up @@ -36,17 +37,26 @@ describe('set theme', () => {
palettes: {},
});

create({}, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, light]);
create({ _inherit: false }, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, light, {}]);
});

test('should extend from dark theme when type is dark', () => {
extendMock.mockReturnValue({
palettes: {},
});

create({ type: 'dark' }, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, dark]);
create({ type: 'dark', _inherit: false }, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, dark, {}]);
});

test('should extend and add base inheritance', () => {
extendMock.mockReturnValue({
palettes: {},
});

create({}, resolveMock);
expect(extendMock.mock.calls[0]).toEqual([true, {}, base, light, baseIn]);
});

test('should not extend scales and palette arrays', () => {
Expand Down
8 changes: 7 additions & 1 deletion apis/theme/src/set-theme.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/* eslint no-underscore-dangle:0 */
import extend from 'extend';

import baseRawJSON from './themes/base.json';
import lightRawJSON from './themes/light.json';
import darkRawJSON from './themes/dark.json';
import baseInheritRawJSON from './themes/base_inherit.json';

export default function setTheme(t, resolve) {
const colorRawJSON = t.type === 'dark' ? darkRawJSON : lightRawJSON;
const root = extend(true, {}, baseRawJSON, colorRawJSON);
let baseInherit = baseInheritRawJSON;
if (t._inherit === false || t._inherit === 'false') {
baseInherit = {};
}
const root = extend(true, {}, baseRawJSON, colorRawJSON, baseInherit);
// avoid merging known array objects as it could cause issues if they are of different types (pyramid vs class) or length
const rawThemeJSON = extend(true, {}, root, { scales: null, palettes: { data: null, ui: null } }, t);
if (!rawThemeJSON.palettes.data || !rawThemeJSON.palettes.data.length) {
Expand Down
18 changes: 18 additions & 0 deletions apis/theme/src/themes/base_inherit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"object": {
"listBox": {
"backgroundColor": "#ffffff",
"title": {
"main": {
"color": "#404040",
"fontSize": "14px",
"fontWeight": "bold"
}
},
"content": {
"color": "#404040",
"fontSize": "12px"
}
}
}
}
2 changes: 1 addition & 1 deletion test/rendering/listbox/listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
sc = { toolbar: false };
break;
case 'longTitle':
sc = { title: 'Alpha long title' };
sc = { title: 'Alpha long title that needs more words' };
break;
default:
throw new Error('Invalid test scenario', s);
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.
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.
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.
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.
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.

0 comments on commit ef75133

Please sign in to comment.