formbuilder-plugin-group 1.0.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/LICENSE +21 -0
- package/README.md +174 -0
- package/dist/group.mjs +378 -0
- package/dist/group.umd.js +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Chappell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# formbuilder-plugin-group
|
|
2
|
+
|
|
3
|
+
A **Group** field type for [formBuilder](https://formbuilder.online/) that bundles any number of
|
|
4
|
+
registered child fields into a single, optionally-repeatable set.
|
|
5
|
+
|
|
6
|
+
**▶ [Live demo](https://kevinchappell.github.io/formBuilder-plugin-group/)**
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Drag a single **Group** field onto the builder stage — the whole set moves as one unit.
|
|
13
|
+
- Inline child-field picker: add, reorder (drag handle), duplicate, or remove child fields
|
|
14
|
+
without leaving the builder.
|
|
15
|
+
- Per-child edit panels powered by the formBuilder edit-panel hook (see [Requirement](#requirement-formbuilderwith-edit-panel-hook) below).
|
|
16
|
+
- Clean nested serialisation: `getData()` returns `fields` as a proper JSON array, not a
|
|
17
|
+
stringified blob.
|
|
18
|
+
- formRender support: static rendering and repeatable instances with Add / Remove buttons,
|
|
19
|
+
enforced `min` / `max`, and bracket-style child input naming (`group[0][child]`).
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Requirement: formBuilder with edit-panel hook
|
|
24
|
+
|
|
25
|
+
The editor's per-child edit panels rely on a core hook (`generateAdvFields` /
|
|
26
|
+
`getAttrVals` / `updatePreview` / `stage.fbInstance`) that is **not yet in the published
|
|
27
|
+
npm package**. It will ship with formBuilder **>= 3.23.0**.
|
|
28
|
+
|
|
29
|
+
Until that release:
|
|
30
|
+
|
|
31
|
+
1. Clone and build the patched formBuilder branch locally:
|
|
32
|
+
```bash
|
|
33
|
+
git clone https://github.com/kevinchappell/formBuilder.git ~/Dev/formBuilder
|
|
34
|
+
cd ~/Dev/formBuilder
|
|
35
|
+
git checkout feat/edit-panel-hook
|
|
36
|
+
npm install
|
|
37
|
+
npm run build:dev # produces dist/form-builder.min.js
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
2. In your project, reference that local build instead of the npm package:
|
|
41
|
+
```html
|
|
42
|
+
<script src="/path/to/formBuilder/dist/form-builder.min.js"></script>
|
|
43
|
+
```
|
|
44
|
+
or, if you use a bundler, set up a path alias / `npm link`.
|
|
45
|
+
|
|
46
|
+
> formRender (rendering only, no editor) works with the **published** npm package and does
|
|
47
|
+
> **not** require the hook.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install formbuilder-plugin-group
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or download `dist/group.umd.js` and include it via a `<script>` tag.
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Script tag (UMD)
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<!-- 1. jQuery -->
|
|
67
|
+
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
|
68
|
+
<!-- 2. formBuilder WITH edit-panel hook (see Requirement above) -->
|
|
69
|
+
<script src="/path/to/form-builder.min.js"></script>
|
|
70
|
+
<!-- 3. Group plugin — must load AFTER formBuilder -->
|
|
71
|
+
<script src="node_modules/formbuilder-plugin-group/dist/group.umd.js"></script>
|
|
72
|
+
|
|
73
|
+
<div id="build"></div>
|
|
74
|
+
<script>
|
|
75
|
+
const fb = $('#build').formBuilder()
|
|
76
|
+
</script>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
For **formRender** only (no editor):
|
|
80
|
+
|
|
81
|
+
```html
|
|
82
|
+
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
|
83
|
+
<script src="node_modules/formBuilder/dist/form-render.min.js"></script>
|
|
84
|
+
<script src="node_modules/formbuilder-plugin-group/dist/group.umd.js"></script>
|
|
85
|
+
|
|
86
|
+
<form id="form"></form>
|
|
87
|
+
<script>
|
|
88
|
+
$('#form').formRender({
|
|
89
|
+
formData: [
|
|
90
|
+
{
|
|
91
|
+
type: 'group',
|
|
92
|
+
name: 'address',
|
|
93
|
+
label: 'Address',
|
|
94
|
+
repeatable: true,
|
|
95
|
+
min: 1,
|
|
96
|
+
max: 3,
|
|
97
|
+
addLabel: 'Add another address',
|
|
98
|
+
fields: [
|
|
99
|
+
{ type: 'text', name: 'street', label: 'Street' },
|
|
100
|
+
{ type: 'text', name: 'city', label: 'City' },
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
})
|
|
105
|
+
</script>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### ESM (bundler)
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
import 'formbuilder-plugin-group' // registers the control via window.fbControls
|
|
112
|
+
import $ from 'jquery'
|
|
113
|
+
|
|
114
|
+
const fb = $('#build').formBuilder()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Styles
|
|
118
|
+
|
|
119
|
+
Styles are bundled into the plugin script and injected automatically when it loads — there is no
|
|
120
|
+
separate stylesheet to include. To customise, override the `.group-editor`, `.group-child`,
|
|
121
|
+
`.fb-group`, and `.fb-group-instance` classes in your own CSS after the plugin loads.
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Group field options
|
|
126
|
+
|
|
127
|
+
These options appear in the formBuilder edit panel for a Group field and are also accepted
|
|
128
|
+
directly in `formData` / `formRender`.
|
|
129
|
+
|
|
130
|
+
| Option | Type | Default | Description |
|
|
131
|
+
|---|---|---|---|
|
|
132
|
+
| `label` | `string` | `"Group"` | Human-readable label shown above the group. |
|
|
133
|
+
| `name` | `string` | auto | Field name; used as the bracket-notation prefix for child inputs in formRender (`name[i][child]`). |
|
|
134
|
+
| `repeatable` | `boolean` | `false` | When `true`, the rendered group shows Add / Remove buttons so users can create multiple instances. |
|
|
135
|
+
| `min` | `number` | `1` | Minimum number of instances (only relevant when `repeatable: true`). Remove is disabled when the instance count equals `min`. |
|
|
136
|
+
| `max` | `number` | _(none)_ | Maximum number of instances (only relevant when `repeatable: true`). Add is disabled when the instance count equals `max`. Leave empty for unlimited. |
|
|
137
|
+
| `addLabel` | `string` | `"Add another"` | Label text for the Add-instance button in formRender. |
|
|
138
|
+
| `fields` | `array` | `[]` | Child field configurations (serialised as a JSON array). Managed via the inline editor in formBuilder; pass directly in `formData` for formRender. |
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## v1 Non-goals
|
|
143
|
+
|
|
144
|
+
The following features are intentionally **out of scope** for v1 and will not be
|
|
145
|
+
implemented without a new major design:
|
|
146
|
+
|
|
147
|
+
- **Nested groups** — a Group field cannot contain another Group field. The child-field
|
|
148
|
+
picker explicitly excludes `group` from the list of available types.
|
|
149
|
+
- **Per-child grid / column layout** — all child fields stack vertically within a group
|
|
150
|
+
instance; custom grid positioning of individual children is not supported.
|
|
151
|
+
- **Alternative naming schemes** — child input names always use bracket notation
|
|
152
|
+
(`group[i][child]`). Dot-notation or flat naming are not configurable.
|
|
153
|
+
- **Group-level value binding** — the `value` and `placeholder` attributes are disabled
|
|
154
|
+
on the group control itself; values live in the individual child fields.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Development
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
npm install
|
|
162
|
+
npm run build # produces dist/group.mjs + dist/group.umd.js
|
|
163
|
+
npm test # unit + integration tests via Vitest
|
|
164
|
+
npm run lint # ESLint
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Demo pages in `demo/` load the UMD build directly; see `demo/MANUAL-VERIFICATION.md` for
|
|
168
|
+
the human browser-verification checklist.
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
MIT
|
package/dist/group.mjs
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
//#region src/group.css?inline
|
|
2
|
+
var e = ".fields-wrap{display:none}.group-editor{box-sizing:border-box;border:1px dashed #b6b6b6;border-radius:4px;width:100%;padding:8px}.group-children{margin:0;padding:0;list-style:none}.group-child{background:#f7f7f7;border-radius:3px;margin-bottom:4px;padding:4px}.group-child .gc-handle{cursor:move}.group-child .gc-label{flex:1}.group-item-preview{align-items:center;gap:6px;display:flex}.group-item-preview .gc-btn{appearance:none;color:#8d8d8d;cursor:pointer;background:0 0;border:0;border-radius:3px;flex:none;width:20px;height:20px;padding:0;line-height:20px;transition:color .15s,background-color .15s;font-size:.85em!important}.group-item-preview .gc-btn:hover{color:#333;background-color:#0000000f}.group-item-preview .gc-remove:hover{color:#c10000}.group-child-edit{flex-basis:100%;margin-top:6px}.prev-holder .group-child-edit .form-group>label:not([class=formbuilder-checkbox-label]){display:block}.prev-holder .group-child-edit .form-group{margin-bottom:15px!important;font-size:.85em!important}.prev-holder .group-child-edit .form-group label{display:block!important}.prev-holder .group-child-edit .form-group .form-control{height:28px;font-size:inherit}.prev-holder .group-child-edit input[type=text],.prev-holder .group-child-edit input[type=number],.prev-holder .group-child-edit textarea,.prev-holder .group-child-edit select{box-shadow:none}.fb-group{border:1px solid #ddd;border-radius:4px;padding:12px}.fb-group-instance{border-bottom:1px solid #eee;padding:8px 0;position:relative}.fb-group-instance:last-child{border-bottom:none}.fb-group-remove{position:absolute;top:8px;right:0}";
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region src/injectStyles.js
|
|
5
|
+
function t(e) {
|
|
6
|
+
if (typeof document > "u" || !e || document.head.querySelector("style[data-formbuilder-group]")) return;
|
|
7
|
+
let t = document.createElement("style");
|
|
8
|
+
t.setAttribute("data-formbuilder-group", ""), t.textContent = e, document.head.appendChild(t);
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/lib/normalizeFields.js
|
|
12
|
+
function n(e) {
|
|
13
|
+
if (Array.isArray(e)) return e;
|
|
14
|
+
if (typeof e == "string" && e.trim() !== "") try {
|
|
15
|
+
let t = JSON.parse(e);
|
|
16
|
+
return Array.isArray(t) ? t : [];
|
|
17
|
+
} catch {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/render/buildRender.js
|
|
24
|
+
function r(e) {
|
|
25
|
+
let t = e.label, n = e.markup("div", null, { className: "group-instances" }), r = [];
|
|
26
|
+
t && r.push(e.markup("legend", e.parsedHtml(t))), r.push(n);
|
|
27
|
+
let i = e.markup("fieldset", r, { className: "fb-group" });
|
|
28
|
+
return e._groupHost = n, {
|
|
29
|
+
field: i,
|
|
30
|
+
layout: "noLabel"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/render/renderInstance.js
|
|
35
|
+
function i(e, t, n) {
|
|
36
|
+
n(e).formRender({
|
|
37
|
+
formData: t,
|
|
38
|
+
container: e
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/lib/indexChildConfigs.js
|
|
43
|
+
function a(e, t, n) {
|
|
44
|
+
return (e || []).map((e) => {
|
|
45
|
+
let r = structuredClone(e);
|
|
46
|
+
if (delete r.id, typeof r.name == "string" && r.name !== "") {
|
|
47
|
+
let e = r.name.endsWith("[]");
|
|
48
|
+
r.name = `${t}[${n}][${e ? r.name.slice(0, -2) : r.name}]${e ? "[]" : ""}`;
|
|
49
|
+
}
|
|
50
|
+
return r;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/render/repeatable.js
|
|
55
|
+
function o(e, t, n) {
|
|
56
|
+
let { fields: r, groupName: o, addLabel: s } = t, c = Number.isFinite(+t.min) && +t.min > 0 ? +t.min : 1, l = t.max === "" || t.max == null ? null : +t.max;
|
|
57
|
+
l !== null && l < c && (l = c);
|
|
58
|
+
let u = 0, d = 0, f = document.createElement("div");
|
|
59
|
+
f.className = "fb-group-controls";
|
|
60
|
+
let p = document.createElement("button");
|
|
61
|
+
p.type = "button", p.className = "fb-group-add", p.textContent = s || "Add another", f.appendChild(p);
|
|
62
|
+
function m() {
|
|
63
|
+
p.disabled = l != null && d >= l, e.querySelectorAll(".fb-group-remove").forEach((e) => {
|
|
64
|
+
e.disabled = d <= c;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function h() {
|
|
68
|
+
if (l != null && d >= l) return;
|
|
69
|
+
let t = u++, s = document.createElement("div");
|
|
70
|
+
s.className = "fb-group-instance";
|
|
71
|
+
let c = document.createElement("div");
|
|
72
|
+
s.appendChild(c);
|
|
73
|
+
let f = document.createElement("button");
|
|
74
|
+
f.type = "button", f.className = "fb-group-remove", f.textContent = "×", f.addEventListener("click", () => g(s)), s.appendChild(f), e.appendChild(s), i(c, a(r, o, t), n), d++, m();
|
|
75
|
+
}
|
|
76
|
+
function g(e) {
|
|
77
|
+
d <= c || (e.remove(), d--, m());
|
|
78
|
+
}
|
|
79
|
+
p.addEventListener("click", h), (e.parentNode || e).appendChild(f);
|
|
80
|
+
for (let e = 0; e < c; e++) h();
|
|
81
|
+
return {
|
|
82
|
+
addInstance: h,
|
|
83
|
+
removeInstance: g,
|
|
84
|
+
count: () => d
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/editor/groupState.js
|
|
89
|
+
function s(e) {
|
|
90
|
+
let t = (e || []).map((e) => structuredClone(e));
|
|
91
|
+
return {
|
|
92
|
+
get: () => t.map((e) => structuredClone(e)),
|
|
93
|
+
set: (e) => {
|
|
94
|
+
t = e.map((e) => structuredClone(e));
|
|
95
|
+
},
|
|
96
|
+
add: (e) => {
|
|
97
|
+
t.push(structuredClone(e));
|
|
98
|
+
},
|
|
99
|
+
removeAt: (e) => {
|
|
100
|
+
t.splice(e, 1);
|
|
101
|
+
},
|
|
102
|
+
updateAt: (e, n) => {
|
|
103
|
+
t[e] = structuredClone(n);
|
|
104
|
+
},
|
|
105
|
+
moveTo: (e, n) => {
|
|
106
|
+
let [r] = t.splice(e, 1);
|
|
107
|
+
t.splice(n, 0, r);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/editor/buildEditor.js
|
|
113
|
+
function c(e) {
|
|
114
|
+
let t = e && e.closest(".form-field");
|
|
115
|
+
return t ? t.querySelector(".fld-fields") : null;
|
|
116
|
+
}
|
|
117
|
+
function l(e, t) {
|
|
118
|
+
e && (e.value = JSON.stringify(t.get()));
|
|
119
|
+
}
|
|
120
|
+
function u(e) {
|
|
121
|
+
let t = s(e.childFields), n = e.markup("ol", null, { className: "group-children" }), r = e.markup("div", null, { className: "group-add" }), i = e.markup("div", [n, r], { className: "group-editor" });
|
|
122
|
+
return e._groupEditor = {
|
|
123
|
+
state: t,
|
|
124
|
+
list: n,
|
|
125
|
+
pickerMount: r,
|
|
126
|
+
editor: i,
|
|
127
|
+
hidden: null
|
|
128
|
+
}, {
|
|
129
|
+
field: i,
|
|
130
|
+
layout: "noLabel"
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/editor/picker.js
|
|
135
|
+
function d(e, t) {
|
|
136
|
+
let n = document.createElement("div");
|
|
137
|
+
n.className = "group-picker";
|
|
138
|
+
let r = document.createElement("select");
|
|
139
|
+
e.filter((e) => e.type !== "group").forEach((e) => {
|
|
140
|
+
let t = document.createElement("option");
|
|
141
|
+
t.value = e.type, t.textContent = e.label || e.type, r.appendChild(t);
|
|
142
|
+
});
|
|
143
|
+
let i = document.createElement("button");
|
|
144
|
+
return i.type = "button", i.className = "group-add-btn", i.textContent = "Add field", i.addEventListener("click", () => {
|
|
145
|
+
r.value && t(r.value);
|
|
146
|
+
}), n.append(r, i), n;
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/editor/palette.js
|
|
150
|
+
function f(e) {
|
|
151
|
+
return e = String(e).replace(/[^\w\s\-\[\]]/gi, ""), e = e.replace(/\[([^\]]+)\]/g, "-$1"), e = e.replace(/([A-Z])/g, (e) => "-" + e.toLowerCase()), e.replace(/\s/g, "-").replace(/^-+/g, "");
|
|
152
|
+
}
|
|
153
|
+
function p(e) {
|
|
154
|
+
return JSON.parse(JSON.stringify(e));
|
|
155
|
+
}
|
|
156
|
+
var m = (() => {
|
|
157
|
+
let e, t = 0;
|
|
158
|
+
return (n) => {
|
|
159
|
+
let r = Date.now();
|
|
160
|
+
return r === e ? t++ : (t = 0, e = r), `${n.type || f(n.label)}-${r}-${t}`;
|
|
161
|
+
};
|
|
162
|
+
})(), h = [
|
|
163
|
+
"text",
|
|
164
|
+
"number",
|
|
165
|
+
"file",
|
|
166
|
+
"date",
|
|
167
|
+
"select",
|
|
168
|
+
"textarea",
|
|
169
|
+
"autocomplete"
|
|
170
|
+
];
|
|
171
|
+
function g(e) {
|
|
172
|
+
return e.name ||= m(e), h.includes(e.type) && (e.className = e.className || "form-control"), e;
|
|
173
|
+
}
|
|
174
|
+
function _(e, t) {
|
|
175
|
+
let n = e && e.controls;
|
|
176
|
+
if (!n) return [g({
|
|
177
|
+
type: t,
|
|
178
|
+
label: t
|
|
179
|
+
})];
|
|
180
|
+
let r = (n.opts && n.opts.inputSets || []).find((e) => (e.name || f(e.label)) === t);
|
|
181
|
+
if (r) {
|
|
182
|
+
let e = [];
|
|
183
|
+
return r.showHeader && e.push(g({
|
|
184
|
+
type: "header",
|
|
185
|
+
subtype: "h2",
|
|
186
|
+
id: r.name,
|
|
187
|
+
label: r.label
|
|
188
|
+
})), (r.fields || []).forEach((t) => e.push(g(p(t)))), e;
|
|
189
|
+
}
|
|
190
|
+
let i = n.custom && n.custom.lookup(t);
|
|
191
|
+
if (i) {
|
|
192
|
+
let e = p(i);
|
|
193
|
+
return e.label = n.custom.label(t), [g(e)];
|
|
194
|
+
}
|
|
195
|
+
let a = n.getClass && n.getClass(t);
|
|
196
|
+
return [g({
|
|
197
|
+
type: t,
|
|
198
|
+
label: a && typeof a.label == "function" ? a.label(t) : t
|
|
199
|
+
})];
|
|
200
|
+
}
|
|
201
|
+
function v(e, t) {
|
|
202
|
+
let n = e && e.controls;
|
|
203
|
+
return n && Array.isArray(n.controlList) ? n.controlList.filter((e) => e !== "group").map((e) => {
|
|
204
|
+
let t = n.allControls && n.allControls[e];
|
|
205
|
+
return {
|
|
206
|
+
type: e,
|
|
207
|
+
label: (t && t.textContent ? t.textContent.trim() : "") || e
|
|
208
|
+
};
|
|
209
|
+
}) : (t && t.getRegistered ? t.getRegistered() : []).filter((e) => e !== "group").map((e) => ({
|
|
210
|
+
type: e,
|
|
211
|
+
label: e
|
|
212
|
+
}));
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/editor/childRow.js
|
|
216
|
+
function y(e, t, n) {
|
|
217
|
+
let r = document.createElement("li");
|
|
218
|
+
r.className = "group-child", r.dataset.index = String(t);
|
|
219
|
+
let i = document.createElement("span");
|
|
220
|
+
i.className = "gc-handle", i.textContent = "⋮⋮";
|
|
221
|
+
let a = document.createElement("span");
|
|
222
|
+
a.className = "gc-label", a.textContent = `${e.type}: ${e.label || e.name || ""}`;
|
|
223
|
+
let o = (e, n, r, i) => {
|
|
224
|
+
let a = document.createElement("button");
|
|
225
|
+
return a.type = "button", a.className = `gc-btn ${e} ${n}`, a.title = r, a.setAttribute("aria-label", r), a.addEventListener("click", () => i(t)), a;
|
|
226
|
+
}, s = document.createElement("div");
|
|
227
|
+
return s.className = "group-item-preview", s.append(i, a, o("gc-edit", "formbuilder-icon-pencil", "Edit", n.onEdit), o("gc-duplicate", "formbuilder-icon-copy", "Duplicate", n.onDuplicate), o("gc-remove", "formbuilder-icon-cancel", "Remove", n.onRemove)), r.append(s), r;
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/editor/findInstance.js
|
|
231
|
+
function b(e) {
|
|
232
|
+
let t = e;
|
|
233
|
+
for (; t;) {
|
|
234
|
+
if (t.fbInstance) return t.fbInstance;
|
|
235
|
+
t = t.parentElement;
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/editor/childEditor.js
|
|
241
|
+
function x(e, t, n) {
|
|
242
|
+
let { state: r, hidden: i } = e._groupEditor, a = e._findInstance();
|
|
243
|
+
if (!a || typeof a.generateAdvFields != "function") return;
|
|
244
|
+
let o = n.querySelector(".group-child-edit");
|
|
245
|
+
if (o) {
|
|
246
|
+
o.remove();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
let s = r.get()[t], c = a.generateAdvFields(s), u = document.createElement("div");
|
|
250
|
+
u.className = "group-child-edit", u.appendChild(c), n.appendChild(u);
|
|
251
|
+
let d = () => {
|
|
252
|
+
let e = a.getAttrVals(c);
|
|
253
|
+
r.updateAt(t, {
|
|
254
|
+
type: s.type,
|
|
255
|
+
...e
|
|
256
|
+
}), l(i, r);
|
|
257
|
+
};
|
|
258
|
+
c.addEventListener("input", d), c.addEventListener("change", d);
|
|
259
|
+
}
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/group.control.js
|
|
262
|
+
window.fbControls || (window.fbControls = []);
|
|
263
|
+
function S(e, t) {
|
|
264
|
+
let a = e;
|
|
265
|
+
e.jsonAttrs && e.jsonAttrs.set("group", ["fields"]);
|
|
266
|
+
class s extends e {
|
|
267
|
+
static get definition() {
|
|
268
|
+
return {
|
|
269
|
+
icon: "▦",
|
|
270
|
+
i18n: { default: "Group" },
|
|
271
|
+
defaultAttrs: {
|
|
272
|
+
repeatable: {
|
|
273
|
+
label: "Repeatable",
|
|
274
|
+
type: "checkbox",
|
|
275
|
+
value: !1
|
|
276
|
+
},
|
|
277
|
+
min: {
|
|
278
|
+
label: "Min instances",
|
|
279
|
+
type: "number",
|
|
280
|
+
value: 1
|
|
281
|
+
},
|
|
282
|
+
max: {
|
|
283
|
+
label: "Max instances",
|
|
284
|
+
type: "number",
|
|
285
|
+
value: ""
|
|
286
|
+
},
|
|
287
|
+
addLabel: {
|
|
288
|
+
label: "Add button text",
|
|
289
|
+
type: "text",
|
|
290
|
+
value: "Add another"
|
|
291
|
+
},
|
|
292
|
+
fields: {
|
|
293
|
+
label: "",
|
|
294
|
+
type: "hidden",
|
|
295
|
+
value: "[]"
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
disabledAttrs: ["value", "placeholder"]
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
get childFields() {
|
|
302
|
+
return n(this.config.fields);
|
|
303
|
+
}
|
|
304
|
+
build() {
|
|
305
|
+
return this.preview ? this.buildEditor() : this.buildRender();
|
|
306
|
+
}
|
|
307
|
+
buildEditor() {
|
|
308
|
+
return u(this);
|
|
309
|
+
}
|
|
310
|
+
buildRender() {
|
|
311
|
+
return r(this);
|
|
312
|
+
}
|
|
313
|
+
onRender() {
|
|
314
|
+
if (this.preview) return this.onRenderEditor();
|
|
315
|
+
if (this._rendered) return;
|
|
316
|
+
this._rendered = !0;
|
|
317
|
+
let e = window.jQuery;
|
|
318
|
+
this.config.repeatable ? this.renderRepeatable(e) : i(this._groupHost, this.childFields, e);
|
|
319
|
+
}
|
|
320
|
+
renderRepeatable(e) {
|
|
321
|
+
o(this._groupHost, {
|
|
322
|
+
fields: this.childFields,
|
|
323
|
+
groupName: this.config.name,
|
|
324
|
+
min: this.config.min,
|
|
325
|
+
max: this.config.max,
|
|
326
|
+
addLabel: this.config.addLabel
|
|
327
|
+
}, e);
|
|
328
|
+
}
|
|
329
|
+
onRenderEditor() {
|
|
330
|
+
this._groupEditor.hidden = c(this.element);
|
|
331
|
+
let { state: e, list: t, pickerMount: n } = this._groupEditor, r = () => {
|
|
332
|
+
l(this._groupEditor.hidden, e), this.onRenderEditor();
|
|
333
|
+
};
|
|
334
|
+
t.replaceChildren(), e.get().forEach((n, i) => {
|
|
335
|
+
t.appendChild(y(n, i, {
|
|
336
|
+
onEdit: (e) => this.openChildEditor(e),
|
|
337
|
+
onDuplicate: (t) => {
|
|
338
|
+
e.add(e.get()[t]), r();
|
|
339
|
+
},
|
|
340
|
+
onRemove: (t) => {
|
|
341
|
+
e.removeAt(t), r();
|
|
342
|
+
}
|
|
343
|
+
}));
|
|
344
|
+
});
|
|
345
|
+
let i = window.jQuery;
|
|
346
|
+
i && i.fn.sortable && i(t).sortable({
|
|
347
|
+
handle: ".gc-handle",
|
|
348
|
+
items: "> li.group-child",
|
|
349
|
+
update: (n, a) => {
|
|
350
|
+
let o = Number(a.item.data("startIndex")), s = i(t).children().index(a.item);
|
|
351
|
+
!Number.isNaN(o) && o !== s && (e.moveTo(o, s), r());
|
|
352
|
+
},
|
|
353
|
+
start: (e, t) => t.item.data("startIndex", t.item.index())
|
|
354
|
+
});
|
|
355
|
+
let o = this._findInstance(), s = v(o, a);
|
|
356
|
+
n.replaceChildren(), n.appendChild(d(s, (t) => {
|
|
357
|
+
_(o, t).forEach((t) => e.add(t)), r();
|
|
358
|
+
}));
|
|
359
|
+
}
|
|
360
|
+
_findInstance() {
|
|
361
|
+
return b(this.element);
|
|
362
|
+
}
|
|
363
|
+
openChildEditor(e) {
|
|
364
|
+
let t = this._groupEditor.list.querySelector(`li.group-child[data-index="${e}"]`);
|
|
365
|
+
t && x(this, e, t);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return e.register("group", s), s;
|
|
369
|
+
}
|
|
370
|
+
window.fbControls.push(S);
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region package.json
|
|
373
|
+
var C = "0.1.0";
|
|
374
|
+
//#endregion
|
|
375
|
+
//#region src/index.js
|
|
376
|
+
t(e);
|
|
377
|
+
//#endregion
|
|
378
|
+
export { C as VERSION };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.formBuilderGroup={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`.fields-wrap{display:none}.group-editor{box-sizing:border-box;border:1px dashed #b6b6b6;border-radius:4px;width:100%;padding:8px}.group-children{margin:0;padding:0;list-style:none}.group-child{background:#f7f7f7;border-radius:3px;margin-bottom:4px;padding:4px}.group-child .gc-handle{cursor:move}.group-child .gc-label{flex:1}.group-item-preview{align-items:center;gap:6px;display:flex}.group-item-preview .gc-btn{appearance:none;color:#8d8d8d;cursor:pointer;background:0 0;border:0;border-radius:3px;flex:none;width:20px;height:20px;padding:0;line-height:20px;transition:color .15s,background-color .15s;font-size:.85em!important}.group-item-preview .gc-btn:hover{color:#333;background-color:#0000000f}.group-item-preview .gc-remove:hover{color:#c10000}.group-child-edit{flex-basis:100%;margin-top:6px}.prev-holder .group-child-edit .form-group>label:not([class=formbuilder-checkbox-label]){display:block}.prev-holder .group-child-edit .form-group{margin-bottom:15px!important;font-size:.85em!important}.prev-holder .group-child-edit .form-group label{display:block!important}.prev-holder .group-child-edit .form-group .form-control{height:28px;font-size:inherit}.prev-holder .group-child-edit input[type=text],.prev-holder .group-child-edit input[type=number],.prev-holder .group-child-edit textarea,.prev-holder .group-child-edit select{box-shadow:none}.fb-group{border:1px solid #ddd;border-radius:4px;padding:12px}.fb-group-instance{border-bottom:1px solid #eee;padding:8px 0;position:relative}.fb-group-instance:last-child{border-bottom:none}.fb-group-remove{position:absolute;top:8px;right:0}`;function n(e){if(typeof document>`u`||!e||document.head.querySelector(`style[data-formbuilder-group]`))return;let t=document.createElement(`style`);t.setAttribute(`data-formbuilder-group`,``),t.textContent=e,document.head.appendChild(t)}function r(e){if(Array.isArray(e))return e;if(typeof e==`string`&&e.trim()!==``)try{let t=JSON.parse(e);return Array.isArray(t)?t:[]}catch{return[]}return[]}function i(e){let t=e.label,n=e.markup(`div`,null,{className:`group-instances`}),r=[];t&&r.push(e.markup(`legend`,e.parsedHtml(t))),r.push(n);let i=e.markup(`fieldset`,r,{className:`fb-group`});return e._groupHost=n,{field:i,layout:`noLabel`}}function a(e,t,n){n(e).formRender({formData:t,container:e})}function o(e,t,n){return(e||[]).map(e=>{let r=structuredClone(e);if(delete r.id,typeof r.name==`string`&&r.name!==``){let e=r.name.endsWith(`[]`);r.name=`${t}[${n}][${e?r.name.slice(0,-2):r.name}]${e?`[]`:``}`}return r})}function s(e,t,n){let{fields:r,groupName:i,addLabel:s}=t,c=Number.isFinite(+t.min)&&+t.min>0?+t.min:1,l=t.max===``||t.max==null?null:+t.max;l!==null&&l<c&&(l=c);let u=0,d=0,f=document.createElement(`div`);f.className=`fb-group-controls`;let p=document.createElement(`button`);p.type=`button`,p.className=`fb-group-add`,p.textContent=s||`Add another`,f.appendChild(p);function m(){p.disabled=l!=null&&d>=l,e.querySelectorAll(`.fb-group-remove`).forEach(e=>{e.disabled=d<=c})}function h(){if(l!=null&&d>=l)return;let t=u++,s=document.createElement(`div`);s.className=`fb-group-instance`;let c=document.createElement(`div`);s.appendChild(c);let f=document.createElement(`button`);f.type=`button`,f.className=`fb-group-remove`,f.textContent=`×`,f.addEventListener(`click`,()=>g(s)),s.appendChild(f),e.appendChild(s),a(c,o(r,i,t),n),d++,m()}function g(e){d<=c||(e.remove(),d--,m())}p.addEventListener(`click`,h),(e.parentNode||e).appendChild(f);for(let e=0;e<c;e++)h();return{addInstance:h,removeInstance:g,count:()=>d}}function c(e){let t=(e||[]).map(e=>structuredClone(e));return{get:()=>t.map(e=>structuredClone(e)),set:e=>{t=e.map(e=>structuredClone(e))},add:e=>{t.push(structuredClone(e))},removeAt:e=>{t.splice(e,1)},updateAt:(e,n)=>{t[e]=structuredClone(n)},moveTo:(e,n)=>{let[r]=t.splice(e,1);t.splice(n,0,r)}}}function l(e){let t=e&&e.closest(`.form-field`);return t?t.querySelector(`.fld-fields`):null}function u(e,t){e&&(e.value=JSON.stringify(t.get()))}function d(e){let t=c(e.childFields),n=e.markup(`ol`,null,{className:`group-children`}),r=e.markup(`div`,null,{className:`group-add`}),i=e.markup(`div`,[n,r],{className:`group-editor`});return e._groupEditor={state:t,list:n,pickerMount:r,editor:i,hidden:null},{field:i,layout:`noLabel`}}function f(e,t){let n=document.createElement(`div`);n.className=`group-picker`;let r=document.createElement(`select`);e.filter(e=>e.type!==`group`).forEach(e=>{let t=document.createElement(`option`);t.value=e.type,t.textContent=e.label||e.type,r.appendChild(t)});let i=document.createElement(`button`);return i.type=`button`,i.className=`group-add-btn`,i.textContent=`Add field`,i.addEventListener(`click`,()=>{r.value&&t(r.value)}),n.append(r,i),n}function p(e){return e=String(e).replace(/[^\w\s\-\[\]]/gi,``),e=e.replace(/\[([^\]]+)\]/g,`-$1`),e=e.replace(/([A-Z])/g,e=>`-`+e.toLowerCase()),e.replace(/\s/g,`-`).replace(/^-+/g,``)}function m(e){return JSON.parse(JSON.stringify(e))}var h=(()=>{let e,t=0;return n=>{let r=Date.now();return r===e?t++:(t=0,e=r),`${n.type||p(n.label)}-${r}-${t}`}})(),g=[`text`,`number`,`file`,`date`,`select`,`textarea`,`autocomplete`];function _(e){return e.name||=h(e),g.includes(e.type)&&(e.className=e.className||`form-control`),e}function v(e,t){let n=e&&e.controls;if(!n)return[_({type:t,label:t})];let r=(n.opts&&n.opts.inputSets||[]).find(e=>(e.name||p(e.label))===t);if(r){let e=[];return r.showHeader&&e.push(_({type:`header`,subtype:`h2`,id:r.name,label:r.label})),(r.fields||[]).forEach(t=>e.push(_(m(t)))),e}let i=n.custom&&n.custom.lookup(t);if(i){let e=m(i);return e.label=n.custom.label(t),[_(e)]}let a=n.getClass&&n.getClass(t);return[_({type:t,label:a&&typeof a.label==`function`?a.label(t):t})]}function y(e,t){let n=e&&e.controls;return n&&Array.isArray(n.controlList)?n.controlList.filter(e=>e!==`group`).map(e=>{let t=n.allControls&&n.allControls[e];return{type:e,label:(t&&t.textContent?t.textContent.trim():``)||e}}):(t&&t.getRegistered?t.getRegistered():[]).filter(e=>e!==`group`).map(e=>({type:e,label:e}))}function b(e,t,n){let r=document.createElement(`li`);r.className=`group-child`,r.dataset.index=String(t);let i=document.createElement(`span`);i.className=`gc-handle`,i.textContent=`⋮⋮`;let a=document.createElement(`span`);a.className=`gc-label`,a.textContent=`${e.type}: ${e.label||e.name||``}`;let o=(e,n,r,i)=>{let a=document.createElement(`button`);return a.type=`button`,a.className=`gc-btn ${e} ${n}`,a.title=r,a.setAttribute(`aria-label`,r),a.addEventListener(`click`,()=>i(t)),a},s=document.createElement(`div`);return s.className=`group-item-preview`,s.append(i,a,o(`gc-edit`,`formbuilder-icon-pencil`,`Edit`,n.onEdit),o(`gc-duplicate`,`formbuilder-icon-copy`,`Duplicate`,n.onDuplicate),o(`gc-remove`,`formbuilder-icon-cancel`,`Remove`,n.onRemove)),r.append(s),r}function x(e){let t=e;for(;t;){if(t.fbInstance)return t.fbInstance;t=t.parentElement}return null}function S(e,t,n){let{state:r,hidden:i}=e._groupEditor,a=e._findInstance();if(!a||typeof a.generateAdvFields!=`function`)return;let o=n.querySelector(`.group-child-edit`);if(o){o.remove();return}let s=r.get()[t],c=a.generateAdvFields(s),l=document.createElement(`div`);l.className=`group-child-edit`,l.appendChild(c),n.appendChild(l);let d=()=>{let e=a.getAttrVals(c);r.updateAt(t,{type:s.type,...e}),u(i,r)};c.addEventListener(`input`,d),c.addEventListener(`change`,d)}window.fbControls||(window.fbControls=[]);function C(e,t){let n=e;e.jsonAttrs&&e.jsonAttrs.set(`group`,[`fields`]);class o extends e{static get definition(){return{icon:`▦`,i18n:{default:`Group`},defaultAttrs:{repeatable:{label:`Repeatable`,type:`checkbox`,value:!1},min:{label:`Min instances`,type:`number`,value:1},max:{label:`Max instances`,type:`number`,value:``},addLabel:{label:`Add button text`,type:`text`,value:`Add another`},fields:{label:``,type:`hidden`,value:`[]`}},disabledAttrs:[`value`,`placeholder`]}}get childFields(){return r(this.config.fields)}build(){return this.preview?this.buildEditor():this.buildRender()}buildEditor(){return d(this)}buildRender(){return i(this)}onRender(){if(this.preview)return this.onRenderEditor();if(this._rendered)return;this._rendered=!0;let e=window.jQuery;this.config.repeatable?this.renderRepeatable(e):a(this._groupHost,this.childFields,e)}renderRepeatable(e){s(this._groupHost,{fields:this.childFields,groupName:this.config.name,min:this.config.min,max:this.config.max,addLabel:this.config.addLabel},e)}onRenderEditor(){this._groupEditor.hidden=l(this.element);let{state:e,list:t,pickerMount:r}=this._groupEditor,i=()=>{u(this._groupEditor.hidden,e),this.onRenderEditor()};t.replaceChildren(),e.get().forEach((n,r)=>{t.appendChild(b(n,r,{onEdit:e=>this.openChildEditor(e),onDuplicate:t=>{e.add(e.get()[t]),i()},onRemove:t=>{e.removeAt(t),i()}}))});let a=window.jQuery;a&&a.fn.sortable&&a(t).sortable({handle:`.gc-handle`,items:`> li.group-child`,update:(n,r)=>{let o=Number(r.item.data(`startIndex`)),s=a(t).children().index(r.item);!Number.isNaN(o)&&o!==s&&(e.moveTo(o,s),i())},start:(e,t)=>t.item.data(`startIndex`,t.item.index())});let o=this._findInstance(),s=y(o,n);r.replaceChildren(),r.appendChild(f(s,t=>{v(o,t).forEach(t=>e.add(t)),i()}))}_findInstance(){return x(this.element)}openChildEditor(e){let t=this._groupEditor.list.querySelector(`li.group-child[data-index="${e}"]`);t&&S(this,e,t)}}return e.register(`group`,o),o}window.fbControls.push(C),n(t),e.VERSION=`0.1.0`});
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "formbuilder-plugin-group",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Group control plugin for formBuilder — bundle registered inputs into a draggable, repeatable set.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/group.umd.js",
|
|
7
|
+
"module": "dist/group.mjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/group.mjs",
|
|
11
|
+
"require": "./dist/group.umd.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "vite build",
|
|
19
|
+
"build:pages": "npm run build && bash scripts/build-pages.sh",
|
|
20
|
+
"test": "vitest run",
|
|
21
|
+
"test:watch": "vitest",
|
|
22
|
+
"lint": "eslint src tests",
|
|
23
|
+
"bundle": "bash scripts/bundle.sh",
|
|
24
|
+
"release": "semantic-release",
|
|
25
|
+
"prepare": "node scripts/install-hooks.js"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"formBuilder": ">=3.23.0",
|
|
29
|
+
"jquery": ">=3.4.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@commitlint/cli": "21.0.2",
|
|
33
|
+
"@commitlint/config-conventional": "21.0.2",
|
|
34
|
+
"@semantic-release/changelog": "6.0.3",
|
|
35
|
+
"@semantic-release/exec": "7.1.0",
|
|
36
|
+
"@semantic-release/git": "10.0.1",
|
|
37
|
+
"eslint": "^9.0.0",
|
|
38
|
+
"formBuilder": "git+https://github.com/kevinchappell/formBuilder.git#feat/edit-panel-hook",
|
|
39
|
+
"jquery": "^3.7.1",
|
|
40
|
+
"jsdom": "^24.0.0",
|
|
41
|
+
"lefthook": "2.1.9",
|
|
42
|
+
"semantic-release": "25.0.5",
|
|
43
|
+
"vite": "8.0.16",
|
|
44
|
+
"vitest": "4.1.9"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT"
|
|
47
|
+
}
|