Skip to content

Commit

Permalink
Fix server-side rendering (#8)
Browse files Browse the repository at this point in the history
We can't use DOM APIs if we expect this to be server renderable, so we'll
do the "array-of-children" thing as much as it bothers me...
  • Loading branch information
toddself authored Jun 23, 2017
1 parent a57bc89 commit ec5c0f4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
7 changes: 6 additions & 1 deletion label-el.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ const html = require('bel')

module.exports = label

function label (label, labelOpts) {
function label (label, labelOpts, children) {
var classes = ['db', 'mt3', 'ttu', 'lh-copy']
var style = 'text-indent: 4px;'

if (!Array.isArray(children)) {
children = [children]
}

labelOpts = labelOpts || {}

classes = applyOpts.classes(classes, labelOpts)
style = applyOpts.style(style, labelOpts)

var $label = html`<label class="${classes.join(' ')}" style="${style}">
${label}<br>
${children.map((child) => child)}
</label>`

return applyOpts.opts($label, labelOpts)
Expand Down
3 changes: 1 addition & 2 deletions labeled-input-el.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ const inputEl = require('./input-el')
const labelEl = require('./label-el')

function labeledInput (text, type, handler, value, inputOpts, labelOpts) {
const $label = labelEl(text, labelOpts)
const $input = inputEl(type, handler, value, inputOpts)
$label.appendChild($input)
const $label = labelEl(text, labelOpts, $input)
return $label
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Form elements for showrunner",
"main": "form-elements.js",
"scripts": {
"test": "standard && npm run dep && browserify test/*.spec.js | tape-run",
"test": "standard && npm run dep && node test/ssr-rendering.js && browserify test/*.spec.js | tape-run",
"coverage": "npm run test && node test-harness.js",
"start": "bankai start standalone.js --no-uglify --debug",
"dep": "dependency-check . && dependency-check . --unused --no-dev -i envify -i yo-yoify -i babelify -i babel-preset-env -i sheetify"
Expand Down
14 changes: 14 additions & 0 deletions test/ssr-rendering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const test = require('tape')
const els = require('../')

test('noooooooooooooooode', (t) => {
// this way we get a failure if we add/remove an el without making a test
const keys = Object.keys(els)
t.plan(keys.length)

t.ok(els.button('test', null), 'button renders')
t.ok(els.label('test'), 'label renders')
t.ok(els.input('text', null, ''), 'input renders')
t.ok(els.labeledInput('test', 'text', null, ''), 'labelled input renders')
t.ok(els.error().render(), 'error renders')
})

0 comments on commit ec5c0f4

Please sign in to comment.