|
1 | | -import { Component, Ctrl, Ref, VNode } from './types' |
| 1 | +import { Component, Ctrl, Ref, UIEvent, VNode } from './types' |
2 | 2 | import { h, renderer } from './vdom' |
3 | 3 |
|
4 | 4 | // @ts-ignore |
5 | 5 | import { patch } from './superfine' |
6 | 6 |
|
7 | 7 | // === exports ======================================================= |
8 | 8 |
|
9 | | -export { attr, define, ref } |
| 9 | +export { attr, define, event, ref } |
10 | 10 |
|
11 | 11 | // === local data ===================================================== |
12 | 12 |
|
@@ -54,6 +54,21 @@ function ref<T>(value: T | null = null): Ref<T> { |
54 | 54 | return { current: value } |
55 | 55 | } |
56 | 56 |
|
| 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 | + |
57 | 72 | function define(tagName: string, main: () => () => VNode): Component<{}> |
58 | 73 |
|
59 | 74 | function define<P>( |
@@ -130,17 +145,22 @@ function buildCustomElementClass( |
130 | 145 | ]) |
131 | 146 | ) |
132 | 147 |
|
133 | | - const propNameToAttrNameMap: Map<string, string> = new Map( |
134 | | - Array.from(attrNameToPropNameMap).map(([k, v]) => [v, k]) |
135 | | - ) |
136 | | - |
137 | 148 | const propNameToConverterMap: Map<string, PropConverter<any>> = new Map( |
138 | 149 | !attrsOptions |
139 | 150 | ? 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 | + }) |
144 | 164 | ) |
145 | 165 |
|
146 | 166 | const customElementClass = class extends HTMLElement { |
@@ -378,21 +398,19 @@ function createNotifier(): Notifier { |
378 | 398 | } |
379 | 399 | } |
380 | 400 |
|
381 | | -// === attribute converters ========================================== |
382 | | - |
383 | | -const commonPropConverters = new Map<AttrKind, PropConverter<any>>() |
| 401 | +// === prop converters =============================================== |
384 | 402 |
|
385 | | -commonPropConverters.set(String, { |
| 403 | +const stringPropConv = { |
386 | 404 | fromPropToString: (it: string) => it, |
387 | 405 | fromStringToProp: (it: string) => it |
388 | | -}) |
| 406 | +} |
389 | 407 |
|
390 | | -commonPropConverters.set(Number, { |
| 408 | +const numberPropConv = { |
391 | 409 | fromPropToString: (it: number) => String(it), |
392 | 410 | fromStringToProp: (it: string) => Number.parseFloat(it) |
393 | | -}) |
| 411 | +} |
394 | 412 |
|
395 | | -commonPropConverters.set(Boolean, { |
| 413 | +const booleanPropConv = { |
396 | 414 | fromPropToString: (it: boolean) => (it ? 'true' : 'false'), |
397 | 415 | fromStringToProp: (it: string) => (it === 'true' ? true : false) |
398 | | -}) |
| 416 | +} |
0 commit comments