@neeloong/form 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -34,12 +34,12 @@ const template = `
34
34
 
35
35
  /** @type {Layout.Options} */
36
36
  const layoutOptions = {
37
- creteCalc: value => {
37
+ createCalc: value => {
38
38
  const fn = new Function('globalThis', `with(globalThis) { return ${value} }`);
39
39
  fn.toString = () => value;
40
40
  return /** @type {*} */(fn);
41
41
  },
42
- creteEvent: value => {
42
+ createEvent: value => {
43
43
  const fn = new Function('$event', 'globalThis', `with(globalThis) { ${value} }`);
44
44
  fn.toString = () => value;
45
45
  return /** @type {*} */(fn);
@@ -176,13 +176,14 @@ render(store, layouts, app);
176
176
  1. 字段扩展隐式属性
177
177
  - `$value`
178
178
  - `$state`
179
+ - `$store` 只读属性
180
+ - `$schema` 只读属性
179
181
  - `$null` 只读属性
180
182
  - `$index` 只读属性
181
183
  - `$no` 只读属性
182
184
  - `$length` 只读属性
183
185
  - `$creatable` 只读属性
184
186
  - `$immutable` 只读属性
185
- - `$schema` 只读属性
186
187
  - `$new` 只读属性
187
188
  - `$readonly` 只读属性
188
189
  - `$hidden` 只读属性
@@ -196,6 +197,10 @@ render(store, layouts, app);
196
197
  - `$max` 只读属性
197
198
  - `$step` 只读属性
198
199
  - `$values` 只读属性
200
+ - `$type` 只读属性
201
+ - `$meta` 只读属性
202
+ - `$component` 只读属性
203
+ - `$kind` 只读属性
199
204
  1. 数组字段扩展隐式函数(只在事件中可用)
200
205
  - `$insert(index, value)`
201
206
  - `$add(value)`
@@ -209,9 +214,13 @@ render(store, layouts, app);
209
214
  - `$remove()`
210
215
  - `$upMove()`
211
216
  - `$downMove()`
217
+ 1. 上下文隐式变量
218
+ - '$store' 只读属性
219
+ - '$root' 只读属性
212
220
  1. 当前范围及组件范围的所有字段(数组字段的成员除外,因为数组字段成员索引为数字,不符合标识符命名规则)都存在 `field$value` 及 `field$$value` 形式的变量
213
221
  1. 如果别名为字段的别名,则也存在 `alias$value` 形式的变量
214
222
  1. 当存在同名变量时,则会按照变量来源类型决定优先级,从高到低依次为:
223
+ 1. 上下文隐式变量
215
224
  1. 显式声明(如别名、计算名、显式变量),如果别名是字段的别名,则也包括 `alias$value` 形式的变量
216
225
  1. 全局变量,此部分由 `render` 的参数传入
217
226
  1. 字段声明,包括 `field$value` 及 `field$$value` 形式的变量
package/index.d.mts CHANGED
@@ -1,14 +1,85 @@
1
1
  /*!
2
- * @neeloong/form v0.3.0
2
+ * @neeloong/form v0.4.1
3
3
  * (c) 2024-2025 Fierflame
4
4
  * @license Apache-2.0
5
5
  */
6
6
 
7
7
  export { Signal } from 'signal-polyfill';
8
8
 
9
+ /**
10
+ *
11
+ * @param {string} source
12
+ * @param {Layout.Options} [options]
13
+ * @returns {(Layout.Node | string)[]}
14
+ */
15
+ declare function parse(source: string, { createCalc, createEvent, simpleTag, }?: Options): (Node | string)[];
16
+
17
+ /**
18
+ *
19
+ * @param {Layout.Node | (Layout.Node |string)[]} value
20
+ * @param {boolean} [formable]
21
+ * @returns {string}
22
+ */
23
+ declare function toString(value: Node | (Node | string)[], formable?: boolean): string;
24
+
25
+ type Directives = {
26
+ template?: string | undefined;
27
+ fragment?: string | boolean | undefined;
28
+ if?: string | Calc | undefined;
29
+ else?: boolean | undefined;
30
+ /**
31
+ * 值关联(关联为列表)
32
+ */
33
+ value?: string | undefined;
34
+ /**
35
+ * 列表属性枚举
36
+ */
37
+ enum?: string | boolean | Calc | undefined;
38
+ bind?: string | boolean | undefined;
39
+ text?: string | Calc | undefined;
40
+ html?: string | Calc | undefined;
41
+ /**
42
+ * 注释
43
+ */
44
+ comment?: string | undefined;
45
+ };
46
+ type Options = {
47
+ createCalc?: ((t: string) => Calc) | undefined;
48
+ createEvent?: ((t: string) => EventListener) | undefined;
49
+ simpleTag?: Set<string> | undefined;
50
+ };
51
+ type Node = {
52
+ name: string;
53
+ is?: string | null | undefined;
54
+ id?: string | undefined;
55
+ attrs: Record<string, string | {
56
+ name: string;
57
+ } | Calc>;
58
+ params: Record<string, string | Calc>;
59
+ classes: Record<string, string | boolean | Calc>;
60
+ styles: Record<string, string | Calc>;
61
+ events: Record<string, string | EventListener>;
62
+ vars: Record<string, string | Calc>;
63
+ aliases: Record<string, string | Calc>;
64
+ directives: Directives;
65
+ children: (Node | string)[];
66
+ };
67
+ type Calc = (env: Record<string, any>) => any;
68
+ type EventListener = ($event: any, env: Record<string, any>) => void;
69
+
70
+ type index_d_Calc = Calc;
71
+ type index_d_Directives = Directives;
72
+ type index_d_EventListener = EventListener;
73
+ type index_d_Node = Node;
74
+ type index_d_Options = Options;
75
+ declare const index_d_parse: typeof parse;
76
+ declare namespace index_d {
77
+ export { type index_d_Calc as Calc, type index_d_Directives as Directives, type index_d_EventListener as EventListener, type index_d_Node as Node, type index_d_Options as Options, index_d_parse as parse, toString as stringify };
78
+ }
79
+
9
80
  type VerifyError = any;
10
81
  type Component = {
11
- tag: string | ((ctx: any) => Element);
82
+ tag: string | ((ctx: any) => Element | [Element, (Element | null)?]);
12
83
  is?: string | undefined;
13
84
  attrs?: Record<string, Component.Attr> | undefined;
14
85
  events?: Record<string, Component.Event> | undefined;
@@ -20,13 +91,13 @@ declare namespace Component {
20
91
  * // TODO: 可否计算,可否关联
21
92
  */
22
93
  isProp?: boolean | undefined;
23
- bind?: [event: string, set: ($event: any, global: any) => any, (boolean | undefined)?] | "hidden" | "clearable" | "readonly" | "disabled" | "required" | undefined;
94
+ bind?: [event: string, set: EventListener, (boolean | undefined)?] | "hidden" | "clearable" | "readonly" | "disabled" | "required" | undefined;
24
95
  default?: any;
25
96
  immutable?: boolean | undefined;
26
97
  };
27
98
  type Handler = {
28
99
  set: (name: string, value: any) => void;
29
- addEvent: (event: string, listener: ($event: any, global: any) => any) => void;
100
+ addEvent: (event: string, listener: EventListener) => void;
30
101
  destroy: () => void;
31
102
  tag: any;
32
103
  init: () => void;
@@ -52,8 +123,11 @@ declare namespace Component {
52
123
  /**
53
124
  * 过滤器
54
125
  */
55
- filters: Record<string, (($event: any, param: string[], env: any) => boolean | null | void) | string>;
126
+ filters: Record<string, Component.Event.Filter | string>;
56
127
  };
128
+ namespace Event {
129
+ type Filter = ($event: any, param: string[], env: any) => boolean | null | void;
130
+ }
57
131
  }
58
132
  type ComponentGetter = (path: string[], next?: ((path: string[]) => Component | null) | undefined) => Component | null;
59
133
  type Schema = Record<string, Schema.Field>;
@@ -82,25 +156,22 @@ declare namespace Schema {
82
156
  type?: null | undefined;
83
157
  props?: Record<string, Schema.Field> | undefined;
84
158
  array?: boolean | undefined;
85
- meta?: any;
86
159
  };
87
160
  type Type = {
88
161
  type: string;
89
162
  props?: null | undefined;
90
163
  array?: boolean | undefined;
91
- meta?: any;
92
164
  };
93
165
  type Event = {
94
- input?: Function | null | undefined;
95
- change?: Function | null | undefined;
96
- click?: Function | null | undefined;
97
- focus?: Function | null | undefined;
98
- blur?: Function | null | undefined;
99
- add?: Function | null | undefined;
100
- remove?: Function | null | undefined;
101
- move?: Function | null | undefined;
166
+ input: InputEvent;
167
+ change: InputEvent;
168
+ click: Event;
169
+ focus: Event;
170
+ blur: Event;
102
171
  };
103
172
  type Attr = {
173
+ meta?: any;
174
+ component?: any;
104
175
  immutable?: boolean | undefined;
105
176
  creatable?: boolean | undefined;
106
177
  hidden?: boolean | ((store: Store, root: Store) => boolean) | null | undefined;
@@ -136,6 +207,13 @@ declare namespace Schema {
136
207
  * 可选值
137
208
  */
138
209
  values?: (Schema.Value.Define | Schema.Value.Group.Define)[] | undefined;
210
+ events?: {
211
+ input?: ((this: Store, value: InputEvent, store: Store) => void | boolean | null) | null | undefined;
212
+ change?: ((this: Store, value: InputEvent, store: Store) => void | boolean | null) | null | undefined;
213
+ click?: ((this: Store, value: Event, store: Store) => void | boolean | null) | null | undefined;
214
+ focus?: ((this: Store, value: Event, store: Store) => void | boolean | null) | null | undefined;
215
+ blur?: ((this: Store, value: Event, store: Store) => void | boolean | null) | null | undefined;
216
+ } | undefined;
139
217
  };
140
218
  }
141
219
 
@@ -206,10 +284,30 @@ declare class Store<T = any> {
206
284
  onUpdate?: ((value: T | null, index: any) => void) | null | undefined;
207
285
  onUpdateState?: ((value: T | null, index: any) => void) | null | undefined;
208
286
  });
287
+ /**
288
+ *
289
+ * @template {keyof Schema.Event} K
290
+ * @param {K} event
291
+ * @param {Schema.Event[K]} value
292
+ */
293
+ emit<K extends keyof Schema.Event>(event: K, value: Schema.Event[K]): boolean;
294
+ /**
295
+ *
296
+ * @template {keyof Schema.Event} K
297
+ * @param {K} event
298
+ * @param {(this: this, p: Schema.Event[K], store: this) => void | boolean | null} listener
299
+ * @returns {() => void}
300
+ */
301
+ listen<K extends keyof Schema.Event>(event: K, listener: (this: this, p: Schema.Event[K], store: this) => void | boolean | null): () => void;
209
302
  get null(): boolean;
303
+ get kind(): string;
210
304
  schema: Schema.Field;
305
+ get store(): this;
211
306
  get parent(): Store<any> | null;
212
307
  get root(): Store<any>;
308
+ get type(): any;
309
+ get meta(): any;
310
+ get component(): any;
213
311
  set length(v: number);
214
312
  get length(): number;
215
313
  set index(v: string | number);
@@ -308,73 +406,6 @@ declare class ObjectStore extends Store<any> {
308
406
  #private;
309
407
  }
310
408
 
311
- /**
312
- *
313
- * @param {string} source
314
- * @param {Layout.Options} [options]
315
- * @returns {(Layout.Node | string)[]}
316
- */
317
- declare function parse(source: string, { creteCalc, creteEvent, simpleTag, }?: Options): (Node | string)[];
318
-
319
- /**
320
- *
321
- * @param {Layout.Node | (Layout.Node |string)[]} value
322
- * @param {boolean} [formable]
323
- * @returns {string}
324
- */
325
- declare function toString(value: Node | (Node | string)[], formable?: boolean): string;
326
-
327
- type Directives = {
328
- template?: string | undefined;
329
- fragment?: string | boolean | undefined;
330
- if?: string | Function | undefined;
331
- else?: boolean | undefined;
332
- /**
333
- * 值关联(关联为列表)
334
- */
335
- value?: string | undefined;
336
- /**
337
- * 列表属性枚举
338
- */
339
- enum?: string | boolean | Function | undefined;
340
- bind?: string | undefined;
341
- text?: string | Function | undefined;
342
- html?: string | Function | undefined;
343
- /**
344
- * 注释
345
- */
346
- comment?: string | Function | undefined;
347
- };
348
- type Options = {
349
- creteCalc?: ((t: string) => (vars: Record<string, any>) => any) | undefined;
350
- creteEvent?: ((t: string) => ($event: any, vars: Record<string, any>) => any) | undefined;
351
- simpleTag?: Set<string> | undefined;
352
- };
353
- type Node = {
354
- name: string;
355
- is?: string | null | undefined;
356
- id?: string | undefined;
357
- attrs: Record<string, string | {
358
- name: string;
359
- } | ((global: any) => void)>;
360
- params: Record<string, string | ((global: any) => void)>;
361
- classes: Record<string, string | boolean | ((global: any) => void)>;
362
- styles: Record<string, string | ((global: any) => void)>;
363
- events: Record<string, string | (($event: any, global: any) => void)>;
364
- vars: Record<string, string | ((global: any) => void)>;
365
- aliases: Record<string, string | Function>;
366
- directives: Directives;
367
- children: (Node | string)[];
368
- };
369
-
370
- type index_d_Directives = Directives;
371
- type index_d_Node = Node;
372
- type index_d_Options = Options;
373
- declare const index_d_parse: typeof parse;
374
- declare namespace index_d {
375
- export { type index_d_Directives as Directives, type index_d_Node as Node, type index_d_Options as Options, index_d_parse as parse, toString as stringify };
376
- }
377
-
378
409
  /**
379
410
  * @overload
380
411
  * @param {Store} store