Skip to content

Commit 2de1804

Browse files
committed
feat: create Component interface
1 parent 5ef8a08 commit 2de1804

File tree

3 files changed

+106
-13
lines changed

3 files changed

+106
-13
lines changed

.storybook/component.stories.tsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
const {action} = require('@storybook/addon-actions');
4+
const {linkTo} = require('@storybook/addon-links');
5+
import {create} from '../index';
6+
import {addon as addonRule} from '../addon/rule';
7+
import {addon as addonCache} from '../addon/cache';
8+
import {addon as addonComponent} from '../addon/component';
9+
const renderer = create({h});
10+
addonRule(renderer);
11+
addonCache(renderer);
12+
addonComponent(renderer);
13+
const {Component} = renderer;
14+
15+
16+
class CssTest extends Component {
17+
static css = {
18+
border: '1px solid red'
19+
};
20+
21+
render () {
22+
return <div>Hello there</div>;
23+
}
24+
}
25+
26+
class Dynamic extends Component {
27+
static css = {
28+
border: '1px solid red'
29+
};
30+
31+
css() {
32+
return {
33+
color: 'green'
34+
};
35+
}
36+
37+
render () {
38+
return <div>Hello there</div>;
39+
}
40+
}
41+
42+
class Static extends Component {
43+
static css = {
44+
border: '1px dashed blue',
45+
};
46+
47+
render () {
48+
return <div>Hello there</div>;
49+
}
50+
}
51+
52+
class Static2 extends Component {
53+
static css = {
54+
border: '1px dashed blue',
55+
};
56+
57+
css () {
58+
return {
59+
color: this.props.color
60+
};
61+
}
62+
63+
render () {
64+
return <div>Hello there</div>;
65+
}
66+
}
67+
68+
storiesOf('Component', module)
69+
.add('Default', () =>
70+
h(CssTest as any)
71+
)
72+
.add('Dynamic styles', () =>
73+
h(Dynamic as any)
74+
)
75+
.add('Static decorator', () =>
76+
h(Static as any)
77+
)
78+
.add('Static dynamic styles', () =>
79+
h(Static2 as any, {color: 'blue'})
80+
)

addon/component.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var React = require('react');
4+
var Component = React.Component;
5+
var transformComponentStatic = require('./util/transformComponentStatic');
6+
var transformComponentDynamic = require('./util/transformComponentDynamic');
7+
8+
exports.addon = function (renderer) {
9+
if (process.env.NODE_ENV !== 'production') {
10+
require('./__dev__/warnOnMissingDependencies')('component', renderer, ['rule', 'cache']);
11+
}
12+
13+
function CssComponent (props, context) {
14+
Component.call(this, props, context);
15+
16+
var Comp = this.constructor;
17+
18+
if (Comp.css) transformComponentStatic(renderer, Comp.prototype, Comp.css);
19+
if (this.css) transformComponentDynamic(renderer, Comp, this.css.bind(this));
20+
}
21+
22+
CssComponent.prototype = Object.create(Component.prototype);
23+
CssComponent.prototype.constructor = CssComponent;
24+
25+
renderer.Component = CssComponent;
26+
};

addon/util/transformCssComponent.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)