Skip to content

Commit

Permalink
fix(馃帹 Themes): Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
artalat committed Dec 16, 2018
1 parent d5b15c7 commit 2627b47
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 14 deletions.
Expand Up @@ -7,12 +7,21 @@ exports[`BlueBaseApp Snapshot BlueBaseApp component 1`] = `
`;

exports[`BlueBaseApp Snapshot BlueBaseApp component after complete rendering 1`] = `
<View>
<View>
<View
styles={Object {}}
>
<View
styles={Object {}}
>
<Text
style={
Array [
undefined,
Object {
"color": "rgba(0, 0, 0, 0.87)",
"fontFamily": "Roboto",
"fontSize": 16,
"fontWeight": "400",
},
undefined,
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/Theme/applyStyles.tsx
Expand Up @@ -47,7 +47,7 @@ export const applyStyles = (
<ThemeContext.Consumer children={(args?: { theme: ThemeValue }) => {

if(!args) {
return;
return React.createElement(Component, { ...rest });
}

const { theme } = args;
Expand Down
37 changes: 34 additions & 3 deletions src/registries/ComponentRegistry.ts
Expand Up @@ -3,8 +3,9 @@ import {
BlueBaseModuleRegistryInputItem,
BlueBaseModuleRegistryItem,
} from './BlueBaseModuleRegistry';
import { Thunk, getDefiniteBlueBaseModule, isBlueBaseModule } from '../utils';
import { MaybeThunk, Thunk, getDefiniteBlueBaseModule, isBlueBaseModule } from '../utils';
import { BlueBase } from '../BlueBase';
import { ComponentStyles, applyStyles } from '../models';
import { ItemCollection } from './Registry';
import Loadable from 'react-loadable';
import { ReactLoadableLoading } from '../components';
Expand All @@ -28,6 +29,7 @@ export interface ComponentSource {
interface ComponentRegistryItemExtras {
hocs: Array<ComponentRegistryHocItem | ComponentRegistryHocItemWithArgs>,
source: ComponentSource,
styles: MaybeThunk<ComponentStyles>,
isAsync: boolean,
}

Expand Down Expand Up @@ -73,12 +75,15 @@ export class ComponentRegistry extends BlueBaseModuleRegistry<ComponentRegistryI
throw Error(`Could not resolve any of the following components: [${keys.join(', ')}].`);
}

const rawComponent = item.value.isAsync
let rawComponent = item.value.isAsync
? Loadable({
loader: () => item.value,
loading: ReactLoadableLoading,
})
: item.value.module;
: item.value.module as React.ComponentType<any>;

// Add withStyles HOC
rawComponent = applyStyles(item.key, rawComponent, item.styles);

const hocs = item.hocs
.map(hoc => (Array.isArray(hoc) ? hoc[0](hoc[1]) : hoc));
Expand Down Expand Up @@ -108,6 +113,32 @@ export class ComponentRegistry extends BlueBaseModuleRegistry<ComponentRegistryI
// // TODO:
// }

// TODO: Add docs
public setStyles(key: string, styles: MaybeThunk<ComponentStyles>) {
const item = this.get(key);

if (!item) {
throw Error(
`Cannot set styles "${key}" component. Reason: Component not found.`
);
}

item.styles = styles;

this.data = this.data.set(key, item);
}

// TODO: Add docs
public getStyles(key: string): MaybeThunk<ComponentStyles> | undefined {
const item = this.get(key);

if (!item) {
return;
}

return item.styles;
}

protected createItem(key: string, partial: any): ComponentRegistryItem {

const value = getDefiniteBlueBaseModule(partial.value);
Expand Down
2 changes: 1 addition & 1 deletion src/registries/__stories__/ComponentRegistry.stories.tsx
@@ -1,4 +1,4 @@
import { BlueBase, BlueBaseConsumer } from '../../..';
import { BlueBase, BlueBaseConsumer } from '../../';
import { Text, View } from 'react-native';
import React from 'react';
import storiesOf from '@bluebase/storybook-addon';
Expand Down
2 changes: 1 addition & 1 deletion src/registries/__stories__/ThemeContext.stories.tsx
@@ -1,5 +1,5 @@
// tslint:disable:no-console
import { BlueBase, BlueBaseConsumer } from '../../..';
import { BlueBase, BlueBaseConsumer } from '../../';
import React from 'react';
import { ThemeDemo } from './ThemeDemo';
import { ThemePicker } from './ThemePicker';
Expand Down
2 changes: 1 addition & 1 deletion src/registries/__stories__/ThemePicker.tsx
@@ -1,4 +1,4 @@
import { BlueBase, BlueBaseContext } from '../../..';
import { BlueBase, BlueBaseContext } from '../../';
import { ThemeContext, ThemeContextData } from '../../themes';
import { Picker } from 'react-native';
import React from 'react';
Expand Down
2 changes: 1 addition & 1 deletion src/registries/__stories__/ThemedCard.tsx
@@ -1,4 +1,4 @@
import { BlueBase, BlueBaseContext } from '../../..';
import { BlueBase, BlueBaseContext } from '../../';
import { TextStyle, ViewStyle } from 'react-native';
import { MaybeThunk } from '../../utils';
import React from 'react';
Expand Down
7 changes: 4 additions & 3 deletions src/registries/__tests__/ComponentRegisty.test.tsx
Expand Up @@ -67,10 +67,11 @@ describe('ComponentRegistry', () => {

it('should resolve a Promised component', async (done) => {
const BB = new BlueBase();
const Components = new ComponentRegistry(BB);
await BB.boot();
// const Components = new ComponentRegistry(BB);

await Components.register('Foo', { value: Promise.resolve(Text) as any });
const Foo = Components.resolve('Foo');
await BB.Components.register('Foo', { value: Promise.resolve(Text) as any });
const Foo = BB.Components.resolve('Foo');

const rendered = TestRenderer.create(<Foo>Some text</Foo>);
const foo = rendered.root.findByType(ReactLoadableLoading);
Expand Down

0 comments on commit 2627b47

Please sign in to comment.