@abgov/react-components 6.5.0-alpha.2 → 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.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,
@@ -412,6 +422,8 @@ function GoabCheckbox({
412
422
  value,
413
423
  text,
414
424
  description,
425
+ reveal,
426
+ revealAriaLabel,
415
427
  maxWidth,
416
428
  children,
417
429
  onChange,
@@ -449,6 +461,7 @@ function GoabCheckbox({
449
461
  text,
450
462
  value: typeof value === "boolean" ? value ? "true" : void 0 : value,
451
463
  arialabel: ariaLabel,
464
+ revealarialabel: revealAriaLabel,
452
465
  description: typeof description === "string" ? description : void 0,
453
466
  maxwidth: maxWidth,
454
467
  mt,
@@ -456,8 +469,9 @@ function GoabCheckbox({
456
469
  mb,
457
470
  ml,
458
471
  children: [
459
- description && typeof description !== "string" && /* @__PURE__ */ jsx("div", { slot: "description", children: description }),
460
- children
472
+ children,
473
+ typeof description !== "string" && description && /* @__PURE__ */ jsx("div", { slot: "description", children: description }),
474
+ reveal && /* @__PURE__ */ jsx("div", { slot: "reveal", children: reveal })
461
475
  ]
462
476
  }
463
477
  );
@@ -573,6 +587,7 @@ function GoabDatePicker({
573
587
  max,
574
588
  testId,
575
589
  disabled,
590
+ type,
576
591
  mt,
577
592
  mr,
578
593
  mb,
@@ -588,11 +603,15 @@ function GoabDatePicker({
588
603
  const current = ref.current;
589
604
  const handleChange = (e) => {
590
605
  const detail = e.detail;
591
- onChange(detail);
606
+ onChange == null ? void 0 : onChange(detail);
592
607
  };
593
- current.addEventListener("_change", handleChange);
608
+ if (onChange) {
609
+ current.addEventListener("_change", handleChange);
610
+ }
594
611
  return () => {
595
- current.removeEventListener("_change", handleChange);
612
+ if (onChange) {
613
+ current.removeEventListener("_change", handleChange);
614
+ }
596
615
  };
597
616
  }, [onChange]);
598
617
  return /* @__PURE__ */ jsx(
@@ -601,6 +620,7 @@ function GoabDatePicker({
601
620
  ref,
602
621
  name,
603
622
  value: (value == null ? void 0 : value.toISOString()) || "",
623
+ type,
604
624
  error: error ? "true" : void 0,
605
625
  disabled: disabled ? "true" : void 0,
606
626
  min: min == null ? void 0 : min.toISOString(),
@@ -2847,6 +2867,7 @@ function GoabInput({
2847
2867
  trailingContent,
2848
2868
  maxLength,
2849
2869
  trailingIconAriaLabel,
2870
+ textAlign = "left",
2850
2871
  onTrailingIconClick,
2851
2872
  onChange,
2852
2873
  onFocus,
@@ -2924,6 +2945,7 @@ function GoabInput({
2924
2945
  ml,
2925
2946
  handletrailingiconclick: onTrailingIconClick ? "true" : "false",
2926
2947
  trailingiconarialabel: trailingIconAriaLabel,
2948
+ textalign: textAlign,
2927
2949
  children: [
2928
2950
  leadingContent && /* @__PURE__ */ jsx("div", { slot: "leadingContent", children: leadingContent }),
2929
2951
  trailingContent && /* @__PURE__ */ jsx("div", { slot: "trailingContent", children: trailingContent })
@@ -3058,6 +3080,7 @@ function GoabInputNumber({
3058
3080
  min = Number.MIN_VALUE,
3059
3081
  max = Number.MAX_VALUE,
3060
3082
  value,
3083
+ textAlign = "right",
3061
3084
  ...props
3062
3085
  }) {
3063
3086
  const onNumberChange = ({ name, value: value2 }) => {
@@ -3087,7 +3110,8 @@ function GoabInputNumber({
3087
3110
  onFocus,
3088
3111
  onBlur,
3089
3112
  type: "number",
3090
- onKeyPress
3113
+ onKeyPress,
3114
+ textAlign
3091
3115
  }
3092
3116
  );
3093
3117
  }
@@ -3339,6 +3363,8 @@ function GoabRadioItem({
3339
3363
  label,
3340
3364
  value,
3341
3365
  description,
3366
+ reveal,
3367
+ revealAriaLabel,
3342
3368
  maxWidth,
3343
3369
  disabled,
3344
3370
  checked,
@@ -3362,12 +3388,14 @@ function GoabRadioItem({
3362
3388
  disabled: disabled ? "true" : void 0,
3363
3389
  checked: checked ? "true" : void 0,
3364
3390
  arialabel: ariaLabel,
3391
+ revealarialabel: revealAriaLabel,
3365
3392
  mt,
3366
3393
  mr,
3367
3394
  mb,
3368
3395
  ml,
3369
3396
  children: [
3370
3397
  description && typeof description !== "string" && /* @__PURE__ */ jsx("div", { slot: "description", children: description }),
3398
+ reveal && /* @__PURE__ */ jsx("div", { slot: "reveal", children: reveal }),
3371
3399
  children
3372
3400
  ]
3373
3401
  }
@@ -3749,6 +3777,326 @@ const GoabFilterChip = ({
3749
3777
  }
3750
3778
  );
3751
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
+ }
3752
4100
  export {
3753
4101
  GoALinkButton,
3754
4102
  GoabAccordion,
@@ -3833,6 +4181,7 @@ export {
3833
4181
  GoabTextArea as GoabTextarea,
3834
4182
  GoabThreeColumnLayout,
3835
4183
  GoabTooltip,
3836
- GoabTwoColumnLayout
4184
+ GoabTwoColumnLayout,
4185
+ usePublicFormController
3837
4186
  };
3838
4187
  //# sourceMappingURL=index.mjs.map