From 48ff1f9b4355e90cec7668e48e34f0c548639260 Mon Sep 17 00:00:00 2001 From: foxdonut Date: Mon, 9 Jul 2018 18:39:17 -0400 Subject: [PATCH] Update --- README.md | 473 +++++++++++++++++++++++++++++++++++++++++++-- src/index.js | 4 +- src/util.js | 18 +- test/index.test.js | 29 +++ test/util.test.js | 27 +++ 5 files changed, 520 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 5c7b96f..bb70e9a 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,6 @@ write views in a way that is independent of the virtual DOM library being used. It's also nice to use convenient features even if the underlying virtual DOM library does not support them. -## Convenient Features - -- CSS-style selectors such as `"input:text.form-control[name=username]"` -- Removal of `null`, `undefined`, and `false`, so that you can write - `isMessage && ["div", "message"]` -- CSS classes included or excluded using flags, so that you can write - `["div", { className: { "error": isError } }, "message"]` -- Express children as an array or as varargs - ## Example Instead of writing this in JSX: @@ -41,7 +32,7 @@ h("div", { id: "home" }, [ ]) ``` -You write this with `seview`: +You can write this with `seview`: ```javascript ["div#home", @@ -55,26 +46,142 @@ Besides the conveniences of the syntax, you also don't have to write `h` at ever switch from one virtual DOM library to another, you only need to make changes in **one** place. All your view code can remain the same. -## Installation +## Features -Using Node.js: +`seview` supports CSS-style selectors in tag names, `{ className: boolean }` for toggling classes, +using an array or varags for children, flattening of nested arrays, and removal of null/empty elements. + +### Element + +An element is an array: ``` -npm i -S seview +[tag, attrs, children] ``` -With a script tag: +or a string (text node): +``` +"this is a text node" +``` + +The `tag` can be a string, or something that your virtual DOM library understands; for example, +a `Component` in React. For the latter, `seview` just returns the selector as-is. + +### Tag + +When the tag is a string, it is assumed to be a tag name, possibly with CSS-style selectors: + +- `"div"`, `"span"`, `"h1"`, `"input"`, etc. +- `"div.highlighted"`, `"button.btn.btn-default"` for classes +- `"div#home"` for `id` +- `"input:text"` for ``. There can only be one type, so additional types are +ignored. `"input:password:text"` would result in ``. +- `"input[name=username][required]"` results in `` +- if you need spaces, just use them: `"input[placeholder=Enter your name here]"` +- default tag is `"div"`, so you can write `""`, `".highlighted"`, `"#home"`, etc. +- these features can all be used together, for example + `"input:password#duck.quack.yellow[name=pwd][required]"` results in + `` + +### Attributes + +If the second item is an object, it is considered to be the attributes for the element. + +Of course, for everything that you can do with a CSS-style selector in a tag as shown in the +previous section, you can also use attributes: + +```javascript +["input", { type: "password", name: "password", placeholder: "Enter your password here" }] +``` + +You can also mix selectors and attributes. If you specify something in both places, the attribute +overwrites the selector. + +```javascript +["input:password[name=password]", { placeholder: "Enter your password here" }] +``` ```html - + +``` + +```javascript +["input:password[name=username]", { type: "text", placeholder: "Enter your username here" }] +``` +```html + ``` +### Classes -## More Content to come +Classes can be specified in the tag as a selector (as shown above), and/or in attributes using +`className`: -_This README is a work-in-progress, more content is forthcoming._ +```javascript +["button.btn.info", { className: "btn-default special" }] +``` +```html +