Skip to content

Commit

Permalink
refactor(vdom): rename function elementFactory() to element()
Browse files Browse the repository at this point in the history
Makes API more consistent.

BREAKING CHANGE: `elementFactory()` function renamed to `element()`
  • Loading branch information
localvoid committed May 11, 2018
1 parent b591819 commit 6324a91
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/ivi/__tests__/element_factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { VNodeFlags, elementFactory } from "ivi";
import { VNodeFlags, element } from "ivi";
import * as h from "ivi-html";

const div = h.div();
const input = h.input();
const divFactory = elementFactory(div);
const inputFactory = elementFactory(input);
const divFactory = element(div);
const inputFactory = element(input);

test(`div flags`, () => {
expect(divFactory().flags & ~VNodeFlags.ElementFactory).toBe(h.div().flags);
Expand Down
12 changes: 6 additions & 6 deletions packages/ivi/__tests__/render_element_factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { elementFactory } from "ivi";
import { element } from "ivi";
import * as h from "ivi-html";
import { startRender } from "./utils";

test(`<div></div>`, () => {
startRender<HTMLElement>((r) => {
const e = elementFactory(h.div());
const e = element(h.div());
const n = r(e());

expect(n.tagName.toLowerCase()).toBe("div");
Expand All @@ -13,7 +13,7 @@ test(`<div></div>`, () => {

test(`predefined className: <div class="a"></div>`, () => {
startRender<HTMLElement>((r) => {
const e = elementFactory(h.div("a"));
const e = element(h.div("a"));
const n = r(e());

expect(n.classList.length).toBe(1);
Expand All @@ -23,7 +23,7 @@ test(`predefined className: <div class="a"></div>`, () => {

test(`<div class="a"></div>`, () => {
startRender<HTMLElement>((r) => {
const e = elementFactory(h.div());
const e = element(h.div());
const n = r(e("a"));

expect(n.classList.length).toBe(1);
Expand All @@ -33,7 +33,7 @@ test(`<div class="a"></div>`, () => {

test(`<div id="123"></div>`, () => {
startRender<HTMLElement>((r) => {
const e = elementFactory(h.div().a({ id: "123" }));
const e = element(h.div().a({ id: "123" }));
const n = r(e());

expect(n.attributes.length).toBe(1);
Expand All @@ -43,7 +43,7 @@ test(`<div id="123"></div>`, () => {

test(`render twice: <div id="123"></div>`, () => {
startRender<HTMLElement>((r) => {
const e = elementFactory(h.div().a({ id: "123" }));
const e = element(h.div().a({ id: "123" }));
r(e());
const n = r(e());

Expand Down
2 changes: 1 addition & 1 deletion packages/ivi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export {
autofocus, stopDirtyChecking,
} from "./vdom/vnode";
export { children, map, mapRange } from "./vdom/vnode_collections";
export { elementFactory } from "./vdom/element";
export { element } from "./vdom/element";
export { statefulComponent, statelessComponent, context, connect } from "./vdom/vnode_factories";

/**
Expand Down
23 changes: 17 additions & 6 deletions packages/ivi/src/vdom/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ import { VNodeFlags } from "./flags";
import { VNode } from "./vnode";

/**
* elementFactory creates an element factory for an element with predefined attributes.
* element creates an virtual DOM node factory that produces elements with predefined attributes.
*
* @param element Element prototype.
* @returns VNode factory function.
* const DivWithIdAttribute = element(
* h.div().a({
* "id": "predefined-id",
* });
* );
*
* render(
* DivWithIdAttribute(),
* DOMContainer,
* );
*
* @param proto virtual DOM element prototype.
* @returns factory that produces elements with predefined attributes.
*/
export function elementFactory<P, N>(element: VNode<P, N>): (className?: string) => VNode<P, N> {
const flags = element.flags | VNodeFlags.ElementFactory;
export function element<P, N>(proto: VNode<P, N>): (className?: string) => VNode<P, N> {
const flags = proto.flags | VNodeFlags.ElementFactory;
return function (className?: string) {
return new VNode<P, N>(
flags,
element,
proto,
void 0,
className,
null,
Expand Down

0 comments on commit 6324a91

Please sign in to comment.