Skip to content

Commit

Permalink
handle composition with createComponent using combineRules
Browse files Browse the repository at this point in the history
  • Loading branch information
wcastand committed May 29, 2017
1 parent d0ebbb7 commit a102fbd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/__snapshots__/createComponent.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,29 @@ Object {
"text": "test",
}
`;

exports[`createComponent should compose 1`] = `
Object {
"children": undefined,
"data": Object {
"component": [Function],
},
"elm": undefined,
"key": undefined,
"sel": "div",
"text": "test",
}
`;

exports[`createComponent should compose and overwrite rule 1`] = `
Object {
"children": undefined,
"data": Object {
"component": [Function],
},
"elm": undefined,
"key": undefined,
"sel": "div",
"text": "test",
}
`;
9 changes: 8 additions & 1 deletion src/createComponent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { h } from 'snabbdom'
import { combineRules } from 'fela'

export default function createComponent(rules, selector = 'div') {
return function(props, children) {
const c = arguments.length === 1 ? props : children
const p = arguments.length === 1
? { component: rules }
: Object.assign({}, props, { component: rules })
return h(selector, p, c)
if (typeof selector === 'string') {
return h(selector, p, c)
}
const sel = selector(p, c)
const mergedRules = combineRules(sel.data.component, rules)
const pp = Object.assign({}, p, { component: mergedRules })
return h(sel.sel, pp, c)
}
}
26 changes: 25 additions & 1 deletion src/createComponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@ test('createComponent should return a function', () =>
expect(typeof createComponent(() => ({}))).toBe('function'))

test('createComponent second call should return an object', () => {
const Div = createComponent(() => ({}))
const Div = createComponent(() => ({ color: 'red' }))
const vdom = Div('test')
const style = vdom.data.component({})

expect(typeof vdom).toBe('object')
expect(style).toEqual({ color: 'red' })

expect(vdom).toMatchSnapshot()
})
test('createComponent should compose', () => {
const Div = createComponent(() => ({ backgroundColor: 'red' }))
const RedBoldDiv = createComponent(() => ({ fontWeight: 'bold' }), Div)
const vdom = RedBoldDiv('test')
const style = vdom.data.component({})

expect(vdom).toMatchSnapshot()
expect(style).toEqual({ backgroundColor: 'red', fontWeight: 'bold' })
})
test('createComponent should compose and overwrite rule', () => {
const RedDiv = createComponent(() => ({ backgroundColor: 'red' }))
const YellowDiv = createComponent(
() => ({ fontWeight: 'bold', backgroundColor: 'yellow' }),
RedDiv,
)
const vdom = YellowDiv('test')
const style = vdom.data.component({})

expect(vdom).toMatchSnapshot()
expect(style).toEqual({ backgroundColor: 'yellow', fontWeight: 'bold' })
})

test('createComponent should have a function in component props', () => {
Expand Down

0 comments on commit a102fbd

Please sign in to comment.