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