UNPKG

740 BJavaScriptView Raw
1import { isObject } from "d3-let";
2
3const componentsFromType = {
4 text: "input",
5 email: "input",
6 password: "input",
7 checkbox: "input",
8 number: "input",
9 date: "input",
10 url: "input",
11 "datetime-local": "input"
12};
13
14export const formComponent = child => {
15 var type = child.type || "text";
16 return componentsFromType[type] || type;
17};
18
19export const addAttributes = (el, attributes) => {
20 var expr, attr;
21
22 if (!isObject(attributes)) return;
23
24 for (attr in attributes) {
25 expr = attributes[attr];
26 if (isObject(expr)) expr = JSON.stringify(expr);
27 el.attr(attr, expr || "");
28 }
29};
30
31export function formChild(child) {
32 var component = formComponent(child);
33 return document.createElement(`d3-form-${component}`);
34}