Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Add optional setWidth option and write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yutakahoulette committed Feb 13, 2017
1 parent 34349a1 commit 26d4784
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
10 changes: 8 additions & 2 deletions tabswap/index.es6
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ const labels = options => {
options = R.merge({
names: []
, active$: flyd.stream(0)
, setWidth: false
}, options)


const width = options.setWidth ? 100 / options.names.length + '%' : false

return h('div', {
attrs: {
'data-ff-tabswap': 'active:' + options.active$()
, 'data-ff-tabswap-labels': true
, 'data-ff-tabswap-labels-count': options.names.length
}
},
map(labelSingle(options.active$), options.names)
map(labelSingle(options.active$, width), options.names)
)
}

const labelSingle = active$ => (name, idx) =>
const labelSingle = (active$, width) => (name, idx) =>
h('div', {
attrs: {'data-ff-tabswap-label-wrapper': true}
, style: {width: width ? width : ''}
}, [
h("a", {
on: {click: [active$, idx]}
Expand Down
23 changes: 18 additions & 5 deletions tabswap/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const h = require('snabbdom/h')
const tabswap = require('../index.es6')
const render = require('flimflam-render')

function init() {
function init(labelOptions) {
const state = {
active$: flyd.stream(1)
}
const view = state => h('div', [
tabswap.labels({
tabswap.labels(R.merge({
names: ['a', 'b']
, active$: state.active$
})
}, labelOptions))
, tabswap.content({
sections: ['content a', 'content b']
, active$: state.active$
Expand All @@ -30,7 +30,6 @@ test('it renders the labels', t=> {
t.plan(1)
const streams = init()
const labels = streams.container.querySelectorAll('[data-ff-tabswap-label]')
document.body.appendChild(streams.container)
t.strictEqual(labels.length, 2)
})

Expand All @@ -51,7 +50,6 @@ test('it renders the text content', t=> {
test('on click of a label, it swaps data states of labels', t=> {
t.plan(1)
const streams = init()
const content = streams.container.textContent
streams.container.querySelectorAll('[data-ff-tabswap-label]')[1].click()
const states = R.map(
node => node.getAttribute('data-ff-tabswap-label')
Expand All @@ -71,3 +69,18 @@ test('on click of a label, it swaps data states of content', t=> {
)
t.deepEqual(states, ['inactive', 'active'])
})

test('it sets the width of the label wrappers when setWidth option is true', t=> {
t.plan(1)
const streams = init({setWidth: true})
const label = streams.container.querySelector('[data-ff-tabswap-label-wrapper]')
t.strictEqual(label.style.width, '50%')
})

test('it does not set the width of the label wrappers when setWidth option is false', t=> {
t.plan(1)
const streams = init()
const label = streams.container.querySelector('[data-ff-tabswap-label-wrapper]')
t.strictEqual(label.style.width, '')
})

0 comments on commit 26d4784

Please sign in to comment.