Skip to content

Commit ed171d4

Browse files
add option to wrap components per registry
1 parent 763e7cd commit ed171d4

5 files changed

Lines changed: 52 additions & 19 deletions

File tree

src/App.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { sideBySideDemo } from './demos/sideBySide';
1010
import { longContentDemo } from './demos/longContent';
1111
import { inputsDemo } from './demos/inputs';
1212

13-
const widgets = new Registry('widgets');
13+
const widgets = new Registry('widgets', {componentWrapper: DemoWrapper});
1414
widgets
1515
.registerAsGrid('Links', 300, linksDemo)
1616
.registerAsTwoColumnTable('Forms', formsDemo)
@@ -44,4 +44,16 @@ export class App extends React.Component {
4444
private onBrandSelect(brand: string) {
4545
console.log('selected brand', brand);
4646
}
47+
}
48+
49+
interface Props {
50+
children: JSX.Element | JSX.Element[];
51+
}
52+
53+
function DemoWrapper({children}: Props) {
54+
return (
55+
<div className="component-demo-local-wrapper">
56+
{children}
57+
</div>
58+
);
4759
}

src/components/registry/Registry.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ import { TabsLayout } from '../layouts/TabsLayout';
66
import { LayoutProps } from '../layouts/LayoutProps';
77
import { LabelInstanceTableLayout } from '../layouts/LabelInstanceTableLayout';
88
import { SingleItemLayout } from '../layouts/SingleItemLayout';
9+
import { wrapComponent } from './componentWrapper';
10+
11+
export interface RegistryConfig {
12+
componentWrapper?: React.ComponentType;
13+
}
914

1015
class Registry {
11-
name: string;
12-
_usedNames: string[] = [];
16+
private readonly config?: RegistryConfig;
17+
private readonly usedNames: string[] = [];
18+
private readonly demoEntries: DemoEntry[] = [];
1319

14-
_demoEntries: DemoEntry[] = [];
15-
_currentDemo?: DemoEntry;
20+
private currentDemo?: DemoEntry;
1621

17-
constructor(name: string) {
22+
readonly name: string;
23+
24+
constructor(name: string, config?: RegistryConfig) {
1825
this.name = name;
26+
this.config = config;
1927
}
2028

2129
registerAsGrid(name: string, minWidth: number, componentRegistrator: (registry: Registry) => void) {
@@ -49,27 +57,27 @@ class Registry {
4957
urlPrefix: string = '',
5058
layoutOpts: object = {}) {
5159

52-
if (this._usedNames.indexOf(name) !== -1) {
60+
if (this.usedNames.indexOf(name) !== -1) {
5361
throw new Error(`name ${name} was already used`);
5462
}
5563

56-
this._currentDemo = new DemoEntry(name, layoutComponent, urlPrefix, layoutOpts);
57-
this._demoEntries.push(this._currentDemo);
64+
this.currentDemo = new DemoEntry(name, layoutComponent, urlPrefix, layoutOpts);
65+
this.demoEntries.push(this.currentDemo);
5866

59-
this._usedNames.push(name);
67+
this.usedNames.push(name);
6068

6169
componentRegistrator(this);
6270

6371
return this;
6472
}
6573

6674
get names(): string[] {
67-
return this._usedNames;
75+
return this.usedNames;
6876
}
6977

7078
description(markdown: string) {
71-
if (this._currentDemo) {
72-
this._currentDemo.description(markdown);
79+
if (this.currentDemo) {
80+
this.currentDemo.description(markdown);
7381
} else {
7482
throw new Error('call register method prior setting the description');
7583
}
@@ -78,8 +86,12 @@ class Registry {
7886
}
7987

8088
add(title: string, component: React.ComponentType, description: string = '') {
81-
if (this._currentDemo) {
82-
this._currentDemo.add(title, description, component);
89+
const componentToRegister = this.config && this.config.componentWrapper ?
90+
wrapComponent(this.config.componentWrapper, component) :
91+
component;
92+
93+
if (this.currentDemo) {
94+
this.currentDemo.add(title, description, componentToRegister);
8395
} else {
8496
throw new Error('call register method prior adding elements');
8597
}
@@ -88,14 +100,14 @@ class Registry {
88100
}
89101

90102
firstMiniAppByUrl(url: string): DemoEntry | null {
91-
const byUrl = this._demoEntries
103+
const byUrl = this.demoEntries
92104
.filter(entry => entry.isMiniApp() && url.startsWith(entry.urlPrefix));
93105

94106
return byUrl.length > 0 ? byUrl[0] : null;
95107
}
96108

97109
findByName(name: string): DemoEntry | null {
98-
const byName = this._demoEntries
110+
const byName = this.demoEntries
99111
.filter(entry => entry.name === name);
100112

101113
if (byName.length === 0) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as React from 'react';
2+
3+
export function wrapComponent(Wrapper: React.ComponentType, Component: React.ComponentType) {
4+
return () => (
5+
<Wrapper>
6+
<Component/>
7+
</Wrapper>
8+
);
9+
}

src/components/viewer/ComponentViewer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
4545
this.state = this.stateFromUrl();
4646
this.hotKeyBoundActions = {
4747
'Alt F': this.onFullScreenToggle,
48-
...this.dropDownKeyBoundActions()};
48+
...this.dropDownKeyBoundActions()
49+
};
4950
}
5051

5152
render() {

tslint.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"max-line-length": [ true, 120 ],
2626
"member-ordering": [
2727
true,
28-
"public-before-private",
2928
"static-before-instance",
3029
"variables-before-functions"
3130
],

0 commit comments

Comments
 (0)