@sealcode/sealgen 0.15.56 → 0.15.58

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.
@@ -1,5 +1,6 @@
1
1
  /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
+ import { Context } from "koa";
3
4
  import { FlatTemplatable } from "tempstream";
4
5
  import { FormField } from "../fields/field.js";
5
6
  import { Table as TableField } from "../fields/table.js";
@@ -13,9 +14,14 @@ export type TableControlOptions<F extends Record<string, FormField>> = {
13
14
  label_add?: string;
14
15
  label: string;
15
16
  label_remove?: string;
16
- } & {
17
- allow_adding: boolean;
18
- };
17
+ } & ({
18
+ allow_adding: false;
19
+ } | {
20
+ allow_adding: true;
21
+ make_new_row: (ctx: Context) => Promise<{
22
+ [field_name in keyof F]: unknown;
23
+ }>;
24
+ });
19
25
  export declare class Table<F extends Record<string, FormField>> extends FormControl {
20
26
  table_field: TableField<F>;
21
27
  options: TableControlOptions<F>;
@@ -75,10 +75,27 @@ class Table extends FormControl {
75
75
  ${this.options.allow_adding ? (
76
76
  /* HTML */
77
77
  `<button
78
- onclick="this.closest('turbo-frame').querySelector('tbody').appendChild(this.closest('turbo-frame').querySelector('template').cloneNode(true).content)"
79
- >
80
- add
81
- </button>`
78
+ onclick="this.closest('turbo-frame').querySelector('tbody').appendChild(this.closest('turbo-frame').querySelector('template').cloneNode(true).content)"
79
+ >
80
+ add
81
+ </button>
82
+ <noscript>
83
+ <input
84
+ type="submit"
85
+ value="${this.options.label_add || "add"}"
86
+ form="${fctx.form_id}"
87
+ formnovalidate
88
+ formaction="${this.getActionURL(
89
+ this.table_field.name,
90
+ {
91
+ insert: {
92
+ index: rows.length,
93
+ value: {}
94
+ }
95
+ }
96
+ )}"
97
+ />
98
+ </noscript>`
82
99
  ) : ""}
83
100
  </div>
84
101
  </turbo-frame>`;
@@ -100,6 +117,21 @@ class Table extends FormControl {
100
117
  await next();
101
118
  return;
102
119
  }
120
+ if (this.options.allow_adding && hasShape(
121
+ {
122
+ insert: predicates.shape({ index: predicates.string })
123
+ },
124
+ action
125
+ )) {
126
+ if (field_name !== this.table_field.name) {
127
+ await next();
128
+ return;
129
+ }
130
+ if (!ctx.$body[this.table_field.name]) {
131
+ ctx.$body[this.table_field.name] = {};
132
+ }
133
+ ctx.$body[this.table_field.name][action.insert.index] = await this.options.make_new_row(ctx);
134
+ }
103
135
  if (hasShape(
104
136
  {
105
137
  remove: predicates.shape({ index: predicates.string })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.15.56",
3
+ "version": "0.15.58",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -28,7 +28,15 @@ export type TableControlOptions<F extends Record<string, FormField>> = {
28
28
  label_add?: string;
29
29
  label: string;
30
30
  label_remove?: string;
31
- } & { allow_adding: boolean };
31
+ } & (
32
+ | { allow_adding: false }
33
+ | {
34
+ allow_adding: true;
35
+ make_new_row: (
36
+ ctx: Context
37
+ ) => Promise<{ [field_name in keyof F]: unknown }>;
38
+ }
39
+ );
32
40
 
33
41
  export class Table<F extends Record<string, FormField>> extends FormControl {
34
42
  role = <const>"input";
@@ -122,10 +130,27 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
122
130
  <template> ${make_row(null, rows.length)} </template>
123
131
  ${this.options.allow_adding
124
132
  ? /* HTML */ `<button
125
- onclick="this.closest('turbo-frame').querySelector('tbody').appendChild(this.closest('turbo-frame').querySelector('template').cloneNode(true).content)"
126
- >
127
- add
128
- </button>`
133
+ onclick="this.closest('turbo-frame').querySelector('tbody').appendChild(this.closest('turbo-frame').querySelector('template').cloneNode(true).content)"
134
+ >
135
+ add
136
+ </button>
137
+ <noscript>
138
+ <input
139
+ type="submit"
140
+ value="${this.options.label_add || "add"}"
141
+ form="${fctx.form_id}"
142
+ formnovalidate
143
+ formaction="${this.getActionURL(
144
+ this.table_field.name,
145
+ {
146
+ insert: {
147
+ index: rows.length,
148
+ value: {},
149
+ },
150
+ }
151
+ )}"
152
+ />
153
+ </noscript>`
129
154
  : ""}
130
155
  </div>
131
156
  </turbo-frame>`;
@@ -153,6 +178,25 @@ export class Table<F extends Record<string, FormField>> extends FormControl {
153
178
  await next();
154
179
  return;
155
180
  }
181
+ if (
182
+ this.options.allow_adding &&
183
+ hasShape(
184
+ {
185
+ insert: predicates.shape({ index: predicates.string }),
186
+ },
187
+ action
188
+ )
189
+ ) {
190
+ if (field_name !== this.table_field.name) {
191
+ await next();
192
+ return;
193
+ }
194
+ if (!ctx.$body[this.table_field.name]) {
195
+ ctx.$body[this.table_field.name] = {};
196
+ }
197
+ (ctx.$body[this.table_field.name] as any)[action.insert.index] =
198
+ await this.options.make_new_row(ctx);
199
+ }
156
200
  if (
157
201
  hasShape(
158
202
  {