@abgov/react-components 6.5.0-alpha.3 → 6.5.0-alpha.4
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/index.d.ts +1 -0
- package/index.js +331 -0
- package/index.js.map +1 -1
- package/index.mjs +333 -2
- package/index.mjs.map +1 -1
- package/lib/use-public-form-controller.d.ts +13 -0
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
8
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
9
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
10
|
+
var _PublicFormController_instances, updateObjectListState_fn, dispatchError_fn;
|
|
1
11
|
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useRef, useEffect, useLayoutEffect } from "react";
|
|
12
|
+
import { useRef, useEffect, useLayoutEffect, useState, useCallback } from "react";
|
|
3
13
|
import { G, a } from "./icon-CoYGOp1V.mjs";
|
|
4
14
|
function GoabAccordion({
|
|
5
15
|
open,
|
|
@@ -3767,6 +3777,326 @@ const GoabFilterChip = ({
|
|
|
3767
3777
|
}
|
|
3768
3778
|
);
|
|
3769
3779
|
};
|
|
3780
|
+
class PublicFormController {
|
|
3781
|
+
constructor(type) {
|
|
3782
|
+
__privateAdd(this, _PublicFormController_instances);
|
|
3783
|
+
__publicField(this, "state");
|
|
3784
|
+
__publicField(this, "_formData");
|
|
3785
|
+
__publicField(this, "_formRef");
|
|
3786
|
+
this.type = type;
|
|
3787
|
+
}
|
|
3788
|
+
// Obtain reference to the form element
|
|
3789
|
+
init(e) {
|
|
3790
|
+
if (this._formRef) {
|
|
3791
|
+
console.warn("init: form element has already been set");
|
|
3792
|
+
return;
|
|
3793
|
+
}
|
|
3794
|
+
this._formRef = e.detail.el;
|
|
3795
|
+
this.state = {
|
|
3796
|
+
uuid: crypto.randomUUID(),
|
|
3797
|
+
form: {},
|
|
3798
|
+
history: [],
|
|
3799
|
+
editting: "",
|
|
3800
|
+
status: "not-started"
|
|
3801
|
+
};
|
|
3802
|
+
}
|
|
3803
|
+
initList(e) {
|
|
3804
|
+
this._formRef = e.detail.el;
|
|
3805
|
+
this.state = [];
|
|
3806
|
+
}
|
|
3807
|
+
// Public method to allow for the initialization of the state
|
|
3808
|
+
initState(state, callback) {
|
|
3809
|
+
relay(this._formRef, "external::init:state", state);
|
|
3810
|
+
if (typeof state === "string") {
|
|
3811
|
+
this.state = JSON.parse(state);
|
|
3812
|
+
} else if (!Array.isArray(state)) {
|
|
3813
|
+
this.state = state;
|
|
3814
|
+
}
|
|
3815
|
+
if (callback) {
|
|
3816
|
+
setTimeout(callback, 200);
|
|
3817
|
+
}
|
|
3818
|
+
}
|
|
3819
|
+
updateListState(e) {
|
|
3820
|
+
const detail = e.detail;
|
|
3821
|
+
if (!Array.isArray(detail.data)) {
|
|
3822
|
+
return;
|
|
3823
|
+
}
|
|
3824
|
+
this.state = detail.data;
|
|
3825
|
+
}
|
|
3826
|
+
updateObjectState(e) {
|
|
3827
|
+
var _a, _b;
|
|
3828
|
+
if (Array.isArray(this.state)) {
|
|
3829
|
+
return;
|
|
3830
|
+
}
|
|
3831
|
+
const detail = e.detail;
|
|
3832
|
+
if (detail.type === "list") {
|
|
3833
|
+
this.state = {
|
|
3834
|
+
...this.state,
|
|
3835
|
+
form: { ...((_a = this.state) == null ? void 0 : _a.form) || {}, [detail.id]: detail.data }
|
|
3836
|
+
};
|
|
3837
|
+
} else {
|
|
3838
|
+
this.state = {
|
|
3839
|
+
...this.state,
|
|
3840
|
+
...detail.data,
|
|
3841
|
+
form: { ...((_b = this.state) == null ? void 0 : _b.form) || {}, ...detail.data.form },
|
|
3842
|
+
history: detail.data.history
|
|
3843
|
+
};
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3846
|
+
getStateList() {
|
|
3847
|
+
if (!this.state) {
|
|
3848
|
+
return [];
|
|
3849
|
+
}
|
|
3850
|
+
if (!Array.isArray(this.state)) {
|
|
3851
|
+
console.warn(
|
|
3852
|
+
"Utils:getStateList: unable to update the state of a non-multi form type",
|
|
3853
|
+
this.state
|
|
3854
|
+
);
|
|
3855
|
+
return [];
|
|
3856
|
+
}
|
|
3857
|
+
if (this.state.length === 0) {
|
|
3858
|
+
return [];
|
|
3859
|
+
}
|
|
3860
|
+
return this.state.map((s) => {
|
|
3861
|
+
return Object.values(s.form).filter((item) => {
|
|
3862
|
+
var _a;
|
|
3863
|
+
return ((_a = item == null ? void 0 : item.data) == null ? void 0 : _a.type) === "details";
|
|
3864
|
+
}).map((item) => {
|
|
3865
|
+
var _a;
|
|
3866
|
+
return item.data.type === "details" && ((_a = item.data) == null ? void 0 : _a.fieldsets) || {};
|
|
3867
|
+
}).reduce(
|
|
3868
|
+
(acc, item) => {
|
|
3869
|
+
for (const [key, value] of Object.entries(item)) {
|
|
3870
|
+
acc[key] = value.value;
|
|
3871
|
+
}
|
|
3872
|
+
return acc;
|
|
3873
|
+
},
|
|
3874
|
+
{}
|
|
3875
|
+
);
|
|
3876
|
+
});
|
|
3877
|
+
}
|
|
3878
|
+
// getStateItems(group: string): Record<string, FieldsetItemState>[] {
|
|
3879
|
+
// if (Array.isArray(this.state)) {
|
|
3880
|
+
// console.error(
|
|
3881
|
+
// "Utils:getStateItems: unable to update the state of a multi form type",
|
|
3882
|
+
// );
|
|
3883
|
+
// return [];
|
|
3884
|
+
// }
|
|
3885
|
+
// if (!this.state) {
|
|
3886
|
+
// console.error("Utils:getStateItems: state has not yet been set");
|
|
3887
|
+
// return [];
|
|
3888
|
+
// }
|
|
3889
|
+
//
|
|
3890
|
+
// const data = this.state.form[group].data;
|
|
3891
|
+
// if (data.type !== "list") {
|
|
3892
|
+
// return [];
|
|
3893
|
+
// }
|
|
3894
|
+
//
|
|
3895
|
+
// return data.items.;
|
|
3896
|
+
// }
|
|
3897
|
+
// Public method to allow for the retrieval of the state value
|
|
3898
|
+
getStateValue(group, key) {
|
|
3899
|
+
if (Array.isArray(this.state)) {
|
|
3900
|
+
console.error("getStateValue: unable to update the state of a multi form type");
|
|
3901
|
+
return "";
|
|
3902
|
+
}
|
|
3903
|
+
if (!this.state) {
|
|
3904
|
+
console.error("getStateValue: state has not yet been set");
|
|
3905
|
+
return "";
|
|
3906
|
+
}
|
|
3907
|
+
const data = this.state.form[group].data;
|
|
3908
|
+
if (data.type !== "details") {
|
|
3909
|
+
return "";
|
|
3910
|
+
}
|
|
3911
|
+
return data.fieldsets[key].value;
|
|
3912
|
+
}
|
|
3913
|
+
// Public method to allow for the continuing to the next page
|
|
3914
|
+
continueTo(next) {
|
|
3915
|
+
if (!next) {
|
|
3916
|
+
console.error("continueTo [name] is undefined");
|
|
3917
|
+
return;
|
|
3918
|
+
}
|
|
3919
|
+
relay(this._formRef, "external::continue", { next });
|
|
3920
|
+
}
|
|
3921
|
+
// Public method to perform validation and send the appropriate messages to the form elements
|
|
3922
|
+
validate(e, field, validators, options) {
|
|
3923
|
+
var _a;
|
|
3924
|
+
const { el, state, cancelled } = e.detail;
|
|
3925
|
+
const value = (_a = state == null ? void 0 : state[field]) == null ? void 0 : _a.value;
|
|
3926
|
+
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
3927
|
+
if (cancelled) {
|
|
3928
|
+
return [true, value];
|
|
3929
|
+
}
|
|
3930
|
+
for (const validator of validators) {
|
|
3931
|
+
const msg = validator(value);
|
|
3932
|
+
__privateMethod(this, _PublicFormController_instances, dispatchError_fn).call(this, el, field, msg, options);
|
|
3933
|
+
if (msg) {
|
|
3934
|
+
return [false, ""];
|
|
3935
|
+
}
|
|
3936
|
+
}
|
|
3937
|
+
return [true, value];
|
|
3938
|
+
}
|
|
3939
|
+
/**
|
|
3940
|
+
* Validates a group of fields ensuring that at least `minPassCount` of the items within the group
|
|
3941
|
+
* passes. This is useful in the scenario when n number fields are required out of n+m number of fields.
|
|
3942
|
+
*
|
|
3943
|
+
* @param {string[]} fields - An array of field names to be validated.
|
|
3944
|
+
* @param {Event} e - The event object associated with the validation trigger.
|
|
3945
|
+
* @param {FieldValidator[]} validators - An array of validator functions to apply to the fields.
|
|
3946
|
+
* @return {[number, Record<string, boolean>]} - Returns back the number of fields that passed and a record of the fields and their pass status.
|
|
3947
|
+
*/
|
|
3948
|
+
validateGroup(e, fields, validators) {
|
|
3949
|
+
let passCount = 0;
|
|
3950
|
+
const validGroups = {};
|
|
3951
|
+
for (const field of fields) {
|
|
3952
|
+
const [_valid] = this.validate(e, field, validators, { grouped: true });
|
|
3953
|
+
if (_valid) {
|
|
3954
|
+
validGroups[field] = true;
|
|
3955
|
+
passCount++;
|
|
3956
|
+
}
|
|
3957
|
+
}
|
|
3958
|
+
return [passCount, validGroups];
|
|
3959
|
+
}
|
|
3960
|
+
edit(index) {
|
|
3961
|
+
relay(this._formRef, "external::alter:state", { index, operation: "edit" });
|
|
3962
|
+
}
|
|
3963
|
+
remove(index) {
|
|
3964
|
+
relay(this._formRef, "external::alter:state", {
|
|
3965
|
+
index,
|
|
3966
|
+
operation: "remove"
|
|
3967
|
+
});
|
|
3968
|
+
}
|
|
3969
|
+
// removes any data collected that doesn't correspond with the final history path
|
|
3970
|
+
clean(data) {
|
|
3971
|
+
return data.history.reduce((acc, fieldsetId) => {
|
|
3972
|
+
acc[fieldsetId] = data.form[fieldsetId];
|
|
3973
|
+
return acc;
|
|
3974
|
+
}, {});
|
|
3975
|
+
}
|
|
3976
|
+
}
|
|
3977
|
+
_PublicFormController_instances = new WeakSet();
|
|
3978
|
+
updateObjectListState_fn = function(detail) {
|
|
3979
|
+
var _a;
|
|
3980
|
+
if (!Array.isArray(detail.data)) {
|
|
3981
|
+
return;
|
|
3982
|
+
}
|
|
3983
|
+
if (Array.isArray(this.state)) {
|
|
3984
|
+
return;
|
|
3985
|
+
}
|
|
3986
|
+
this.state = {
|
|
3987
|
+
...this.state,
|
|
3988
|
+
form: {
|
|
3989
|
+
...((_a = this.state) == null ? void 0 : _a.form) || {},
|
|
3990
|
+
[detail.id]: detail.data
|
|
3991
|
+
}
|
|
3992
|
+
};
|
|
3993
|
+
};
|
|
3994
|
+
// Private method to dispatch the error message to the form element
|
|
3995
|
+
dispatchError_fn = function(el, name, msg, options) {
|
|
3996
|
+
el.dispatchEvent(
|
|
3997
|
+
new CustomEvent("msg", {
|
|
3998
|
+
composed: true,
|
|
3999
|
+
detail: {
|
|
4000
|
+
action: "external::set:error",
|
|
4001
|
+
data: {
|
|
4002
|
+
name,
|
|
4003
|
+
msg,
|
|
4004
|
+
grouped: options == null ? void 0 : options.grouped
|
|
4005
|
+
}
|
|
4006
|
+
}
|
|
4007
|
+
})
|
|
4008
|
+
);
|
|
4009
|
+
};
|
|
4010
|
+
function relay(el, eventName, data, opts) {
|
|
4011
|
+
if (!el) {
|
|
4012
|
+
console.error("dispatch element is null");
|
|
4013
|
+
return;
|
|
4014
|
+
}
|
|
4015
|
+
el.dispatchEvent(
|
|
4016
|
+
new CustomEvent("msg", {
|
|
4017
|
+
composed: true,
|
|
4018
|
+
bubbles: opts == null ? void 0 : opts.bubbles,
|
|
4019
|
+
detail: {
|
|
4020
|
+
action: eventName,
|
|
4021
|
+
data
|
|
4022
|
+
}
|
|
4023
|
+
})
|
|
4024
|
+
);
|
|
4025
|
+
}
|
|
4026
|
+
function usePublicFormController(type = "details") {
|
|
4027
|
+
const controllerRef = useRef(new PublicFormController(type));
|
|
4028
|
+
const [state, setState] = useState(void 0);
|
|
4029
|
+
useEffect(() => {
|
|
4030
|
+
var _a, _b;
|
|
4031
|
+
const originalStateGetter = (_a = Object.getOwnPropertyDescriptor(
|
|
4032
|
+
Object.getPrototypeOf(controllerRef.current),
|
|
4033
|
+
"state"
|
|
4034
|
+
)) == null ? void 0 : _a.get;
|
|
4035
|
+
const originalStateSetter = (_b = Object.getOwnPropertyDescriptor(
|
|
4036
|
+
Object.getPrototypeOf(controllerRef.current),
|
|
4037
|
+
"state"
|
|
4038
|
+
)) == null ? void 0 : _b.set;
|
|
4039
|
+
if (originalStateGetter && originalStateSetter) {
|
|
4040
|
+
Object.defineProperty(controllerRef.current, "state", {
|
|
4041
|
+
get: function() {
|
|
4042
|
+
return originalStateGetter.call(this);
|
|
4043
|
+
},
|
|
4044
|
+
set: function(value) {
|
|
4045
|
+
originalStateSetter.call(this, value);
|
|
4046
|
+
setState(value);
|
|
4047
|
+
},
|
|
4048
|
+
configurable: true
|
|
4049
|
+
});
|
|
4050
|
+
}
|
|
4051
|
+
}, []);
|
|
4052
|
+
const init = useCallback((e) => {
|
|
4053
|
+
var _a;
|
|
4054
|
+
if (!((_a = e.detail) == null ? void 0 : _a.el)) {
|
|
4055
|
+
console.error("El is null during initialization");
|
|
4056
|
+
return;
|
|
4057
|
+
}
|
|
4058
|
+
controllerRef.current.init(e);
|
|
4059
|
+
}, []);
|
|
4060
|
+
const initList = useCallback((e) => {
|
|
4061
|
+
var _a;
|
|
4062
|
+
const customEvent = e;
|
|
4063
|
+
if (!((_a = customEvent == null ? void 0 : customEvent.detail) == null ? void 0 : _a.el)) {
|
|
4064
|
+
console.error("El is null during list initialization");
|
|
4065
|
+
return;
|
|
4066
|
+
}
|
|
4067
|
+
controllerRef.current.initList(e);
|
|
4068
|
+
}, []);
|
|
4069
|
+
const initState = useCallback((state2, callback) => {
|
|
4070
|
+
if (!controllerRef.current._formRef) {
|
|
4071
|
+
console.error("Form ref not set.");
|
|
4072
|
+
return;
|
|
4073
|
+
}
|
|
4074
|
+
controllerRef.current.initState(state2, callback);
|
|
4075
|
+
}, []);
|
|
4076
|
+
const continueTo = useCallback((next) => {
|
|
4077
|
+
controllerRef.current.continueTo(next);
|
|
4078
|
+
}, []);
|
|
4079
|
+
const validate = useCallback((e, field, validators) => {
|
|
4080
|
+
return controllerRef.current.validate(e, field, validators);
|
|
4081
|
+
}, []);
|
|
4082
|
+
const getStateValue = useCallback((group, key) => {
|
|
4083
|
+
return controllerRef.current.getStateValue(group, key);
|
|
4084
|
+
}, []);
|
|
4085
|
+
const getStateList = useCallback(() => {
|
|
4086
|
+
return controllerRef.current.getStateList();
|
|
4087
|
+
}, []);
|
|
4088
|
+
return {
|
|
4089
|
+
state,
|
|
4090
|
+
init,
|
|
4091
|
+
initList,
|
|
4092
|
+
initState,
|
|
4093
|
+
continueTo,
|
|
4094
|
+
validate,
|
|
4095
|
+
getStateValue,
|
|
4096
|
+
getStateList,
|
|
4097
|
+
controller: controllerRef.current
|
|
4098
|
+
};
|
|
4099
|
+
}
|
|
3770
4100
|
export {
|
|
3771
4101
|
GoALinkButton,
|
|
3772
4102
|
GoabAccordion,
|
|
@@ -3851,6 +4181,7 @@ export {
|
|
|
3851
4181
|
GoabTextArea as GoabTextarea,
|
|
3852
4182
|
GoabThreeColumnLayout,
|
|
3853
4183
|
GoabTooltip,
|
|
3854
|
-
GoabTwoColumnLayout
|
|
4184
|
+
GoabTwoColumnLayout,
|
|
4185
|
+
usePublicFormController
|
|
3855
4186
|
};
|
|
3856
4187
|
//# sourceMappingURL=index.mjs.map
|