@thi.ng/rdom-forms 0.1.0
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/CHANGELOG.md +22 -0
- package/LICENSE +201 -0
- package/README.md +115 -0
- package/api.d.ts +277 -0
- package/api.js +1 -0
- package/compile.d.ts +55 -0
- package/compile.js +353 -0
- package/index.d.ts +3 -0
- package/index.js +2 -0
- package/package.json +87 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
- **Last updated**: 2023-12-09T19:12:03Z
|
|
4
|
+
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
|
+
|
|
6
|
+
All notable changes to this project will be documented in this file.
|
|
7
|
+
See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
|
|
8
|
+
|
|
9
|
+
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
|
+
and/or version bumps of transitive dependencies.
|
|
11
|
+
|
|
12
|
+
## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rdom-forms@0.1.0) (2023-12-09)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- import as new pkg ([894e58d](https://github.com/thi-ng/umbrella/commit/894e58d))
|
|
17
|
+
- add docs, update generateForm(), factories ([716a988](https://github.com/thi-ng/umbrella/commit/716a988))
|
|
18
|
+
- add BehaviorOpts.values, update value attrib handling ([1283d80](https://github.com/thi-ng/umbrella/commit/1283d80))
|
|
19
|
+
|
|
20
|
+
#### 🩹 Bug fixes
|
|
21
|
+
|
|
22
|
+
- fix radio attribs ([7bb28ca](https://github.com/thi-ng/umbrella/commit/7bb28ca))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
|
+
<!-- Please see: https://github.com/thi-ng/umbrella/blob/develop/CONTRIBUTING.md#changes-to-readme-files -->
|
|
3
|
+
|
|
4
|
+
# 
|
|
5
|
+
|
|
6
|
+
[](https://www.npmjs.com/package/@thi.ng/rdom-forms)
|
|
7
|
+

|
|
8
|
+
[](https://mastodon.thi.ng/@toxi)
|
|
9
|
+
|
|
10
|
+
This project is part of the
|
|
11
|
+
[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo and anti-framework.
|
|
12
|
+
|
|
13
|
+
- [About](#about)
|
|
14
|
+
- [Status](#status)
|
|
15
|
+
- [Related packages](#related-packages)
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Dependencies](#dependencies)
|
|
18
|
+
- [Usage examples](#usage-examples)
|
|
19
|
+
- [API](#api)
|
|
20
|
+
- [Authors](#authors)
|
|
21
|
+
- [License](#license)
|
|
22
|
+
|
|
23
|
+
## About
|
|
24
|
+
|
|
25
|
+
Data-driven declarative & extensible HTML form generation. This is a support package for [@thi.ng/rdom](https://github.com/thi-ng/umbrella/tree/develop/packages/rdom).
|
|
26
|
+
|
|
27
|
+
This package uses vanilla JS objects to define component specs for various types
|
|
28
|
+
of form elements (various factory functions are provided). These specs can then
|
|
29
|
+
be passed to the polymorphic & dynamically extensible
|
|
30
|
+
[`compileForm()`](https://docs.thi.ng/umbrella/rdom/functions/compileForm.html)
|
|
31
|
+
function to generate the actual form elements/components in hiccup format, which
|
|
32
|
+
can then be used with
|
|
33
|
+
[thi.ng/rdom](https://github.com/thi-ng/umbrella/tree/develop/packages/rdom) or
|
|
34
|
+
for static (or SSR) HTML generation via
|
|
35
|
+
[thi.ng/hiccup](https://github.com/thi-ng/umbrella/tree/develop/packages/hiccup).
|
|
36
|
+
|
|
37
|
+
All generated form elements are unstyled by default, but can be fully customized (in various stages) via user-provided options.
|
|
38
|
+
|
|
39
|
+
Please see the example project linked further below, which demonstrates all currently provided elements...
|
|
40
|
+
|
|
41
|
+
## Status
|
|
42
|
+
|
|
43
|
+
**ALPHA** - bleeding edge / work-in-progress
|
|
44
|
+
|
|
45
|
+
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Brdom-forms%5D+in%3Atitle)
|
|
46
|
+
|
|
47
|
+
## Related packages
|
|
48
|
+
|
|
49
|
+
- [@thi.ng/rdom-components](https://github.com/thi-ng/umbrella/tree/develop/packages/rdom-components) - Collection of unstyled, customizable components for [@thi.ng/rdom](https://github.com/thi-ng/umbrella/tree/develop/packages/rdom)
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
yarn add @thi.ng/rdom-forms
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
ES module import:
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<script type="module" src="https://cdn.skypack.dev/@thi.ng/rdom-forms"></script>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
[Skypack documentation](https://docs.skypack.dev/)
|
|
64
|
+
|
|
65
|
+
For Node.js REPL:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
const rdomForms = await import("@thi.ng/rdom-forms");
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.13 KB
|
|
72
|
+
|
|
73
|
+
## Dependencies
|
|
74
|
+
|
|
75
|
+
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
76
|
+
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
77
|
+
- [@thi.ng/defmulti](https://github.com/thi-ng/umbrella/tree/develop/packages/defmulti)
|
|
78
|
+
- [@thi.ng/hiccup-html](https://github.com/thi-ng/umbrella/tree/develop/packages/hiccup-html)
|
|
79
|
+
- [@thi.ng/rdom](https://github.com/thi-ng/umbrella/tree/develop/packages/rdom)
|
|
80
|
+
- [@thi.ng/rstream](https://github.com/thi-ng/umbrella/tree/develop/packages/rstream)
|
|
81
|
+
|
|
82
|
+
## Usage examples
|
|
83
|
+
|
|
84
|
+
One project in this repo's
|
|
85
|
+
[/examples](https://github.com/thi-ng/umbrella/tree/develop/examples)
|
|
86
|
+
directory is using this package:
|
|
87
|
+
|
|
88
|
+
| Screenshot | Description | Live demo | Source |
|
|
89
|
+
|:--------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------|:---------------------------------------------------|:--------------------------------------------------------------------------------|
|
|
90
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/rdom-formgen.jpg" width="240"/> | Basic usage of the declarative rdom-forms generator | [Demo](https://demo.thi.ng/umbrella/rdom-formgen/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/rdom-formgen) |
|
|
91
|
+
|
|
92
|
+
## API
|
|
93
|
+
|
|
94
|
+
[Generated API docs](https://docs.thi.ng/umbrella/rdom-forms/)
|
|
95
|
+
|
|
96
|
+
TODO
|
|
97
|
+
|
|
98
|
+
## Authors
|
|
99
|
+
|
|
100
|
+
- [Karsten Schmidt](https://thi.ng)
|
|
101
|
+
|
|
102
|
+
If this project contributes to an academic publication, please cite it as:
|
|
103
|
+
|
|
104
|
+
```bibtex
|
|
105
|
+
@misc{thing-rdom-forms,
|
|
106
|
+
title = "@thi.ng/rdom-forms",
|
|
107
|
+
author = "Karsten Schmidt",
|
|
108
|
+
note = "https://thi.ng/rdom-forms",
|
|
109
|
+
year = 2023
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
© 2023 Karsten Schmidt // Apache License 2.0
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import type { Predicate } from "@thi.ng/api";
|
|
2
|
+
import type { Attribs, FormAttribs, InputFileAttribs, InputRadioAttribs } from "@thi.ng/hiccup-html";
|
|
3
|
+
import type { ComponentLike } from "@thi.ng/rdom";
|
|
4
|
+
import type { ISubscriber, ISubscription } from "@thi.ng/rstream";
|
|
5
|
+
export interface CommonAttribs {
|
|
6
|
+
wrapperAttribs: Partial<Attribs>;
|
|
7
|
+
labelAttribs: Partial<Attribs>;
|
|
8
|
+
descAttribs: Partial<Attribs>;
|
|
9
|
+
}
|
|
10
|
+
export interface FormItem {
|
|
11
|
+
/**
|
|
12
|
+
* Value/widget type identifier. Used by {@link compileForm} to delegate
|
|
13
|
+
*/
|
|
14
|
+
type: string;
|
|
15
|
+
/**
|
|
16
|
+
* Attrib overrides for the actual input element.
|
|
17
|
+
*/
|
|
18
|
+
attribs?: Partial<Attribs>;
|
|
19
|
+
}
|
|
20
|
+
export interface Form {
|
|
21
|
+
type: "form";
|
|
22
|
+
items: FormItem[];
|
|
23
|
+
attribs?: Partial<FormAttribs>;
|
|
24
|
+
}
|
|
25
|
+
export interface Container extends FormItem {
|
|
26
|
+
type: "container";
|
|
27
|
+
items: FormItem[];
|
|
28
|
+
}
|
|
29
|
+
export interface Custom extends Pick<FormItem, "type"> {
|
|
30
|
+
type: "custom";
|
|
31
|
+
body: ComponentLike;
|
|
32
|
+
}
|
|
33
|
+
export interface Group extends FormItem {
|
|
34
|
+
type: "group";
|
|
35
|
+
items: FormItem[];
|
|
36
|
+
label?: string | false;
|
|
37
|
+
}
|
|
38
|
+
export interface Value extends FormItem, Partial<CommonAttribs> {
|
|
39
|
+
id: string;
|
|
40
|
+
name?: string;
|
|
41
|
+
label?: string | false;
|
|
42
|
+
desc?: any;
|
|
43
|
+
required?: boolean;
|
|
44
|
+
readonly?: boolean;
|
|
45
|
+
}
|
|
46
|
+
export interface WithPresets<T> {
|
|
47
|
+
/**
|
|
48
|
+
* Array of possible preset values.
|
|
49
|
+
*/
|
|
50
|
+
list?: T[];
|
|
51
|
+
}
|
|
52
|
+
export interface Num extends Value, WithPresets<number> {
|
|
53
|
+
type: "num";
|
|
54
|
+
min?: number;
|
|
55
|
+
max?: number;
|
|
56
|
+
placeholder?: string;
|
|
57
|
+
size?: number;
|
|
58
|
+
step?: number;
|
|
59
|
+
value?: ISubscription<number, number>;
|
|
60
|
+
}
|
|
61
|
+
export interface Range extends Omit<Num, "type" | "placeholder" | "size"> {
|
|
62
|
+
type: "range";
|
|
63
|
+
vlabel?: boolean;
|
|
64
|
+
vlabelPrec?: number;
|
|
65
|
+
}
|
|
66
|
+
export interface Str extends Value, WithPresets<string> {
|
|
67
|
+
type: "str";
|
|
68
|
+
min?: number;
|
|
69
|
+
max?: number;
|
|
70
|
+
pattern?: string | RegExp | Predicate<string>;
|
|
71
|
+
placeholder?: string;
|
|
72
|
+
size?: number;
|
|
73
|
+
value?: ISubscription<string, string>;
|
|
74
|
+
}
|
|
75
|
+
export interface Email extends Omit<Str, "type"> {
|
|
76
|
+
type: "email";
|
|
77
|
+
autocomplete?: boolean;
|
|
78
|
+
}
|
|
79
|
+
export interface Phone extends Omit<Str, "type"> {
|
|
80
|
+
type: "tel";
|
|
81
|
+
autocomplete?: boolean;
|
|
82
|
+
}
|
|
83
|
+
export interface UrlVal extends Omit<Str, "type"> {
|
|
84
|
+
type: "url";
|
|
85
|
+
}
|
|
86
|
+
export interface Password extends Omit<Str, "type"> {
|
|
87
|
+
type: "password";
|
|
88
|
+
autocomplete?: boolean;
|
|
89
|
+
}
|
|
90
|
+
export interface Text extends Value {
|
|
91
|
+
type: "text";
|
|
92
|
+
cols?: number;
|
|
93
|
+
rows?: number;
|
|
94
|
+
placeholder?: string;
|
|
95
|
+
value?: ISubscription<string, string>;
|
|
96
|
+
}
|
|
97
|
+
export interface Color extends Value, WithPresets<string> {
|
|
98
|
+
type: "color";
|
|
99
|
+
value?: ISubscription<string, string>;
|
|
100
|
+
}
|
|
101
|
+
export interface DateTime extends Value, WithPresets<string> {
|
|
102
|
+
type: "dateTime";
|
|
103
|
+
min?: string;
|
|
104
|
+
max?: string;
|
|
105
|
+
step?: number;
|
|
106
|
+
value?: ISubscription<string, string>;
|
|
107
|
+
}
|
|
108
|
+
export interface DateVal extends Omit<DateTime, "type"> {
|
|
109
|
+
type: "date";
|
|
110
|
+
}
|
|
111
|
+
export interface Time extends Omit<DateTime, "type"> {
|
|
112
|
+
type: "time";
|
|
113
|
+
}
|
|
114
|
+
export interface Week extends Omit<DateTime, "type" | "list"> {
|
|
115
|
+
type: "week";
|
|
116
|
+
}
|
|
117
|
+
export interface Month extends Omit<DateTime, "type" | "list"> {
|
|
118
|
+
type: "month";
|
|
119
|
+
}
|
|
120
|
+
export interface Select<T> extends Value {
|
|
121
|
+
items: (T | SelectItem<T> | SelectItemGroup<T>)[];
|
|
122
|
+
value?: ISubscription<T, T>;
|
|
123
|
+
}
|
|
124
|
+
export interface SelectItemGroup<T> {
|
|
125
|
+
name: string;
|
|
126
|
+
items: (T | SelectItem<T>)[];
|
|
127
|
+
}
|
|
128
|
+
export interface SelectItem<T> {
|
|
129
|
+
value: T;
|
|
130
|
+
label?: string;
|
|
131
|
+
desc?: string;
|
|
132
|
+
}
|
|
133
|
+
export interface SelectStr extends Select<string> {
|
|
134
|
+
type: "selectStr";
|
|
135
|
+
}
|
|
136
|
+
export interface SelectNum extends Select<number> {
|
|
137
|
+
type: "selectNum";
|
|
138
|
+
}
|
|
139
|
+
export interface MultiSelect<T> extends Value {
|
|
140
|
+
items: (T | SelectItem<T> | SelectItemGroup<T>)[];
|
|
141
|
+
value?: ISubscription<T[], T[]>;
|
|
142
|
+
size?: number;
|
|
143
|
+
}
|
|
144
|
+
export interface MultiSelectStr extends MultiSelect<string> {
|
|
145
|
+
type: "multiSelectStr";
|
|
146
|
+
}
|
|
147
|
+
export interface MultiSelectNum extends MultiSelect<number> {
|
|
148
|
+
type: "multiSelectNum";
|
|
149
|
+
}
|
|
150
|
+
export interface Toggle extends Value {
|
|
151
|
+
type: "toggle";
|
|
152
|
+
value?: ISubscription<boolean, boolean>;
|
|
153
|
+
}
|
|
154
|
+
export interface Trigger extends Value {
|
|
155
|
+
type: "trigger";
|
|
156
|
+
title: string;
|
|
157
|
+
value?: ISubscriber<boolean>;
|
|
158
|
+
}
|
|
159
|
+
export interface Radio<T> extends Value {
|
|
160
|
+
items: (T | SelectItem<T>)[];
|
|
161
|
+
value?: ISubscription<T, T>;
|
|
162
|
+
}
|
|
163
|
+
export interface RadioNum extends Radio<number> {
|
|
164
|
+
type: "radioNum";
|
|
165
|
+
}
|
|
166
|
+
export interface RadioStr extends Radio<string> {
|
|
167
|
+
type: "radioStr";
|
|
168
|
+
}
|
|
169
|
+
export interface FileVal extends Value {
|
|
170
|
+
type: "file";
|
|
171
|
+
accept?: string[];
|
|
172
|
+
capture?: InputFileAttribs["capture"];
|
|
173
|
+
value?: ISubscriber<File>;
|
|
174
|
+
}
|
|
175
|
+
export interface MultiFileVal extends Value {
|
|
176
|
+
type: "multiFile";
|
|
177
|
+
accept?: string[];
|
|
178
|
+
value?: ISubscriber<FileList>;
|
|
179
|
+
}
|
|
180
|
+
type KnownTypes = Color | Container | DateTime | DateVal | Email | FileVal | Group | Month | MultiFileVal | MultiSelectNum | MultiSelectStr | Num | Password | Phone | Range | SelectNum | SelectStr | Str | Text | Time | Toggle | Trigger | UrlVal | Week;
|
|
181
|
+
/**
|
|
182
|
+
* Type specific attribute overrides
|
|
183
|
+
*/
|
|
184
|
+
export interface TypeAttribs extends Record<KnownTypes["type"], Partial<Attribs>> {
|
|
185
|
+
form: Partial<FormAttribs>;
|
|
186
|
+
/**
|
|
187
|
+
* Attribs for {@link group} label elements
|
|
188
|
+
*/
|
|
189
|
+
groupLabel: Partial<Attribs>;
|
|
190
|
+
/**
|
|
191
|
+
* Attribs for the actual {@link radio} `<input>` element
|
|
192
|
+
*/
|
|
193
|
+
radio: Partial<InputRadioAttribs>;
|
|
194
|
+
/**
|
|
195
|
+
* Attribs for the outermost {@link radio} group wrapper element (incl. main
|
|
196
|
+
* group label)
|
|
197
|
+
*/
|
|
198
|
+
radioWrapper: Partial<Attribs>;
|
|
199
|
+
/**
|
|
200
|
+
* Attribs for the wrapper element only containing the {@link radio} items.
|
|
201
|
+
*/
|
|
202
|
+
radioItems: Partial<Attribs>;
|
|
203
|
+
/**
|
|
204
|
+
* Attribs for the wrapper element of a single {@link radio} item (incl.
|
|
205
|
+
* input element and its label)
|
|
206
|
+
*/
|
|
207
|
+
radioItem: Partial<Attribs>;
|
|
208
|
+
/**
|
|
209
|
+
* Attribs for a single {@link radio} item's label
|
|
210
|
+
*/
|
|
211
|
+
radioItemLabel: Partial<Attribs>;
|
|
212
|
+
/**
|
|
213
|
+
* Attribs for {@link range} label elements
|
|
214
|
+
*/
|
|
215
|
+
rangeLabelAttribs: Partial<Attribs>;
|
|
216
|
+
[id: string]: Partial<Attribs>;
|
|
217
|
+
}
|
|
218
|
+
export interface FormOpts extends CommonAttribs {
|
|
219
|
+
/**
|
|
220
|
+
* ID prefix to prepend for all {@link FormItem}s. Needed if the a form is
|
|
221
|
+
* to used in multiple places at the same time.
|
|
222
|
+
*/
|
|
223
|
+
prefix: string;
|
|
224
|
+
/**
|
|
225
|
+
* Type specific behavior options.
|
|
226
|
+
*/
|
|
227
|
+
behaviors: Partial<BehaviorOpts>;
|
|
228
|
+
/**
|
|
229
|
+
* Type specific attrib overrides
|
|
230
|
+
*/
|
|
231
|
+
typeAttribs: Partial<TypeAttribs>;
|
|
232
|
+
}
|
|
233
|
+
export interface BehaviorOpts {
|
|
234
|
+
/**
|
|
235
|
+
* If false, no label elements will be generated.
|
|
236
|
+
*
|
|
237
|
+
* @defaultValue true
|
|
238
|
+
*/
|
|
239
|
+
labels: boolean;
|
|
240
|
+
/**
|
|
241
|
+
* If false, no `value` attributes and event handlers will be generated.
|
|
242
|
+
*
|
|
243
|
+
* @defaultValue true
|
|
244
|
+
*/
|
|
245
|
+
values: boolean;
|
|
246
|
+
/**
|
|
247
|
+
* Unless false, {@link range} widgets will emit `oninput` events, if false
|
|
248
|
+
* only `onchange` events.
|
|
249
|
+
*
|
|
250
|
+
* @defaultValue true
|
|
251
|
+
*/
|
|
252
|
+
rangeOnInput: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* Unless false, {@link str}-like widgets will emit `oninput` events, if
|
|
255
|
+
* false only `onchange` events.
|
|
256
|
+
*
|
|
257
|
+
* @defaultValue true
|
|
258
|
+
*/
|
|
259
|
+
strOnInput: boolean;
|
|
260
|
+
/**
|
|
261
|
+
* Unless false, {@link text} elements will emit `oninput` events, if false
|
|
262
|
+
* only `onchange` events.
|
|
263
|
+
*
|
|
264
|
+
* @defaultValue true
|
|
265
|
+
*/
|
|
266
|
+
textOnInput: boolean;
|
|
267
|
+
/**
|
|
268
|
+
* If true, the label for individual radio items will come before the actual
|
|
269
|
+
* input element. By default, the order is reversed.
|
|
270
|
+
*
|
|
271
|
+
* @defaultValue false
|
|
272
|
+
*/
|
|
273
|
+
radioLabelBefore: boolean;
|
|
274
|
+
toggleLabelBefore: boolean;
|
|
275
|
+
}
|
|
276
|
+
export {};
|
|
277
|
+
//# sourceMappingURL=api.d.ts.map
|
package/api.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/compile.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { MultiFn2 } from "@thi.ng/defmulti";
|
|
2
|
+
import type { Attribs, FormAttribs } from "@thi.ng/hiccup-html";
|
|
3
|
+
import { type ComponentLike } from "@thi.ng/rdom";
|
|
4
|
+
import type { ISubscribable } from "@thi.ng/rstream";
|
|
5
|
+
import type { Color, Container, Custom, DateTime, DateVal, Email, FileVal, Form, FormItem, FormOpts, Group, Month, MultiFileVal, MultiSelectNum, MultiSelectStr, Num, Password, RadioNum, RadioStr, Range, SelectNum, SelectStr, Str, Text, Time, Toggle, Trigger, UrlVal, Value, Week } from "./api.js";
|
|
6
|
+
export declare const form: (attribs: Partial<FormAttribs>, ...items: FormItem[]) => Form;
|
|
7
|
+
export declare const container: (attribs: Partial<Attribs>, ...items: FormItem[]) => Container;
|
|
8
|
+
export declare const group: (spec: Omit<Group, "type" | "id" | "items"> & {
|
|
9
|
+
id?: string;
|
|
10
|
+
}, ...items: FormItem[]) => Group;
|
|
11
|
+
export declare const custom: (body: ComponentLike) => Custom;
|
|
12
|
+
type PartialSpec<T extends Value> = Omit<T, "type" | "id"> & {
|
|
13
|
+
id?: string;
|
|
14
|
+
};
|
|
15
|
+
type ReadonlyPartialSpec<T extends Value, V = string> = Omit<T, "type" | "id" | "readonly" | "value"> & {
|
|
16
|
+
id?: string;
|
|
17
|
+
readonly: true;
|
|
18
|
+
value?: ISubscribable<V>;
|
|
19
|
+
};
|
|
20
|
+
export declare const color: (spec: PartialSpec<Color> | ReadonlyPartialSpec<Color, string>) => Color;
|
|
21
|
+
export declare const date: (spec: PartialSpec<DateVal> | ReadonlyPartialSpec<DateVal, string>) => DateVal;
|
|
22
|
+
export declare const dateTime: (spec: PartialSpec<DateTime> | ReadonlyPartialSpec<DateTime, string>) => DateTime;
|
|
23
|
+
export declare const email: (spec: PartialSpec<Email> | ReadonlyPartialSpec<Email, string>) => Email;
|
|
24
|
+
export declare const file: (spec: PartialSpec<FileVal> | ReadonlyPartialSpec<FileVal, never>) => FileVal;
|
|
25
|
+
export declare const month: (spec: PartialSpec<Month> | ReadonlyPartialSpec<Month, string>) => Month;
|
|
26
|
+
export declare const multiFile: (spec: PartialSpec<MultiFileVal> | ReadonlyPartialSpec<MultiFileVal, never>) => MultiFileVal;
|
|
27
|
+
export declare const multiSelectNum: (spec: PartialSpec<MultiSelectNum> | ReadonlyPartialSpec<MultiSelectNum, number>) => MultiSelectNum;
|
|
28
|
+
export declare const multiSelectStr: (spec: PartialSpec<MultiSelectStr> | ReadonlyPartialSpec<MultiSelectStr, string>) => MultiSelectStr;
|
|
29
|
+
export declare const num: (spec: PartialSpec<Num> | ReadonlyPartialSpec<Num, number>) => Num;
|
|
30
|
+
export declare const password: (spec: PartialSpec<Password> | ReadonlyPartialSpec<Password, string>) => Password;
|
|
31
|
+
export declare const phone: (spec: PartialSpec<Email> | ReadonlyPartialSpec<Email, string>) => Email;
|
|
32
|
+
export declare const radioNum: (spec: PartialSpec<RadioNum> | ReadonlyPartialSpec<RadioNum, number>) => RadioNum;
|
|
33
|
+
export declare const radioStr: (spec: PartialSpec<RadioStr> | ReadonlyPartialSpec<RadioStr, string>) => RadioStr;
|
|
34
|
+
export declare const range: (spec: PartialSpec<Range> | ReadonlyPartialSpec<Range, number>) => Range;
|
|
35
|
+
export declare const search: (spec: PartialSpec<Str> | ReadonlyPartialSpec<Str, string>) => Str;
|
|
36
|
+
export declare const selectNum: (spec: PartialSpec<SelectNum> | ReadonlyPartialSpec<SelectNum, number>) => SelectNum;
|
|
37
|
+
export declare const selectStr: (spec: PartialSpec<SelectStr> | ReadonlyPartialSpec<SelectStr, string>) => SelectStr;
|
|
38
|
+
export declare const str: (spec: PartialSpec<Str> | ReadonlyPartialSpec<Str, string>) => Str;
|
|
39
|
+
export declare const text: (spec: PartialSpec<Text> | ReadonlyPartialSpec<Text, string>) => Text;
|
|
40
|
+
export declare const time: (spec: PartialSpec<Time> | ReadonlyPartialSpec<Time, string>) => Time;
|
|
41
|
+
export declare const toggle: (spec: PartialSpec<Toggle> | ReadonlyPartialSpec<Toggle, boolean>) => Toggle;
|
|
42
|
+
export declare const trigger: (spec: PartialSpec<Trigger> | ReadonlyPartialSpec<Trigger, string>) => Trigger;
|
|
43
|
+
export declare const url: (spec: PartialSpec<UrlVal> | ReadonlyPartialSpec<UrlVal, string>) => UrlVal;
|
|
44
|
+
export declare const week: (spec: PartialSpec<Week> | ReadonlyPartialSpec<Week, string>) => Week;
|
|
45
|
+
/**
|
|
46
|
+
* Compiles given {@link FormItem} spec into a hiccup/rdom component, using
|
|
47
|
+
* provided options to customize attributes and behaviors.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* This function is polymorphic and dynamically extensible for new/custom form
|
|
51
|
+
* element types. See thi.ng/defmulti readme for instructions.
|
|
52
|
+
*/
|
|
53
|
+
export declare const compileForm: MultiFn2<FormItem, Partial<FormOpts>, ComponentLike>;
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=compile.d.ts.map
|
package/compile.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { isArray } from "@thi.ng/checks/is-array";
|
|
2
|
+
import { isFunction } from "@thi.ng/checks/is-function";
|
|
3
|
+
import { isPlainObject } from "@thi.ng/checks/is-plain-object";
|
|
4
|
+
import { isString } from "@thi.ng/checks/is-string";
|
|
5
|
+
import { defmulti } from "@thi.ng/defmulti/defmulti";
|
|
6
|
+
import { div } from "@thi.ng/hiccup-html/blocks";
|
|
7
|
+
import { form as $form, button, checkbox, fieldset, inputColor, inputFile, inputNumber, inputRange, inputText, label, legend, optGroup, option, radio, select, textArea, } from "@thi.ng/hiccup-html/forms";
|
|
8
|
+
import { span } from "@thi.ng/hiccup-html/inline";
|
|
9
|
+
import { datalist } from "@thi.ng/hiccup-html/lists";
|
|
10
|
+
import { $attribs, $input, $inputCheckbox, $inputFile, $inputFiles, $inputNum, $inputTrigger, $replace, } from "@thi.ng/rdom";
|
|
11
|
+
export const form = (attribs, ...items) => ({
|
|
12
|
+
type: "form",
|
|
13
|
+
attribs,
|
|
14
|
+
items,
|
|
15
|
+
});
|
|
16
|
+
export const container = (attribs, ...items) => ({
|
|
17
|
+
type: "container",
|
|
18
|
+
attribs,
|
|
19
|
+
items,
|
|
20
|
+
});
|
|
21
|
+
export const group = (spec, ...items) => ({
|
|
22
|
+
...spec,
|
|
23
|
+
type: "group",
|
|
24
|
+
items,
|
|
25
|
+
});
|
|
26
|
+
export const custom = (body) => ({
|
|
27
|
+
type: "custom",
|
|
28
|
+
body,
|
|
29
|
+
});
|
|
30
|
+
let __nextID = 0;
|
|
31
|
+
const $ = (type, defaults) => (spec) => ({
|
|
32
|
+
id: spec.id || `${type}-${__nextID++}`,
|
|
33
|
+
type,
|
|
34
|
+
...defaults,
|
|
35
|
+
...spec,
|
|
36
|
+
});
|
|
37
|
+
export const color = $("color");
|
|
38
|
+
export const date = $("date");
|
|
39
|
+
export const dateTime = $("dateTime");
|
|
40
|
+
export const email = $("email", { autocomplete: true });
|
|
41
|
+
export const file = $("file");
|
|
42
|
+
export const month = $("month");
|
|
43
|
+
export const multiFile = $("multiFile");
|
|
44
|
+
export const multiSelectNum = $("multiSelectNum");
|
|
45
|
+
export const multiSelectStr = $("multiSelectStr");
|
|
46
|
+
export const num = $("num");
|
|
47
|
+
export const password = $("password", { autocomplete: true });
|
|
48
|
+
export const phone = $("tel", { autocomplete: true });
|
|
49
|
+
export const radioNum = $("radioNum");
|
|
50
|
+
export const radioStr = $("radioStr");
|
|
51
|
+
export const range = $("range");
|
|
52
|
+
export const search = $("search");
|
|
53
|
+
export const selectNum = $("selectNum");
|
|
54
|
+
export const selectStr = $("selectStr");
|
|
55
|
+
export const str = $("str");
|
|
56
|
+
export const text = $("text");
|
|
57
|
+
export const time = $("time");
|
|
58
|
+
export const toggle = $("toggle");
|
|
59
|
+
export const trigger = $("trigger");
|
|
60
|
+
export const url = $("url");
|
|
61
|
+
export const week = $("week");
|
|
62
|
+
/** @internal */
|
|
63
|
+
const __genID = (id, opts) => opts.prefix ? opts.prefix + id : id;
|
|
64
|
+
/** @internal */
|
|
65
|
+
const __genLabel = (x, opts) => label({ ...opts.labelAttribs, ...x.labelAttribs, for: __genID(x.id, opts) }, x.label ?? x.id, x.desc ? span({ ...opts.descAttribs, ...x.descAttribs }, x.desc) : null);
|
|
66
|
+
/** @internal */
|
|
67
|
+
const __genList = (id, list) => datalist({ id: id + "--list" }, ...list.map((value) => option({ value })));
|
|
68
|
+
/** @internal */
|
|
69
|
+
const __genCommon = (val, opts) => {
|
|
70
|
+
const res = [];
|
|
71
|
+
if (val.label !== false && opts.behaviors?.labels !== false) {
|
|
72
|
+
res.push(__genLabel(val, opts));
|
|
73
|
+
}
|
|
74
|
+
if (val.list) {
|
|
75
|
+
res.push(__genList(__genID(val.id, opts), val.list));
|
|
76
|
+
}
|
|
77
|
+
return res;
|
|
78
|
+
};
|
|
79
|
+
/** @internal */
|
|
80
|
+
const __attribs = (attribs, events, val, opts, value = "value") => {
|
|
81
|
+
const id = __genID(val.id, opts);
|
|
82
|
+
Object.assign(attribs, {
|
|
83
|
+
id,
|
|
84
|
+
name: val.name || val.id,
|
|
85
|
+
list: val.list ? id + "--list" : undefined,
|
|
86
|
+
required: val.required,
|
|
87
|
+
readonly: val.readonly,
|
|
88
|
+
}, val.attribs);
|
|
89
|
+
if (__useValues(opts)) {
|
|
90
|
+
if (!val.readonly) {
|
|
91
|
+
Object.assign(attribs, events);
|
|
92
|
+
}
|
|
93
|
+
if (value !== false) {
|
|
94
|
+
attribs[value] = val.value;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return attribs;
|
|
98
|
+
};
|
|
99
|
+
/** @internal */
|
|
100
|
+
const __component = (val, opts, el, attribs, events, value = "value", ...body) => div({ ...opts.wrapperAttribs, ...val.wrapperAttribs }, ...__genCommon(val, opts),
|
|
101
|
+
// @ts-ignore extra args
|
|
102
|
+
el(__attribs(attribs, events, val, opts, value), ...body));
|
|
103
|
+
/** @internal */
|
|
104
|
+
const __edit = (val) => {
|
|
105
|
+
if (val.pattern) {
|
|
106
|
+
let match;
|
|
107
|
+
if (isFunction(val.pattern)) {
|
|
108
|
+
match = val.pattern;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const re = isString(val.pattern)
|
|
112
|
+
? new RegExp(val.pattern)
|
|
113
|
+
: val.pattern;
|
|
114
|
+
match = (x) => re.test(x);
|
|
115
|
+
}
|
|
116
|
+
return (e) => {
|
|
117
|
+
const target = e.target;
|
|
118
|
+
const body = target.value;
|
|
119
|
+
const ok = match(body);
|
|
120
|
+
if (ok)
|
|
121
|
+
val.value.next(body);
|
|
122
|
+
$attribs(target, { invalid: !ok });
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return $input(val.value);
|
|
126
|
+
};
|
|
127
|
+
const __useValues = (opts) => opts.behaviors?.values !== false;
|
|
128
|
+
/**
|
|
129
|
+
* Compiles given {@link FormItem} spec into a hiccup/rdom component, using
|
|
130
|
+
* provided options to customize attributes and behaviors.
|
|
131
|
+
*
|
|
132
|
+
* @remarks
|
|
133
|
+
* This function is polymorphic and dynamically extensible for new/custom form
|
|
134
|
+
* element types. See thi.ng/defmulti readme for instructions.
|
|
135
|
+
*/
|
|
136
|
+
export const compileForm = defmulti((x) => x.type, {
|
|
137
|
+
multiFile: "file",
|
|
138
|
+
dateTime: "date",
|
|
139
|
+
time: "date",
|
|
140
|
+
week: "date",
|
|
141
|
+
month: "date",
|
|
142
|
+
email: "str",
|
|
143
|
+
password: "str",
|
|
144
|
+
tel: "str",
|
|
145
|
+
search: "str",
|
|
146
|
+
url: "str",
|
|
147
|
+
radioNum: "radio",
|
|
148
|
+
radioStr: "radio",
|
|
149
|
+
selectNum: "select",
|
|
150
|
+
selectStr: "select",
|
|
151
|
+
multiSelectNum: "multiSelect",
|
|
152
|
+
multiSelectStr: "multiSelect",
|
|
153
|
+
}, {
|
|
154
|
+
form: ($val, opts) => {
|
|
155
|
+
const val = $val;
|
|
156
|
+
return $form({ ...opts.typeAttribs?.form, ...val.attribs }, ...val.items.map((x) => compileForm(x, opts)));
|
|
157
|
+
},
|
|
158
|
+
container: ($val, opts) => {
|
|
159
|
+
const val = $val;
|
|
160
|
+
return div({ ...opts.typeAttribs?.container, ...val.attribs }, ...val.items.map((x) => compileForm(x, opts)));
|
|
161
|
+
},
|
|
162
|
+
group: ($val, opts) => {
|
|
163
|
+
const val = $val;
|
|
164
|
+
const children = [];
|
|
165
|
+
if (val.label) {
|
|
166
|
+
children.push(legend({ ...opts.typeAttribs?.groupLabel }, val.label));
|
|
167
|
+
}
|
|
168
|
+
return fieldset({ ...opts.typeAttribs?.group, ...val.attribs }, ...children, ...val.items.map((x) => compileForm(x, opts)));
|
|
169
|
+
},
|
|
170
|
+
custom: (val) => val.body,
|
|
171
|
+
toggle: ($val, opts) => {
|
|
172
|
+
const val = $val;
|
|
173
|
+
const label = __genLabel(val, opts);
|
|
174
|
+
const ctrl = checkbox(__attribs({ ...opts.typeAttribs?.toggle }, { onchange: $inputCheckbox($val.value) }, val, opts, "checked"));
|
|
175
|
+
return div({ ...opts.wrapperAttribs, ...val.wrapperAttribs }, ...(opts.behaviors?.toggleLabelBefore !== false
|
|
176
|
+
? [label, ctrl]
|
|
177
|
+
: [ctrl, label]));
|
|
178
|
+
},
|
|
179
|
+
trigger: ($val, opts) => __component($val, opts, button, { ...opts.typeAttribs?.trigger }, { onchange: $inputTrigger($val.value) }, false, $val.title),
|
|
180
|
+
radio: ($val, opts) => {
|
|
181
|
+
const val = $val;
|
|
182
|
+
const labelAttribs = {
|
|
183
|
+
...opts.typeAttribs?.radioItemLabel,
|
|
184
|
+
...val.labelAttribs,
|
|
185
|
+
};
|
|
186
|
+
const $option = ($item) => {
|
|
187
|
+
const item = isPlainObject($item) ? $item : { value: $item };
|
|
188
|
+
const id = val.id + "-" + item.value;
|
|
189
|
+
const label = __genLabel({
|
|
190
|
+
id,
|
|
191
|
+
label: item.label || item.value,
|
|
192
|
+
desc: item.desc,
|
|
193
|
+
labelAttribs,
|
|
194
|
+
descAttribs: val.descAttribs,
|
|
195
|
+
}, opts);
|
|
196
|
+
const ctrl = radio({
|
|
197
|
+
...opts.typeAttribs?.radio,
|
|
198
|
+
...val.attribs,
|
|
199
|
+
onchange: val.value && __useValues(opts)
|
|
200
|
+
? () => val.value.next(item.value)
|
|
201
|
+
: undefined,
|
|
202
|
+
id: __genID(id, opts),
|
|
203
|
+
name: val.name || val.id,
|
|
204
|
+
checked: val.value && __useValues(opts)
|
|
205
|
+
? val.value.map((x) => x === item.value)
|
|
206
|
+
: undefined,
|
|
207
|
+
value: item.value,
|
|
208
|
+
});
|
|
209
|
+
return div({ ...opts.typeAttribs?.radioItem }, ...(opts.behaviors?.radioLabelBefore
|
|
210
|
+
? [label, ctrl]
|
|
211
|
+
: [ctrl, label]));
|
|
212
|
+
};
|
|
213
|
+
return div({
|
|
214
|
+
...opts.wrapperAttribs,
|
|
215
|
+
...opts.typeAttribs?.radioWrapper,
|
|
216
|
+
...val.wrapperAttribs,
|
|
217
|
+
}, ...__genCommon(val, opts), div({ ...opts.typeAttribs?.radioItems }, ...val.items.map($option)));
|
|
218
|
+
},
|
|
219
|
+
color: ($val, opts) => __component($val, opts, inputColor, { ...opts.typeAttribs?.color }, { onchange: $input($val.value) }),
|
|
220
|
+
file: ($val, opts) => {
|
|
221
|
+
const val = $val;
|
|
222
|
+
const isMulti = val.id.startsWith("multi");
|
|
223
|
+
return __component(val, opts, inputFile, {
|
|
224
|
+
...opts.typeAttribs?.num,
|
|
225
|
+
accept: val.accept,
|
|
226
|
+
capture: val.capture,
|
|
227
|
+
multiple: isMulti,
|
|
228
|
+
}, {
|
|
229
|
+
onchange: isMulti
|
|
230
|
+
? $inputFiles($val.value)
|
|
231
|
+
: $inputFile(val.value),
|
|
232
|
+
}, false);
|
|
233
|
+
},
|
|
234
|
+
num: ($val, opts) => {
|
|
235
|
+
const val = $val;
|
|
236
|
+
return __component(val, opts, inputNumber, {
|
|
237
|
+
...opts.typeAttribs?.num,
|
|
238
|
+
min: val.min,
|
|
239
|
+
max: val.max,
|
|
240
|
+
step: val.step,
|
|
241
|
+
placeholder: val.placeholder,
|
|
242
|
+
size: val.size,
|
|
243
|
+
}, { onchange: $inputNum(val.value) });
|
|
244
|
+
},
|
|
245
|
+
range: ($val, opts) => {
|
|
246
|
+
const val = $val;
|
|
247
|
+
const edit = opts.behaviors?.rangeOnInput === false ? "onchange" : "oninput";
|
|
248
|
+
return div({ ...opts.wrapperAttribs, ...val.wrapperAttribs }, ...__genCommon(val, opts), div({}, inputRange(__attribs({
|
|
249
|
+
...opts.typeAttribs?.range,
|
|
250
|
+
min: val.min,
|
|
251
|
+
max: val.max,
|
|
252
|
+
step: val.step,
|
|
253
|
+
}, { [edit]: $inputNum(val.value) }, val, opts)), val.value && val.vlabel !== false && __useValues(opts)
|
|
254
|
+
? span({ ...opts.typeAttribs?.rangeLabel }, val.value.map((x) => x.toFixed(val.vlabelPrec ?? 3)))
|
|
255
|
+
: undefined));
|
|
256
|
+
},
|
|
257
|
+
str: ($val, opts) => {
|
|
258
|
+
const val = $val;
|
|
259
|
+
const type = { dateTime: "datetime-local" }[$val.type] ||
|
|
260
|
+
($val.type !== "str" ? $val.type : "text");
|
|
261
|
+
const edit = opts.behaviors?.strOnInput === false ? "onchange" : "oninput";
|
|
262
|
+
return __component(val, opts, inputText, {
|
|
263
|
+
...(opts.typeAttribs?.[val.type] || opts.typeAttribs?.str),
|
|
264
|
+
type,
|
|
265
|
+
autocomplete: val.autocomplete,
|
|
266
|
+
minlength: val.min,
|
|
267
|
+
maxlength: val.max,
|
|
268
|
+
placeholder: val.placeholder,
|
|
269
|
+
pattern: isString(val.pattern) ? val.pattern : undefined,
|
|
270
|
+
size: val.size,
|
|
271
|
+
}, { [edit]: __edit(val) });
|
|
272
|
+
},
|
|
273
|
+
text: ($val, opts) => {
|
|
274
|
+
const val = $val;
|
|
275
|
+
const edit = opts.behaviors?.textOnInput === false ? "onchange" : "oninput";
|
|
276
|
+
return __component(val, opts, textArea, {
|
|
277
|
+
...opts.typeAttribs?.text,
|
|
278
|
+
cols: val.cols,
|
|
279
|
+
rows: val.rows,
|
|
280
|
+
placeholder: val.placeholder,
|
|
281
|
+
}, { [edit]: $input(val.value) });
|
|
282
|
+
},
|
|
283
|
+
date: ($val, opts) => {
|
|
284
|
+
const val = $val;
|
|
285
|
+
const type = { dateTime: "datetime-local" }[$val.type] || $val.type;
|
|
286
|
+
return __component(val, opts, inputText, {
|
|
287
|
+
...(opts.typeAttribs?.[$val.type] ||
|
|
288
|
+
opts.typeAttribs?.date),
|
|
289
|
+
type,
|
|
290
|
+
min: val.min,
|
|
291
|
+
max: val.max,
|
|
292
|
+
step: val.step,
|
|
293
|
+
}, { onchange: $input(val.value) });
|
|
294
|
+
},
|
|
295
|
+
select: ($val, opts) => {
|
|
296
|
+
const val = $val;
|
|
297
|
+
const isNumeric = val.type.endsWith("Num");
|
|
298
|
+
const $option = ($item, sel) => {
|
|
299
|
+
const item = isPlainObject($item) ? $item : { value: $item };
|
|
300
|
+
return option({
|
|
301
|
+
value: item.value,
|
|
302
|
+
selected: sel === item.value,
|
|
303
|
+
}, item.label || item.value);
|
|
304
|
+
};
|
|
305
|
+
const $select = (sel) => select(__attribs({
|
|
306
|
+
...(opts.typeAttribs?.[val.type] ||
|
|
307
|
+
opts.typeAttribs?.select),
|
|
308
|
+
}, {
|
|
309
|
+
onchange: isNumeric
|
|
310
|
+
? $inputNum(val.value)
|
|
311
|
+
: $input(val.value),
|
|
312
|
+
}, val, opts, false), ...val.items.map((item) => isPlainObject(item) && "items" in item
|
|
313
|
+
? optGroup({ label: item.name }, ...item.items.map((i) => $option(i, sel)))
|
|
314
|
+
: $option(item, sel)));
|
|
315
|
+
return div({ ...opts.wrapperAttribs, ...val.wrapperAttribs }, ...__genCommon(val, opts), val.value && __useValues(opts)
|
|
316
|
+
? $replace(val.value.map($select))
|
|
317
|
+
: $select());
|
|
318
|
+
},
|
|
319
|
+
multiSelect: ($val, opts) => {
|
|
320
|
+
const val = $val;
|
|
321
|
+
const isNumeric = val.type.endsWith("Num");
|
|
322
|
+
const coerce = isNumeric
|
|
323
|
+
? (x) => parseFloat(x.value)
|
|
324
|
+
: (x) => x.value;
|
|
325
|
+
const sel = val.value && __useValues(opts)
|
|
326
|
+
? val.value.map((x) => (isArray(x) ? x : [x]))
|
|
327
|
+
: null;
|
|
328
|
+
const $option = ($item) => {
|
|
329
|
+
const item = isPlainObject($item) ? $item : { value: $item };
|
|
330
|
+
return option({
|
|
331
|
+
value: item.value,
|
|
332
|
+
selected: sel
|
|
333
|
+
? sel.map(($sel) => $sel.includes(item.value))
|
|
334
|
+
: false,
|
|
335
|
+
}, item.label || item.value);
|
|
336
|
+
};
|
|
337
|
+
return __component(val, opts, select, {
|
|
338
|
+
...(opts.typeAttribs?.[val.type] ||
|
|
339
|
+
opts.typeAttribs?.multiSelect),
|
|
340
|
+
multiple: true,
|
|
341
|
+
size: val.size,
|
|
342
|
+
}, {
|
|
343
|
+
onchange: (e) => {
|
|
344
|
+
val.value.next([
|
|
345
|
+
...e.target
|
|
346
|
+
.selectedOptions,
|
|
347
|
+
].map(coerce));
|
|
348
|
+
},
|
|
349
|
+
}, false, ...val.items.map((item) => isPlainObject(item) && "items" in item
|
|
350
|
+
? optGroup({ label: item.name }, ...item.items.map($option))
|
|
351
|
+
: $option(item)));
|
|
352
|
+
},
|
|
353
|
+
});
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thi.ng/rdom-forms",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Data-driven declarative & extensible HTML form generation",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"typings": "./index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/thi-ng/umbrella/tree/develop/packages/rdom-forms#readme",
|
|
14
|
+
"funding": [
|
|
15
|
+
{
|
|
16
|
+
"type": "github",
|
|
17
|
+
"url": "https://github.com/sponsors/postspectacular"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://patreon.com/thing_umbrella"
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "yarn clean && tsc --declaration",
|
|
28
|
+
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
|
+
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
|
+
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
31
|
+
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
|
|
32
|
+
"pub": "yarn npm publish --access public",
|
|
33
|
+
"test": "bun test"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@thi.ng/api": "^8.9.11",
|
|
37
|
+
"@thi.ng/checks": "^3.4.11",
|
|
38
|
+
"@thi.ng/defmulti": "^3.0.9",
|
|
39
|
+
"@thi.ng/hiccup-html": "^2.3.0",
|
|
40
|
+
"@thi.ng/rdom": "^0.13.3",
|
|
41
|
+
"@thi.ng/rstream": "^8.2.13"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@microsoft/api-extractor": "^7.38.3",
|
|
45
|
+
"rimraf": "^5.0.5",
|
|
46
|
+
"tools": "^0.0.1",
|
|
47
|
+
"typedoc": "^0.25.4",
|
|
48
|
+
"typescript": "^5.3.2"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"typescript"
|
|
52
|
+
],
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"browser": {
|
|
57
|
+
"process": false,
|
|
58
|
+
"setTimeout": false
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=14"
|
|
62
|
+
},
|
|
63
|
+
"files": [
|
|
64
|
+
"./*.js",
|
|
65
|
+
"./*.d.ts"
|
|
66
|
+
],
|
|
67
|
+
"exports": {
|
|
68
|
+
".": {
|
|
69
|
+
"default": "./index.js"
|
|
70
|
+
},
|
|
71
|
+
"./api": {
|
|
72
|
+
"default": "./api.js"
|
|
73
|
+
},
|
|
74
|
+
"./compile": {
|
|
75
|
+
"default": "./compile.js"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"thi.ng": {
|
|
79
|
+
"parent": "@thi.ng/rdom",
|
|
80
|
+
"related": [
|
|
81
|
+
"rdom-components"
|
|
82
|
+
],
|
|
83
|
+
"status": "alpha",
|
|
84
|
+
"year": 2023
|
|
85
|
+
},
|
|
86
|
+
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
|
|
87
|
+
}
|