@orderingstack/front-components-product-configurator 1.4.2-beta.6 → 1.4.2-beta.9

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/dist/index.es.js CHANGED
@@ -6861,7 +6861,7 @@ const validateProductFilter = (product, stateFilter) => {
6861
6861
  const commonFilters = (_a = Object.keys(stateFiltersWithFltCtx)) == null ? void 0 : _a.filter(
6862
6862
  (filterName) => {
6863
6863
  var _a2;
6864
- const matchingFilters = (_a2 = Object.keys(productFilters == null ? void 0 : productFilters[filterName])) == null ? void 0 : _a2.filter((x2) => {
6864
+ const matchingFilters = (_a2 = Object.keys((productFilters == null ? void 0 : productFilters[filterName]) || {})) == null ? void 0 : _a2.filter((x2) => {
6865
6865
  var _a3;
6866
6866
  if (((_a3 = productFilters == null ? void 0 : productFilters[filterName]) == null ? void 0 : _a3[x2]) === (stateFiltersWithFltCtx == null ? void 0 : stateFiltersWithFltCtx[filterName])) {
6867
6867
  return true;
@@ -6917,8 +6917,8 @@ function useMenuOptions(product, nestedLvl, parentKind, parentSelCtx) {
6917
6917
  const initialSelected = useStore((state) => state.initialSelected);
6918
6918
  const currency = useStore((state) => state.currency);
6919
6919
  const { setValue, getValues } = useFormContext();
6920
- const { selected = {} } = getValues();
6921
- const isFilterValidated = validateProductFilter(product, stateFilter);
6920
+ const { selected = {}, filter = {} } = getValues();
6921
+ const isFilterValidated = validateProductFilter(product, filter);
6922
6922
  const isOnSubmitted = useStore((state) => state.isOnSubmitted);
6923
6923
  const setValidationMap = useStore((state) => state.setValidationMap);
6924
6924
  const [selCnt, qtyCnt] = getSelCtxCntFromSelectedState(
@@ -8804,15 +8804,63 @@ function Counter(props) {
8804
8804
  })]
8805
8805
  });
8806
8806
  }
8807
- function useEmitFilter(product) {
8808
- const { watch, setValue } = useFormContext();
8809
- const setFilter = useStore((state) => state.setFilter);
8807
+ function useEmitFilter(product, isRadio, nestedLvl) {
8808
+ const { watch, setValue, getValues, unregister } = useFormContext();
8810
8809
  const { id, selCtx, emit = {}, fltCtx } = product;
8811
8810
  const value = watch(`selected.${selCtx}.${id}`);
8812
8811
  useEffect(() => {
8812
+ var _a, _b, _c;
8813
+ if (!nestedLvl || nestedLvl > 1) {
8814
+ return;
8815
+ }
8816
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8817
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8818
+ const fltCtxPath = `filter.${fltCtx}`;
8819
+ if (isRadio && value && fltCtx) {
8820
+ if (isEmpty(emit)) {
8821
+ const groupValues = getValues(groupCntPath);
8822
+ (_a = Object.keys(groupValues || {})) == null ? void 0 : _a.forEach((x2) => {
8823
+ unregister(`${groupCntPath}.${x2}`);
8824
+ const groupsKeys = Object.keys(getValues(groupsPath) || {});
8825
+ if (isEmpty(groupsKeys)) {
8826
+ unregister(`${fltCtxPath}.${x2}`);
8827
+ } else {
8828
+ groupsKeys.forEach((groupSelCtx) => {
8829
+ if (groupSelCtx !== selCtx && !getValues(`${groupsPath}.${groupSelCtx}.${x2}`)) {
8830
+ unregister(`${fltCtxPath}.${x2}`);
8831
+ }
8832
+ });
8833
+ }
8834
+ });
8835
+ return;
8836
+ } else {
8837
+ const productGroupCntKeys = getValues(groupCntPath);
8838
+ const groupsCntKeys = getValues(groupsPath);
8839
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((x2) => {
8840
+ var _a2;
8841
+ (_a2 = Object.keys(productGroupCntKeys || {})) == null ? void 0 : _a2.forEach((productGroupCntKey) => {
8842
+ if (!emit[productGroupCntKey]) {
8843
+ unregister(`${fltCtxPath}.${productGroupCntKey}`);
8844
+ unregister(`${groupCntPath}.${productGroupCntKey}`);
8845
+ }
8846
+ });
8847
+ Object.keys(groupsCntKeys || {}).forEach((groupSelCtx) => {
8848
+ if (groupSelCtx !== selCtx && x2 in getValues(groupsPath == null ? void 0 : groupsPath[groupSelCtx])) {
8849
+ unregister(`${groupsPath}.${groupSelCtx}.${x2}`);
8850
+ }
8851
+ });
8852
+ setValue(`${fltCtxPath}.${x2}`, emit[x2]);
8853
+ setValue(`${groupCntPath}.${x2}`, { cnt: 1, value: emit[x2] });
8854
+ });
8855
+ return;
8856
+ }
8857
+ }
8813
8858
  if (value && fltCtx && !isEmpty(emit)) {
8814
- setFilter(fltCtx, emit);
8815
- setValue(`filter.${fltCtx}`, emit);
8859
+ (_c = Object.keys(emit)) == null ? void 0 : _c.forEach((x2) => {
8860
+ setValue(`filter.${fltCtx}.${x2}`, emit[x2]);
8861
+ const groupKeyCnt = getValues(`${groupCntPath}.${x2}.cnt`) || 0;
8862
+ setValue(`${groupCntPath}.${x2}`, { cnt: groupKeyCnt + 1, value: emit[x2] });
8863
+ });
8816
8864
  }
8817
8865
  }, [value]);
8818
8866
  return {};
@@ -8844,62 +8892,105 @@ const removeProductFromState = (selCtx, getValues, setValue) => {
8844
8892
  }, {})
8845
8893
  );
8846
8894
  };
8847
- const selectUnselectProduct = (setValue, getValues, items, isChecked, state) => {
8895
+ const selectUnselectProduct = (setValue, getValues, product, isChecked, unregister, nestedLvl) => {
8896
+ var _a, _b;
8897
+ const { items, state, emit, fltCtx, selCtx } = product;
8848
8898
  const { selected = {} } = state || {};
8899
+ console.log("selected/unselected product", product, isChecked);
8849
8900
  if (isChecked) {
8850
8901
  addProductToState(selected, setValue);
8851
8902
  const checkSubProducts = (items2) => {
8852
- var _a;
8903
+ var _a2;
8853
8904
  if (!Array.isArray(items2) || isEmpty(state == null ? void 0 : state.selected))
8854
8905
  return;
8855
- (_a = items2 == null ? void 0 : items2.filter((x2) => {
8856
- var _a2, _b;
8857
- return !!((_b = (_a2 = state.selected) == null ? void 0 : _a2[`${x2.selCtx}`]) == null ? void 0 : _b[`${x2.id}`]);
8858
- })) == null ? void 0 : _a.map((product) => {
8859
- if (product.state && "selected" in product.state) {
8906
+ (_a2 = items2 == null ? void 0 : items2.filter((x2) => {
8907
+ var _a3, _b2;
8908
+ return !!((_b2 = (_a3 = state.selected) == null ? void 0 : _a3[`${x2.selCtx}`]) == null ? void 0 : _b2[`${x2.id}`]);
8909
+ })) == null ? void 0 : _a2.map((product2) => {
8910
+ if (product2.state && "selected" in product2.state) {
8860
8911
  const {
8861
8912
  state: { selected: selected2 }
8862
- } = product;
8913
+ } = product2;
8863
8914
  addProductToState(selected2, setValue);
8864
8915
  }
8865
- if (Array.isArray(product.items)) {
8866
- checkSubProducts(product.items);
8916
+ if (Array.isArray(product2.items)) {
8917
+ checkSubProducts(product2.items);
8867
8918
  }
8868
8919
  });
8869
8920
  };
8870
8921
  checkSubProducts(items);
8871
8922
  } else {
8872
- Object.keys(selected).forEach((selCtx) => {
8873
- removeProductFromState(selCtx, getValues, setValue);
8923
+ Object.keys(selected).forEach((selCtx2) => {
8924
+ removeProductFromState(selCtx2, getValues, setValue);
8874
8925
  });
8875
8926
  const checkSubProducts = (items2) => {
8876
8927
  if (!Array.isArray(items2))
8877
8928
  return;
8878
- items2.map((product) => {
8879
- const { selCtx } = product;
8880
- const changedSelCtx = getValues(`selected.${selCtx}`);
8929
+ items2.map((product2) => {
8930
+ const { selCtx: selCtx2 } = product2;
8931
+ const changedSelCtx = getValues(`selected.${selCtx2}`);
8881
8932
  if (changedSelCtx) {
8882
- removeProductFromState(selCtx, getValues, setValue);
8933
+ removeProductFromState(selCtx2, getValues, setValue);
8883
8934
  }
8884
- checkSubProducts(product.items);
8935
+ checkSubProducts(product2.items);
8885
8936
  });
8886
8937
  };
8887
8938
  checkSubProducts(items);
8939
+ (_a = Object.keys(emit)) == null ? void 0 : _a.forEach((x2) => {
8940
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
8941
+ const cnt = getValues(formPath) || 0;
8942
+ if (cnt < 2) {
8943
+ unregister(formPath);
8944
+ } else {
8945
+ setValue(formPath, cnt - 1);
8946
+ }
8947
+ });
8948
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
8949
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8950
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8951
+ const fltCtxPath = `filter.${fltCtx}`;
8952
+ (_b = Object.keys(product.emit)) == null ? void 0 : _b.forEach((emitKey) => {
8953
+ var _a2, _b2, _c;
8954
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
8955
+ if (groupValue) {
8956
+ if (groupValue > 1) {
8957
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
8958
+ } else {
8959
+ unregister(`${groupCntPath}.${emitKey}`);
8960
+ const groupKeysObj = getValues(groupsPath) || {};
8961
+ const otherGroupsCntKeys = (_a2 = Object.keys(groupKeysObj)) == null ? void 0 : _a2.filter((x2) => x2 !== selCtx);
8962
+ if (!otherGroupsCntKeys) {
8963
+ unregister(`${fltCtxPath}.${emitKey}`);
8964
+ } else {
8965
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
8966
+ var _a3;
8967
+ return !!((_a3 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a3[`${emitKey}`]);
8968
+ });
8969
+ if (!sameKeyOtherGroup) {
8970
+ unregister(`${fltCtxPath}.${emitKey}`);
8971
+ } else {
8972
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
8973
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
8974
+ }
8975
+ }
8976
+ }
8977
+ }
8978
+ });
8979
+ }
8888
8980
  }
8889
8981
  };
8890
- function useMenuProduct(product, parentAttrs) {
8982
+ function useMenuProduct(product, parentAttrs, nestedLvl) {
8891
8983
  var _a;
8892
- const { id, selCtx } = product;
8984
+ const { id, selCtx, emit = {}, fltCtx } = product;
8893
8985
  const {
8894
8986
  allowMultiple: parentAllowMultiple,
8895
8987
  selMin: parentSelMin,
8896
8988
  selMax: parentSelMax
8897
8989
  } = parentAttrs;
8898
- useEmitFilter(product);
8899
8990
  const { isInputEnabled: isInputEnabled2, isInputCounterIncrementEnabled: isInputCounterIncrementEnabled2 } = useValidate();
8900
8991
  const refreshForm = useStore((state) => state.refreshForm);
8901
8992
  const currency = useStore((state) => state.currency);
8902
- const { watch, getValues, setValue, register } = useFormContext();
8993
+ const { watch, getValues, setValue, register, unregister } = useFormContext();
8903
8994
  const value = watch(`selected.${selCtx}.${id}`);
8904
8995
  const { selected = {} } = getValues();
8905
8996
  const valueNumber = Number(value || 0);
@@ -8907,7 +8998,8 @@ function useMenuProduct(product, parentAttrs) {
8907
8998
  const isCheckbox = !isRadio;
8908
8999
  const isCounter = isApiBooleanTrue(parentAllowMultiple);
8909
9000
  const isDecrementEnabled = valueNumber > (isRadio ? 1 : 0);
8910
- const isIncrementEnabled = isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
9001
+ const isIncrementEnabled = useEmitFilter(product, isRadio, nestedLvl);
9002
+ isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
8911
9003
  const isChecked = Number(((_a = selected == null ? void 0 : selected[selCtx]) == null ? void 0 : _a[id]) || 0) > 0;
8912
9004
  const isCheckboxEnabled = isInputEnabled2(product) || valueNumber;
8913
9005
  const registerId = `${isRadio ? "radio" : "selected"}.${selCtx}.${id}`;
@@ -8920,10 +9012,20 @@ function useMenuProduct(product, parentAttrs) {
8920
9012
  setValue(`selected.${selCtx}.${id}`, `${valueNumber + 1}`);
8921
9013
  };
8922
9014
  const handleCounterDecrementQty = () => {
9015
+ var _a2, _b;
8923
9016
  const decrementedValue = valueNumber - 1;
8924
9017
  setValue(`selected.${selCtx}.${id}`, `${decrementedValue}`);
8925
9018
  if (isCheckbox && decrementedValue === 0) {
8926
9019
  setValue(`selected.${selCtx}.${id}`, false);
9020
+ (_a2 = Object.keys(emit)) == null ? void 0 : _a2.forEach((x2) => {
9021
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
9022
+ const cnt = getValues(formPath) || 0;
9023
+ if (cnt < 2) {
9024
+ unregister(formPath);
9025
+ } else {
9026
+ setValue(formPath, cnt - 1);
9027
+ }
9028
+ });
8927
9029
  const checkSubProducts = (items) => {
8928
9030
  if (!Array.isArray(items))
8929
9031
  return;
@@ -8937,13 +9039,45 @@ function useMenuProduct(product, parentAttrs) {
8937
9039
  });
8938
9040
  };
8939
9041
  checkSubProducts(product.items);
9042
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
9043
+ const groupsPath = `filterWithCnt.${fltCtx}`;
9044
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
9045
+ const fltCtxPath = `filter.${fltCtx}`;
9046
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((emitKey) => {
9047
+ var _a3, _b2, _c;
9048
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
9049
+ if (groupValue) {
9050
+ if (groupValue > 1) {
9051
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
9052
+ } else {
9053
+ unregister(`${groupCntPath}.${emitKey}`);
9054
+ const groupKeysObj = getValues(groupsPath) || {};
9055
+ const otherGroupsCntKeys = (_a3 = Object.keys(groupKeysObj)) == null ? void 0 : _a3.filter((x2) => x2 !== selCtx);
9056
+ if (!otherGroupsCntKeys) {
9057
+ unregister(`${fltCtxPath}.${emitKey}`);
9058
+ } else {
9059
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
9060
+ var _a4;
9061
+ return !!((_a4 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a4[`${emitKey}`]);
9062
+ });
9063
+ if (!sameKeyOtherGroup) {
9064
+ unregister(`${fltCtxPath}.${emitKey}`);
9065
+ } else {
9066
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
9067
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
9068
+ }
9069
+ }
9070
+ }
9071
+ }
9072
+ });
9073
+ }
8940
9074
  }
8941
9075
  };
8942
9076
  const handleOnInputChange = (e2) => {
8943
9077
  const {
8944
9078
  target: { checked, name: selCtx2, defaultValue: productId }
8945
9079
  } = e2;
8946
- selectUnselectProduct(setValue, getValues, product.items, checked, product.state);
9080
+ selectUnselectProduct(setValue, getValues, product, checked, unregister, nestedLvl);
8947
9081
  if (isRadio) {
8948
9082
  console.log("handleOnInputChange radio", selCtx2, productId);
8949
9083
  const selectedContextName = "selected." + selCtx2.replace("radio.", "").split(".")[0];
@@ -8992,7 +9126,7 @@ const MenuProduct = (props) => {
8992
9126
  handleCounterIncrementQty,
8993
9127
  handleCounterDecrementQty,
8994
9128
  currency
8995
- } = useMenuProduct(product, parentAttrs);
9129
+ } = useMenuProduct(product, parentAttrs, nestedLvl);
8996
9130
  const {
8997
9131
  MenuProductUI
8998
9132
  } = useComponents();
@@ -9229,11 +9363,13 @@ const DisplayState = () => {
9229
9363
  } = useFormContext();
9230
9364
  const selected = watch("selected");
9231
9365
  const filter = watch("filter");
9366
+ const filterWithCnt = watch("filterWithCnt");
9232
9367
  const validationMap = useStore((state) => state.validationMap);
9233
9368
  console.log({
9234
9369
  selected,
9235
9370
  validationMap,
9236
- filter
9371
+ filter,
9372
+ filterWithCnt
9237
9373
  });
9238
9374
  return /* @__PURE__ */ jsxs("div", {
9239
9375
  className: "flex flex-col items-center py-4",