Skip to content

Commit 5fce777

Browse files
committed
Some updates
1 parent 7697c39 commit 5fce777

File tree

9 files changed

+75
-74
lines changed

9 files changed

+75
-74
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-elements",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"description": "",
55
"main": "./index.js",
66
"module": "./dist/js-elements.esm.production.js",

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function createConfig(pkg, moduleFormat, productive) {
4747
replace({
4848
exclude: 'node_modules/**',
4949
delimiters: ['', ''],
50+
preventAssignment: false,
5051

5152
values: {
5253
'process.env.NODE_ENV': productive ? "'production'" : "'development'"

src/main/core/base.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component, Ctrl, Ref, VNode } from './types'
1+
import { Component, Ctrl, Ref, UIEvent, VNode } from './types'
22
import { h, renderer } from './vdom'
33

44
// @ts-ignore
55
import { patch } from './superfine'
66

77
// === exports =======================================================
88

9-
export { attr, define, ref }
9+
export { attr, define, event, ref }
1010

1111
// === local data =====================================================
1212

@@ -54,6 +54,21 @@ function ref<T>(value: T | null = null): Ref<T> {
5454
return { current: value }
5555
}
5656

57+
function event<T extends string, D = null>(
58+
type: T,
59+
detail?: D,
60+
options?: { bubbles: boolean; cancelable?: boolean }
61+
): UIEvent<T, D> {
62+
const params = {
63+
detail: detail || null,
64+
bubbles: !options || !!options.bubbles,
65+
cancabble: !options || !!options.cancelable,
66+
composed: true
67+
}
68+
69+
return new CustomEvent(type, params) as UIEvent<T, D>
70+
}
71+
5772
function define(tagName: string, main: () => () => VNode): Component<{}>
5873

5974
function define<P>(
@@ -130,17 +145,22 @@ function buildCustomElementClass(
130145
])
131146
)
132147

133-
const propNameToAttrNameMap: Map<string, string> = new Map(
134-
Array.from(attrNameToPropNameMap).map(([k, v]) => [v, k])
135-
)
136-
137148
const propNameToConverterMap: Map<string, PropConverter<any>> = new Map(
138149
!attrsOptions
139150
? null
140-
: Array.from(attrsOptions.entries()).map(([propName, attrOptions]) => [
141-
propName,
142-
commonPropConverters.get(attrOptions.kind)!
143-
])
151+
: Array.from(attrsOptions.entries()).map(([propName, attrOptions]) => {
152+
const kind = attrOptions.kind
153+
154+
// TODO!!!
155+
const propConv =
156+
kind === Boolean
157+
? booleanPropConv
158+
: kind === Number
159+
? numberPropConv
160+
: stringPropConv
161+
162+
return [propName, propConv]
163+
})
144164
)
145165

146166
const customElementClass = class extends HTMLElement {
@@ -378,21 +398,19 @@ function createNotifier(): Notifier {
378398
}
379399
}
380400

381-
// === attribute converters ==========================================
382-
383-
const commonPropConverters = new Map<AttrKind, PropConverter<any>>()
401+
// === prop converters ===============================================
384402

385-
commonPropConverters.set(String, {
403+
const stringPropConv = {
386404
fromPropToString: (it: string) => it,
387405
fromStringToProp: (it: string) => it
388-
})
406+
}
389407

390-
commonPropConverters.set(Number, {
408+
const numberPropConv = {
391409
fromPropToString: (it: number) => String(it),
392410
fromStringToProp: (it: string) => Number.parseFloat(it)
393-
})
411+
}
394412

395-
commonPropConverters.set(Boolean, {
413+
const booleanPropConv = {
396414
fromPropToString: (it: boolean) => (it ? 'true' : 'false'),
397415
fromStringToProp: (it: string) => (it === 'true' ? true : false)
398-
})
416+
}

src/main/core/utils.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
import { State, Store, UIEvent } from './types'
22

3-
export { createEvent, initStore }
4-
5-
function createEvent<T extends string, D = null>(
6-
type: T,
7-
detail?: D
8-
): UIEvent<T, D> {
9-
return new CustomEvent(type, {
10-
detail: detail || null,
11-
bubbles: true,
12-
composed: true
13-
}) as any
14-
}
3+
export { initStore }
154

165
// === store =========================================================
176

src/main/core/vdom.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const EMPTY_OBJ = {}
1515

1616
function h(
1717
type: string,
18-
props?: Props | null, // TODO!!!!!
18+
props?: Props | null, // TODO!!!
1919
...children: VNode[]
2020
): VElement
2121

@@ -25,55 +25,51 @@ function h<P extends Props>(
2525
...children: VNode[]
2626
): VElement
2727

28-
function h(t: any, p: any) {
28+
function h(type: string | Component<any>, props?: Props | null): VElement {
2929
const argc = arguments.length
30-
const type = typeof t === 'function' ? t.tagName : t
30+
const tagName = typeof type === 'function' ? (type as any).tagName : type
3131

32-
if (typeof t === 'function' && type === undefined) {
33-
throw new Error('Component cannot be rendered as it is not registered yet')
32+
if (process.env.NODE_ENV === ('development' as string)) {
33+
if (typeof tagName !== 'string') {
34+
throw new Error('[h] First argument must be a string or a component')
35+
}
3436
}
3537

36-
const props =
37-
p === undefined ||
38-
p === null ||
39-
typeof p !== 'object' ||
40-
p.isVElement === true ||
41-
typeof p[Symbol.iterator] === 'function'
42-
? EMPTY_OBJ
43-
: p
44-
45-
const firstChildIdx =
46-
p === undefined || p === null || props !== EMPTY_OBJ ? 2 : 1
47-
48-
let children = EMPTY_ARR
38+
const children = argc > 2 ? [] : EMPTY_ARR
4939

50-
if (firstChildIdx === argc - 1) {
51-
children = asVNode(arguments[firstChildIdx])
52-
} else if (firstChildIdx < argc - 1) {
53-
children = []
40+
if (argc > 2) {
41+
for (let i = 2; i < argc; ++i) {
42+
const child = arguments[i]
5443

55-
for (let i = firstChildIdx; i < argc; ++i) {
56-
children.push(asVNode(arguments[i]))
44+
if (!Array.isArray(child)) {
45+
children.push(asVNode(child))
46+
} else {
47+
for (let j = 0; j < child.length; ++j) {
48+
children.push(asVNode(child[j]))
49+
}
50+
}
5751
}
5852
}
5953

60-
const ret: any = createElement(type, props, children)
54+
const ret: any = createElement(tagName, props || EMPTY_OBJ, children)
6155
ret.isVElement = true
6256
return ret
6357
}
6458

6559
// === render ========================================================
6660

6761
export function render(content: VElement, container: Element | string) {
68-
if (content !== null && (!content || content.isVElement !== true)) {
69-
throw new TypeError()
70-
;('First argument "content" of function "render" must be a virtual element or null')
71-
}
62+
if (process.env.NODE_ENV === ('development' as string)) {
63+
if (content !== null && (!content || content.isVElement !== true)) {
64+
throw new TypeError()
65+
;('First argument "content" of function "render" must be a virtual element or null')
66+
}
7267

73-
if (!container || (typeof container !== 'string' && !container.tagName)) {
74-
throw new TypeError(
75-
'Second argument "container" of function "render" must either be a DOM element or selector string for the DOM element'
76-
)
68+
if (!container || (typeof container !== 'string' && !container.tagName)) {
69+
throw new TypeError(
70+
'Second argument "container" of function "render" must either be a DOM element or selector string for the DOM element'
71+
)
72+
}
7773
}
7874

7975
const target =

src/main/js-elements-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { createEvent, initStore } from './core/utils'
1+
export { initStore } from './core/utils'

src/main/js-elements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { attr, define, ref } from './core/base'
1+
export { attr, define, event, ref } from './core/base'
22
export { h, html, render } from './core/vdom'
33

44
export {

src/stories/demos/button-demo.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { attr, define, h, EventHandler, UIEvent } from 'js-elements'
2-
1+
import { attr, define, event, h, EventHandler, UIEvent } from 'js-elements'
32
import { useEmitter, useStyles } from 'js-elements/hooks'
4-
import { createEvent } from 'js-elements/utils'
53

64
const buttonDemoStyles = `
75
.demo-button {
@@ -33,7 +31,7 @@ const DemoButton = define('demo-button', ButtonProps, (props) => {
3331
useStyles(buttonDemoStyles)
3432

3533
const onClick = () => {
36-
emit(createEvent('button-click'), props.onButtonClick)
34+
emit(event('button-click'), props.onButtonClick)
3735
}
3836

3937
return () => (

src/stories/demos/complex-counter-demo.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import {
22
define,
33
h,
4+
event,
45
ref,
56
MethodsOf,
67
EventHandler,
78
Ref,
89
UIEvent
910
} from 'js-elements'
1011

11-
import { createEvent } from 'js-elements/utils'
12-
1312
import {
1413
useEffect,
1514
useEmitter,
@@ -56,7 +55,7 @@ const Counter = define('complex-counter', CounterProps, (p) => {
5655
useEffect(
5756
() => {
5857
if (status.hasUpdated()) {
59-
emit(createEvent('count-change', { count: s.count }), p.onCountChange)
58+
emit(event('count-change', { count: s.count }), p.onCountChange)
6059
}
6160
},
6261
() => [s.count]

0 commit comments

Comments
 (0)