@orderingstack/front-components-product-configurator 1.0.3 → 1.0.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/dist/index.es.js CHANGED
@@ -2651,7 +2651,7 @@ var I18n = function(_EventEmitter) {
2651
2651
  if (usedLng && usedLng.toLowerCase() === "cimode")
2652
2652
  return usedCallback();
2653
2653
  var toLoad = [];
2654
- var append = function append2(lng) {
2654
+ var append2 = function append3(lng) {
2655
2655
  if (!lng)
2656
2656
  return;
2657
2657
  var lngs = _this3.services.languageUtils.toResolveHierarchy(lng);
@@ -2663,14 +2663,14 @@ var I18n = function(_EventEmitter) {
2663
2663
  if (!usedLng) {
2664
2664
  var fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
2665
2665
  fallbacks.forEach(function(l2) {
2666
- return append(l2);
2666
+ return append2(l2);
2667
2667
  });
2668
2668
  } else {
2669
- append(usedLng);
2669
+ append2(usedLng);
2670
2670
  }
2671
2671
  if (this.options.preload) {
2672
2672
  this.options.preload.forEach(function(l2) {
2673
- return append(l2);
2673
+ return append2(l2);
2674
2674
  });
2675
2675
  }
2676
2676
  this.services.backendConnector.load(toLoad, this.options.ns, function(e2) {
@@ -3077,6 +3077,7 @@ var shouldRenderFormState = (formStateData, _proxyFormState, isRoot) => {
3077
3077
  return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find((key) => _proxyFormState[key] === (!isRoot || VALIDATION_MODE.all));
3078
3078
  };
3079
3079
  var convertToArrayPayload = (value) => Array.isArray(value) ? value : [value];
3080
+ var shouldSubscribeByName = (name, signalName, exact) => exact && signalName ? name === signalName : !name || !signalName || name === signalName || convertToArrayPayload(name).some((currentName) => currentName && (currentName.startsWith(signalName) || signalName.startsWith(currentName)));
3080
3081
  function useSubscribe(props) {
3081
3082
  const _props = React.useRef(props);
3082
3083
  _props.current = props;
@@ -3089,6 +3090,45 @@ function useSubscribe(props) {
3089
3090
  };
3090
3091
  }, [props.disabled]);
3091
3092
  }
3093
+ function useFormState(props) {
3094
+ const methods = useFormContext();
3095
+ const { control = methods.control, disabled, name, exact } = props || {};
3096
+ const [formState, updateFormState] = React.useState(control._formState);
3097
+ const _mounted = React.useRef(true);
3098
+ const _localProxyFormState = React.useRef({
3099
+ isDirty: false,
3100
+ isLoading: false,
3101
+ dirtyFields: false,
3102
+ touchedFields: false,
3103
+ isValidating: false,
3104
+ isValid: false,
3105
+ errors: false
3106
+ });
3107
+ const _name = React.useRef(name);
3108
+ _name.current = name;
3109
+ useSubscribe({
3110
+ disabled,
3111
+ next: (value) => _mounted.current && shouldSubscribeByName(_name.current, value.name, exact) && shouldRenderFormState(value, _localProxyFormState.current) && updateFormState({
3112
+ ...control._formState,
3113
+ ...value
3114
+ }),
3115
+ subject: control._subjects.state
3116
+ });
3117
+ React.useEffect(() => {
3118
+ _mounted.current = true;
3119
+ const isDirty = control._proxyFormState.isDirty && control._getDirty();
3120
+ if (isDirty !== control._formState.isDirty) {
3121
+ control._subjects.state.next({
3122
+ isDirty
3123
+ });
3124
+ }
3125
+ control._updateValid();
3126
+ return () => {
3127
+ _mounted.current = false;
3128
+ };
3129
+ }, [control]);
3130
+ return getProxyFormState(formState, control, _localProxyFormState.current, false);
3131
+ }
3092
3132
  var isString = (value) => typeof value === "string";
3093
3133
  var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
3094
3134
  if (isString(names)) {
@@ -3127,6 +3167,107 @@ function cloneObject(data) {
3127
3167
  }
3128
3168
  return copy2;
3129
3169
  }
3170
+ function useWatch(props) {
3171
+ const methods = useFormContext();
3172
+ const { control = methods.control, name, defaultValue, disabled, exact } = props || {};
3173
+ const _name = React.useRef(name);
3174
+ _name.current = name;
3175
+ useSubscribe({
3176
+ disabled,
3177
+ subject: control._subjects.watch,
3178
+ next: (formState) => {
3179
+ if (shouldSubscribeByName(_name.current, formState.name, exact)) {
3180
+ updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
3181
+ }
3182
+ }
3183
+ });
3184
+ const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
3185
+ React.useEffect(() => control._removeUnmounted());
3186
+ return value;
3187
+ }
3188
+ function useController(props) {
3189
+ const methods = useFormContext();
3190
+ const { name, control = methods.control, shouldUnregister } = props;
3191
+ const isArrayField = isNameInFieldArray(control._names.array, name);
3192
+ const value = useWatch({
3193
+ control,
3194
+ name,
3195
+ defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
3196
+ exact: true
3197
+ });
3198
+ const formState = useFormState({
3199
+ control,
3200
+ name
3201
+ });
3202
+ const _registerProps = React.useRef(control.register(name, {
3203
+ ...props.rules,
3204
+ value
3205
+ }));
3206
+ React.useEffect(() => {
3207
+ const updateMounted = (name2, value2) => {
3208
+ const field = get(control._fields, name2);
3209
+ if (field) {
3210
+ field._f.mount = value2;
3211
+ }
3212
+ };
3213
+ updateMounted(name, true);
3214
+ return () => {
3215
+ const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
3216
+ (isArrayField ? _shouldUnregisterField && !control._stateFlags.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
3217
+ };
3218
+ }, [name, control, isArrayField, shouldUnregister]);
3219
+ return {
3220
+ field: {
3221
+ name,
3222
+ value,
3223
+ onChange: React.useCallback((event) => _registerProps.current.onChange({
3224
+ target: {
3225
+ value: getEventValue(event),
3226
+ name
3227
+ },
3228
+ type: EVENTS.CHANGE
3229
+ }), [name]),
3230
+ onBlur: React.useCallback(() => _registerProps.current.onBlur({
3231
+ target: {
3232
+ value: get(control._formValues, name),
3233
+ name
3234
+ },
3235
+ type: EVENTS.BLUR
3236
+ }), [name, control]),
3237
+ ref: (elm) => {
3238
+ const field = get(control._fields, name);
3239
+ if (field && elm) {
3240
+ field._f.ref = {
3241
+ focus: () => elm.focus(),
3242
+ select: () => elm.select(),
3243
+ setCustomValidity: (message) => elm.setCustomValidity(message),
3244
+ reportValidity: () => elm.reportValidity()
3245
+ };
3246
+ }
3247
+ }
3248
+ },
3249
+ formState,
3250
+ fieldState: Object.defineProperties({}, {
3251
+ invalid: {
3252
+ enumerable: true,
3253
+ get: () => !!get(formState.errors, name)
3254
+ },
3255
+ isDirty: {
3256
+ enumerable: true,
3257
+ get: () => !!get(formState.dirtyFields, name)
3258
+ },
3259
+ isTouched: {
3260
+ enumerable: true,
3261
+ get: () => !!get(formState.touchedFields, name)
3262
+ },
3263
+ error: {
3264
+ enumerable: true,
3265
+ get: () => get(formState.errors, name)
3266
+ }
3267
+ })
3268
+ };
3269
+ }
3270
+ const Controller = (props) => props.render(useController(props));
3130
3271
  var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria ? {
3131
3272
  ...errors[name],
3132
3273
  types: {
@@ -3172,6 +3313,14 @@ const focusFieldBy = (fields, callback, fieldsNames) => {
3172
3313
  }
3173
3314
  }
3174
3315
  };
3316
+ var generateId = () => {
3317
+ const d2 = typeof performance === "undefined" ? Date.now() : performance.now() * 1e3;
3318
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
3319
+ const r2 = (Math.random() * 16 + d2) % 16 | 0;
3320
+ return (c == "x" ? r2 : r2 & 3 | 8).toString(16);
3321
+ });
3322
+ };
3323
+ var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || isUndefined$2(options.shouldFocus) ? options.focusName || `${name}.${isUndefined$2(options.focusIndex) ? index : options.focusIndex}.` : "";
3175
3324
  var getValidationModes = (mode) => ({
3176
3325
  isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
3177
3326
  isOnBlur: mode === VALIDATION_MODE.onBlur,
@@ -3384,6 +3533,43 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
3384
3533
  setCustomValidity(true);
3385
3534
  return error2;
3386
3535
  };
3536
+ function append(data, value) {
3537
+ return [...data, ...convertToArrayPayload(value)];
3538
+ }
3539
+ var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => void 0) : void 0;
3540
+ function insert(data, index, value) {
3541
+ return [
3542
+ ...data.slice(0, index),
3543
+ ...convertToArrayPayload(value),
3544
+ ...data.slice(index)
3545
+ ];
3546
+ }
3547
+ var moveArrayAt = (data, from, to) => {
3548
+ if (!Array.isArray(data)) {
3549
+ return [];
3550
+ }
3551
+ if (isUndefined$2(data[to])) {
3552
+ data[to] = void 0;
3553
+ }
3554
+ data.splice(to, 0, data.splice(from, 1)[0]);
3555
+ return data;
3556
+ };
3557
+ function prepend(data, value) {
3558
+ return [...convertToArrayPayload(value), ...convertToArrayPayload(data)];
3559
+ }
3560
+ function removeAtIndexes(data, indexes) {
3561
+ let i2 = 0;
3562
+ const temp = [...data];
3563
+ for (const index of indexes) {
3564
+ temp.splice(index - i2, 1);
3565
+ i2++;
3566
+ }
3567
+ return compact(temp).length ? temp : [];
3568
+ }
3569
+ var removeArrayAt = (data, index) => isUndefined$2(index) ? [] : removeAtIndexes(data, convertToArrayPayload(index).sort((a2, b) => a2 - b));
3570
+ var swapArrayAt = (data, indexA, indexB) => {
3571
+ data[indexA] = [data[indexB], data[indexB] = data[indexA]][0];
3572
+ };
3387
3573
  function baseGet(object, updatePath) {
3388
3574
  const length = updatePath.slice(0, -1).length;
3389
3575
  let index = 0;
@@ -3427,6 +3613,174 @@ function unset(object, path2) {
3427
3613
  }
3428
3614
  return object;
3429
3615
  }
3616
+ var updateAt = (fieldValues, index, value) => {
3617
+ fieldValues[index] = value;
3618
+ return fieldValues;
3619
+ };
3620
+ function useFieldArray(props) {
3621
+ const methods = useFormContext();
3622
+ const { control = methods.control, name, keyName = "id", shouldUnregister } = props;
3623
+ const [fields, setFields] = React.useState(control._getFieldArray(name));
3624
+ const ids = React.useRef(control._getFieldArray(name).map(generateId));
3625
+ const _fieldIds = React.useRef(fields);
3626
+ const _name = React.useRef(name);
3627
+ const _actioned = React.useRef(false);
3628
+ _name.current = name;
3629
+ _fieldIds.current = fields;
3630
+ control._names.array.add(name);
3631
+ props.rules && control.register(name, props.rules);
3632
+ useSubscribe({
3633
+ next: ({ values, name: fieldArrayName }) => {
3634
+ if (fieldArrayName === _name.current || !fieldArrayName) {
3635
+ const fieldValues = get(values, _name.current);
3636
+ if (Array.isArray(fieldValues)) {
3637
+ setFields(fieldValues);
3638
+ ids.current = fieldValues.map(generateId);
3639
+ }
3640
+ }
3641
+ },
3642
+ subject: control._subjects.array
3643
+ });
3644
+ const updateValues = React.useCallback((updatedFieldArrayValues) => {
3645
+ _actioned.current = true;
3646
+ control._updateFieldArray(name, updatedFieldArrayValues);
3647
+ }, [control, name]);
3648
+ const append$1 = (value, options) => {
3649
+ const appendValue = convertToArrayPayload(cloneObject(value));
3650
+ const updatedFieldArrayValues = append(control._getFieldArray(name), appendValue);
3651
+ control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
3652
+ ids.current = append(ids.current, appendValue.map(generateId));
3653
+ updateValues(updatedFieldArrayValues);
3654
+ setFields(updatedFieldArrayValues);
3655
+ control._updateFieldArray(name, updatedFieldArrayValues, append, {
3656
+ argA: fillEmptyArray(value)
3657
+ });
3658
+ };
3659
+ const prepend$1 = (value, options) => {
3660
+ const prependValue = convertToArrayPayload(cloneObject(value));
3661
+ const updatedFieldArrayValues = prepend(control._getFieldArray(name), prependValue);
3662
+ control._names.focus = getFocusFieldName(name, 0, options);
3663
+ ids.current = prepend(ids.current, prependValue.map(generateId));
3664
+ updateValues(updatedFieldArrayValues);
3665
+ setFields(updatedFieldArrayValues);
3666
+ control._updateFieldArray(name, updatedFieldArrayValues, prepend, {
3667
+ argA: fillEmptyArray(value)
3668
+ });
3669
+ };
3670
+ const remove2 = (index) => {
3671
+ const updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
3672
+ ids.current = removeArrayAt(ids.current, index);
3673
+ updateValues(updatedFieldArrayValues);
3674
+ setFields(updatedFieldArrayValues);
3675
+ control._updateFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
3676
+ argA: index
3677
+ });
3678
+ };
3679
+ const insert$1 = (index, value, options) => {
3680
+ const insertValue = convertToArrayPayload(cloneObject(value));
3681
+ const updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
3682
+ control._names.focus = getFocusFieldName(name, index, options);
3683
+ ids.current = insert(ids.current, index, insertValue.map(generateId));
3684
+ updateValues(updatedFieldArrayValues);
3685
+ setFields(updatedFieldArrayValues);
3686
+ control._updateFieldArray(name, updatedFieldArrayValues, insert, {
3687
+ argA: index,
3688
+ argB: fillEmptyArray(value)
3689
+ });
3690
+ };
3691
+ const swap = (indexA, indexB) => {
3692
+ const updatedFieldArrayValues = control._getFieldArray(name);
3693
+ swapArrayAt(updatedFieldArrayValues, indexA, indexB);
3694
+ swapArrayAt(ids.current, indexA, indexB);
3695
+ updateValues(updatedFieldArrayValues);
3696
+ setFields(updatedFieldArrayValues);
3697
+ control._updateFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
3698
+ argA: indexA,
3699
+ argB: indexB
3700
+ }, false);
3701
+ };
3702
+ const move = (from, to) => {
3703
+ const updatedFieldArrayValues = control._getFieldArray(name);
3704
+ moveArrayAt(updatedFieldArrayValues, from, to);
3705
+ moveArrayAt(ids.current, from, to);
3706
+ updateValues(updatedFieldArrayValues);
3707
+ setFields(updatedFieldArrayValues);
3708
+ control._updateFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
3709
+ argA: from,
3710
+ argB: to
3711
+ }, false);
3712
+ };
3713
+ const update = (index, value) => {
3714
+ const updateValue = cloneObject(value);
3715
+ const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
3716
+ ids.current = [...updatedFieldArrayValues].map((item, i2) => !item || i2 === index ? generateId() : ids.current[i2]);
3717
+ updateValues(updatedFieldArrayValues);
3718
+ setFields([...updatedFieldArrayValues]);
3719
+ control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
3720
+ argA: index,
3721
+ argB: updateValue
3722
+ }, true, false);
3723
+ };
3724
+ const replace = (value) => {
3725
+ const updatedFieldArrayValues = convertToArrayPayload(cloneObject(value));
3726
+ ids.current = updatedFieldArrayValues.map(generateId);
3727
+ updateValues([...updatedFieldArrayValues]);
3728
+ setFields([...updatedFieldArrayValues]);
3729
+ control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
3730
+ };
3731
+ React.useEffect(() => {
3732
+ control._stateFlags.action = false;
3733
+ isWatched(name, control._names) && control._subjects.state.next({});
3734
+ if (_actioned.current && (!getValidationModes(control._options.mode).isOnSubmit || control._formState.isSubmitted)) {
3735
+ if (control._options.resolver) {
3736
+ control._executeSchema([name]).then((result) => {
3737
+ const error2 = get(result.errors, name);
3738
+ const existingError = get(control._formState.errors, name);
3739
+ if (existingError ? !error2 && existingError.type : error2 && error2.type) {
3740
+ error2 ? set(control._formState.errors, name, error2) : unset(control._formState.errors, name);
3741
+ control._subjects.state.next({
3742
+ errors: control._formState.errors
3743
+ });
3744
+ }
3745
+ });
3746
+ } else {
3747
+ const field = get(control._fields, name);
3748
+ if (field && field._f) {
3749
+ validateField(field, get(control._formValues, name), control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error2) => !isEmptyObject(error2) && control._subjects.state.next({
3750
+ errors: updateFieldArrayRootError(control._formState.errors, error2, name)
3751
+ }));
3752
+ }
3753
+ }
3754
+ }
3755
+ control._subjects.watch.next({
3756
+ name,
3757
+ values: control._formValues
3758
+ });
3759
+ control._names.focus && focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ""));
3760
+ control._names.focus = "";
3761
+ control._proxyFormState.isValid && control._updateValid();
3762
+ }, [fields, name, control]);
3763
+ React.useEffect(() => {
3764
+ !get(control._formValues, name) && control._updateFieldArray(name);
3765
+ return () => {
3766
+ (control._options.shouldUnregister || shouldUnregister) && control.unregister(name);
3767
+ };
3768
+ }, [name, control, keyName, shouldUnregister]);
3769
+ return {
3770
+ swap: React.useCallback(swap, [updateValues, name, control]),
3771
+ move: React.useCallback(move, [updateValues, name, control]),
3772
+ prepend: React.useCallback(prepend$1, [updateValues, name, control]),
3773
+ append: React.useCallback(append$1, [updateValues, name, control]),
3774
+ remove: React.useCallback(remove2, [updateValues, name, control]),
3775
+ insert: React.useCallback(insert$1, [updateValues, name, control]),
3776
+ update: React.useCallback(update, [updateValues, name, control]),
3777
+ replace: React.useCallback(replace, [updateValues, name, control]),
3778
+ fields: React.useMemo(() => fields.map((field, index) => ({
3779
+ ...field,
3780
+ [keyName]: ids.current[index] || generateId()
3781
+ })), [fields, keyName])
3782
+ };
3783
+ }
3430
3784
  function createSubject() {
3431
3785
  let _observers = [];
3432
3786
  const next = (value) => {
@@ -4355,6 +4709,20 @@ function useForm(props = {}) {
4355
4709
  _formControl.current.formState = getProxyFormState(formState, control);
4356
4710
  return _formControl.current;
4357
4711
  }
4712
+ var index_esm = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4713
+ __proto__: null,
4714
+ Controller,
4715
+ FormProvider,
4716
+ appendErrors,
4717
+ get,
4718
+ set,
4719
+ useController,
4720
+ useFieldArray,
4721
+ useForm,
4722
+ useFormContext,
4723
+ useFormState,
4724
+ useWatch
4725
+ }, Symbol.toStringTag, { value: "Module" }));
4358
4726
  var MainView = /* @__PURE__ */ ((MainView2) => {
4359
4727
  MainView2["FULL_PAGE"] = "FULL_PAGE";
4360
4728
  MainView2["MODAL"] = "MODAL";
@@ -9696,5 +10064,5 @@ function ProductConfigurator(props) {
9696
10064
  })
9697
10065
  });
9698
10066
  }
9699
- export { ContextType, MainView, ProductConfigurator, ProductKinds, ProductTypes, SelMinSelMaxStatus };
10067
+ export { ContextType, MainView, ProductConfigurator, ProductKinds, ProductTypes, SelMinSelMaxStatus, index_esm as hookForm };
9700
10068
  //# sourceMappingURL=index.es.js.map