Skip to content

Commit a265c78

Browse files
global registries config
1 parent 53ac0b7 commit a265c78

4 files changed

Lines changed: 35 additions & 31 deletions

File tree

src/App.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
import * as React from 'react';
22

3-
import { Registry } from './components';
3+
import { Registries } from './components';
44
import { ComponentViewer } from './components';
55
import { buttonsDemo } from './demos/buttons';
66
import { linksDemo } from './demos/links';
77
import { profileScreenDemo } from './demos/profileScreen';
88
import { formsDemo } from './demos/forms';
99
import { sideBySideDemo } from './demos/sideBySide';
10-
import { longContentDemo } from './demos/longContent';
1110
import { inputsDemo } from './demos/inputs';
1211
import { WrapperProps } from './components/registry/componentWrapper';
1312

14-
const widgets = new Registry('widgets', {componentWrapper: DemoWrapper});
15-
widgets
13+
const registries = new Registries({componentWrapper: DemoWrapper});
14+
registries.add('widgets')
1615
.registerAsGrid('Links', 300, linksDemo)
1716
.registerAsTwoColumnTable('Forms', formsDemo)
1817
.registerAsTwoColumnTable('Buttons', buttonsDemo)
1918
.registerAsRows('Inputs', inputsDemo);
2019

21-
const layouts = new Registry('layouts');
22-
layouts
20+
registries.add('layouts')
2321
.registerSingle('Side by Side', sideBySideDemo);
2422

25-
const endToEnd = new Registry('end to end');
26-
endToEnd.registerAsTabs('Long Content', longContentDemo)
23+
registries.add('end to end')
2724
.registerSingle('Single Screen', profileScreenDemo)
2825
.registerAsMiniApp('Single Screen mini app', '/app', profileScreenDemo);
2926

3027
export class App extends React.Component {
3128
render() {
3229
return (
3330
<ComponentViewer
34-
registries={[widgets, layouts, endToEnd]}
31+
registries={registries}
3532
dropDown={{
3633
label: 'Brand',
3734
items: [

src/components/registry/Registries.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import { Registry } from '../';
2+
import { RegistryConfig } from './Registry';
23

34
export class Registries {
4-
public registries: Registry[];
5-
private _names: string[];
5+
private readonly _names: string[];
6+
private readonly _config: RegistryConfig;
67

7-
constructor(registries: Registry[]) {
8-
this.registries = registries;
9-
this._names = registries.map(r => r.name);
8+
public readonly registries: Registry[];
9+
10+
constructor(config?: RegistryConfig) {
11+
this._config = config || {};
12+
this.registries = [];
13+
this._names = [];
14+
}
15+
16+
add(name: string, config?: RegistryConfig) {
17+
const registry = new Registry(name, {...this._config, ...config});
18+
this.registries.push(registry);
19+
this._names.push(name);
20+
21+
return registry;
1022
}
1123

1224
get first() {

src/components/viewer/ComponentViewer.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ import { VisualizedActions } from '../actions/VisualizedActions';
2929
import './ComponentViewer.css';
3030

3131
export interface Props {
32-
registries: Registry[];
32+
registries: Registries;
3333
dropDown?: ComponentViewerDropDown;
3434
}
3535

3636
class ComponentViewer extends Component<Props, ComponentViewerState> {
37-
private readonly registries: Registries;
3837
private readonly hotKeyBoundActions: HotKeyBoundActions;
3938

4039
private stateCreator: ComponentViewerStateCreator;
@@ -44,8 +43,7 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
4443

4544
const {registries} = this.props;
4645

47-
this.registries = new Registries(registries);
48-
this.stateCreator = new ComponentViewerStateCreator(this.registries);
46+
this.stateCreator = new ComponentViewerStateCreator(registries);
4947

5048
this.state = this.stateFromUrl();
5149
this.hotKeyBoundActions = {
@@ -81,7 +79,7 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
8179
}
8280

8381
renderSelectionPanelAndDemo(demoEntry: DemoEntry | null) {
84-
const {dropDown} = this.props;
82+
const {registries, dropDown} = this.props;
8583

8684
const {
8785
registryName,
@@ -95,7 +93,7 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
9593
<div className="rcv-component-viewer">
9694
<div className="rcv-registry-selection-panel">
9795
<RegistrySelection
98-
names={this.registries.names}
96+
names={registries.names}
9997
selectedName={registryName}
10098
onSelect={this.selectRegistry}
10199
/>
@@ -296,8 +294,9 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
296294
}
297295

298296
private findSelectedRegistry() {
297+
const {registries} = this.props;
299298
const {registryName} = this.state;
300-
return this.registries.registryByName(registryName);
299+
return registries.registryByName(registryName);
301300
}
302301

303302
private get demoNames() {

src/components/viewer/ComponentViewerStateCreator.test.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import * as React from 'react';
22

3-
import { Registries, Registry } from '../';
3+
import { Registries } from '../';
44
import { ComponentViewerStateCreator } from './ComponentViewerStateCreator';
55

66
describe('ComponentViewerStateCreator', () => {
7-
let registryA: Registry;
8-
let registryB: Registry ;
9-
107
let registries: Registries;
118

129
beforeAll(() => {
13-
registryA = new Registry('core');
14-
registryA.registerSingle('FirstC', (registry => registry.add('title-c-a', () => <div/>)));
15-
16-
registryB = new Registry('widgets');
17-
registryA.registerSingle('FirstW', (registry => registry.add('title-w-a', () => <div/>)));
10+
registries = new Registries();
11+
registries.add('core')
12+
.registerSingle('FirstC', (registry => registry.add('title-c-a', () => <div/>)));
13+
registries.add('widgets')
14+
.registerSingle('FirstW', (registry => registry.add('title-w-a', () => <div/>)));
1815

19-
registries = new Registries([registryA, registryB]);
2016
});
2117

2218
it('should pick the first registry, first demo and demo entry if url has no query params', () => {

0 commit comments

Comments
 (0)