Comparing version 1.4.0 to 1.4.1
@@ -32,2 +32,3 @@ 'use strict'; | ||
var DOCUMENT_FRAGMENT_NODE = 11; | ||
var ATTR_TYPE_DEFAULTS = { string: '', boolean: false, number: 0, json: null }; | ||
@@ -538,7 +539,14 @@ /** | ||
var attrSchema = attrsSchema[attr]; | ||
var attrType = attrSchema.type || 'string'; | ||
if (typeof attrSchema === 'string') { | ||
attrSchema = { type: attrSchema }; | ||
} | ||
var attrType = attrSchema.type; | ||
if (!ATTR_TYPE_DEFAULTS.hasOwnProperty(attrType)) { | ||
throw new Error('Unknown type: ' + attrType + ' for attr: ' + attr + ' in attrsSchema. ' + ('Only (' + Object.keys(ATTR_TYPE_DEFAULTS).join(' | ') + ') supported.')); | ||
} | ||
var attrValue = null; | ||
if (!this.hasAttribute(attr) && attrSchema.hasOwnProperty('default')) { | ||
attrValue = attrSchema.default; | ||
if (!this.hasAttribute(attr)) { | ||
attrValue = attrSchema.hasOwnProperty('default') ? attrSchema.default : ATTR_TYPE_DEFAULTS[attrType]; | ||
} else if (attrType === 'string') { | ||
@@ -545,0 +553,0 @@ attrValue = this.getAttribute(attr); |
@@ -8,2 +8,3 @@ import cuid from 'cuid'; | ||
const DOCUMENT_FRAGMENT_NODE = 11; | ||
const ATTR_TYPE_DEFAULTS = {string: ``, boolean: false, number: 0, json: null}; | ||
@@ -430,8 +431,18 @@ /** | ||
if (attrsSchema.hasOwnProperty(attr)) { | ||
const attrSchema = attrsSchema[attr]; | ||
const attrType = attrSchema.type || `string`; | ||
let attrSchema = attrsSchema[attr]; | ||
if (typeof attrSchema === `string`) { | ||
attrSchema = {type: attrSchema}; | ||
} | ||
const attrType = attrSchema.type; | ||
if (!ATTR_TYPE_DEFAULTS.hasOwnProperty(attrType)) { | ||
throw new Error( | ||
`Unknown type: ${attrType} for attr: ${attr} in attrsSchema. ` + | ||
`Only (${Object.keys(ATTR_TYPE_DEFAULTS).join(` | `)}) supported.` | ||
); | ||
} | ||
let attrValue = null; | ||
if (!this.hasAttribute(attr) && attrSchema.hasOwnProperty(`default`)) { | ||
attrValue = attrSchema.default; | ||
if (!this.hasAttribute(attr)) { | ||
attrValue = attrSchema.hasOwnProperty(`default`) ? attrSchema.default : ATTR_TYPE_DEFAULTS[attrType]; | ||
} else if (attrType === `string`) { | ||
@@ -438,0 +449,0 @@ attrValue = this.getAttribute(attr); |
@@ -112,5 +112,7 @@ // Type definitions for panel | ||
type AttrType = 'string' | 'number' | 'boolean' | 'json'; | ||
interface AttrSchema { | ||
/** Type of the attribute, default is 'string' */ | ||
type?: 'string' | 'number' | 'boolean' | 'json'; | ||
/** Type of the attribute */ | ||
type: AttrType; | ||
@@ -132,3 +134,3 @@ /** Default value if the attr is not defined */ | ||
*/ | ||
static attrsSchema: {[attr: string]: Component.AttrSchema}; | ||
static attrsSchema: {[attr: string]: (Component.AttrSchema | Component.AttrType)}; | ||
@@ -135,0 +137,0 @@ /** Attributes parsed from component's html attributes using attrsSchema */ |
{ | ||
"name": "panel", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Web Components with Virtual DOM: lightweight composable web apps", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
124579
2601