@maif/react-forms 1.0.53 → 1.0.56

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/README.md CHANGED
@@ -18,12 +18,12 @@ yarn add @maif/react-forms
18
18
  ```
19
19
 
20
20
  # Usage
21
- You must define a form flow (this is just a javascript array which that represent the rendering order of the form fields) and a schema (that is define all field of your form with type or constraints for example)
21
+ You must define a form flow (this is just a javascript array which represents the rendering order of the form fields) and a schema (that defines all fields of your form with types or constraints for example)
22
22
 
23
23
  ## example
24
24
 
25
25
  ```javascript
26
- import { Form, type, format, constraints } from 'react-forms'
26
+ import { Form, type, format, constraints } from '@maif/react-forms'
27
27
 
28
28
  export const Example = () => {
29
29
  const schema = {
@@ -64,7 +64,7 @@ export const Example = () => {
64
64
  ## schema properties
65
65
 
66
66
  - **type** (required): the type of value. It provided by the imported object `type`. It could be just string like `string`, `number`, `bool`, `object`, `date` or `file`
67
- - **format**: Over the type you can display a special format for the field. It provided by the imported object `format` or just a string
67
+ - **format**: Over the type you can display a special format for the field. It is provided by the imported object `format` or just a string
68
68
  - `select`: display a [react-select](https://react-select.com/home) field with provided options
69
69
  - `buttonsSelect`: display a buttons group, drawn with the same options than the format `select`
70
70
  - `code`: if the type is `string`, display a code input (draw with [react-ace](https://github.com/securingsincity/react-ace)) (add a props.setRef to get ace editor ref)
@@ -226,6 +226,7 @@ By default all fields of the form are nullable (they can accept `null` or `undef
226
226
  - [`constraints.test(name: string, message?:string, test: (val: any) => boolean | Promise<boolean>)`](#constraintstestname-string-messagestring-test-val-any--boolean--promiseboolean)
227
227
  - [`constraints.when(ref: string, test: (val: any) => boolean, then: any = [], otherwise: any = [])`](#constraintswhenref-string-test-val-any--boolean-then-any---otherwise-any--)
228
228
  - [`constraints.oneOf(arrayOfValues: any[], message?:string)`](#constraintsoneofarrayofvalues-any-messagestring)
229
+ - [`constraints.ref(ref: any)`](#constraintsrefrefany)
229
230
  - [string](#string)
230
231
  - [string or number](#string-or-number)
231
232
  - [number](#number)
@@ -279,6 +280,7 @@ the following methods works for all type types of value.
279
280
 
280
281
  #### `constraints.oneOf(arrayOfValues: any[], message?:string)`
281
282
  Whitelist a set of values and display an error under field if the provided value is not contains in this set.
283
+ Values can also be a [`reference`](#constraintsrefrefany) of value
282
284
 
283
285
  ```javascript
284
286
  constraints.oneOf(['foo', 'bar'], 'not foo or bar :(')
@@ -287,6 +289,33 @@ the following methods works for all type types of value.
287
289
  {type: 'oneOf', arrayOfValues: ['foo', 'bar'], message: 'not foo or bar :('}
288
290
  ```
289
291
 
292
+ #### `constraints.ref(ref:any)`
293
+ Some constraints accepts reference as property. This methods create a reference to another field.
294
+ Refs are evaluated in the proper order so that the ref value is resolved before the field using the ref (be careful of circular dependencies!).
295
+
296
+ ```javascript
297
+ const schema = {
298
+ dad_age: { type: type.number},
299
+ son_age: {
300
+ type: type.number,
301
+ constraints: [
302
+ constraints.lessThan(constraints.ref('dad_age'), 'too old')
303
+ ]
304
+ }
305
+ }
306
+ ```
307
+ ```javascript
308
+ const schema = {
309
+ dad_age: { type: 'number'},
310
+ son_age: {
311
+ type: 'number',
312
+ constraints: [
313
+ {type: 'lessThan', ref: { ref: 'dad_age' }, message: 'too old !'}
314
+ ]
315
+ }
316
+ }
317
+ ```
318
+
290
319
  ### string
291
320
  the following methods works for string values. All methods for [mixed](#mixed) are available.
292
321
 
@@ -324,13 +353,13 @@ the following methods works for string or number values. All methods for [mixed]
324
353
 
325
354
 
326
355
  #### `constraints.min(ref: number | Reference<number>, message: string)`
327
- Set the minimum value allowed and display an error if the provided value (for a number) or the length of the string is bigger.
356
+ Set the minimum value allowed and display an error if the provided value (for a number) or the length of the string is bigger. Provided value can be a number or a [`reference`](#constraintsrefrefany) to a number
328
357
  ```javascript
329
358
  {type: 'min', ref: 1, message: 'too small'}
330
359
  ```
331
360
 
332
361
  #### `constraints.max(ref: number | Reference<number>, message: string)`
333
- Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller.
362
+ Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller. value can be a number or a [`reference`](#constraintsrefrefany) to a number
334
363
  ```javascript
335
364
  {type: 'max', ref: 5, message: 'too high'}
336
365
  ```
@@ -356,13 +385,13 @@ The value must be aa integer number and display an error if it's not.
356
385
  ```
357
386
 
358
387
  #### `constraints.lessThan(ref: number | Reference<number>, message: string)`
359
- Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller.
388
+ Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller. value can be a number or a [`reference`](#constraintsrefrefany) to a number
360
389
  ```javascript
361
390
  {type: 'lessThan', ref: 5, message: 'less please'}
362
391
  ```
363
392
 
364
393
  #### `constraints.moreThan(ref: number | Reference<number>, message: string)`
365
- Set the minimum value allowed and display an error if provided value (for a number) or the length of the string is bigger.
394
+ Set the minimum value allowed and display an error if provided value (for a number) or the length of the string is bigger. value can be a number or a [`reference`](#constraintsrefrefany) to a number
366
395
  ```javascript
367
396
  {type: 'moreThan', ref: 5, message: 'more please'}
368
397
  ```
@@ -371,7 +400,7 @@ Set the minimum value allowed and display an error if provided value (for a numb
371
400
  the following methods works for basic types if the format is define to `array`. All methods for [mixed](#mixed) are available.
372
401
 
373
402
  #### `constraints.length(value: number | Reference<number>, message?:string)`
374
- Set the length of the array and display an error if it's different.
403
+ Set the length of the array and display an error if it's different. value can be a number or a [`reference`](#constraintsrefrefany) to a number
375
404
  ```javascript
376
405
  {type: 'length', ref: 5, message: 'this array must have 5 elements'}
377
406
  ```
package/lib/esm/index.js CHANGED
@@ -3,7 +3,7 @@ import React, { useState, useEffect, useRef, useImperativeHandle } from 'react';
3
3
  import { yupResolver } from '@hookform/resolvers/yup';
4
4
  import classNames from 'classnames';
5
5
  import { Eye, EyeOff, PlusCircle, MinusCircle, Trash2, ChevronDown, ChevronUp, HelpCircle, Loader, Upload } from 'react-feather';
6
- import { useController, useFormContext, useForm, FormProvider, Controller, useFieldArray, useWatch } from 'react-hook-form';
6
+ import { useFormContext, useController, useForm, FormProvider, Controller, useFieldArray, useWatch } from 'react-hook-form';
7
7
  import { DatePicker } from 'react-rainbow-components';
8
8
  import ReactToolTip from 'react-tooltip';
9
9
  import { v4 } from 'uuid';
@@ -74,7 +74,8 @@ var _matches = function matches() {
74
74
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This field does not match the pattern";
75
75
  return function (r) {
76
76
  return r.matches(regexp, {
77
- message: message
77
+ message: message,
78
+ excludeEmptyString: false
78
79
  });
79
80
  };
80
81
  }; //string & number
@@ -215,7 +216,7 @@ var _oneOf = function oneOf(arrayOfValues) {
215
216
  return function (r) {
216
217
  return r.oneOf(arrayOfValues.map(maybeRef), message);
217
218
  };
218
- };
219
+ }; //todo: rename by notOneOf
219
220
 
220
221
  var _blacklist = function blacklist(arrayOfValues) {
221
222
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This value can't include the following values ".concat(arrayOfValues.join(', '));
@@ -936,6 +937,9 @@ var arrayFlatten = function arrayFlatten(array) {
936
937
 
937
938
  return array;
938
939
  };
940
+ var isDefined = function isDefined(value) {
941
+ return value !== null && value !== undefined;
942
+ };
939
943
 
940
944
  var valueToSelectOption = function valueToSelectOption(value) {
941
945
  var possibleValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
@@ -970,6 +974,9 @@ var valueToSelectOption = function valueToSelectOption(value) {
970
974
  };
971
975
 
972
976
  var SelectInput = /*#__PURE__*/React.forwardRef(function (props, _) {
977
+ var _useFormContext = useFormContext(),
978
+ getValues = _useFormContext.getValues;
979
+
973
980
  var classes = useCustomStyle();
974
981
  var possibleValues = (props.possibleValues || []).map(function (v) {
975
982
  return props.transformer ? typeof props.transformer === 'function' ? props.transformer(v) : {
@@ -1009,9 +1016,24 @@ var SelectInput = /*#__PURE__*/React.forwardRef(function (props, _) {
1009
1016
 
1010
1017
  if (cond) {
1011
1018
  setLoading(true);
1012
- var promise = isPromise(props.optionsFrom) ? props.optionsFrom : props.httpClient(props.optionsFrom, 'GET').then(function (r) {
1013
- return r.json();
1014
- });
1019
+ var promise;
1020
+
1021
+ if (isPromise(props.optionsFrom)) {
1022
+ promise = props.optionsFrom;
1023
+ } else if (typeof props.optionsFrom === 'function') {
1024
+ var result = props.optionsFrom({
1025
+ rawValues: getValues(),
1026
+ value: getValues(props.id)
1027
+ });
1028
+ promise = isPromise(result) ? result : props.httpClient(result, 'GET').then(function (r) {
1029
+ return r.json();
1030
+ });
1031
+ } else {
1032
+ promise = props.httpClient(props.optionsFrom, 'GET').then(function (r) {
1033
+ return r.json();
1034
+ });
1035
+ }
1036
+
1015
1037
  promise.then(function (values) {
1016
1038
  return values.map(function (x) {
1017
1039
  return props.transformer ? props.transformer(x) : valueToSelectOption(x, values, props.isMulti, props.transformer);
@@ -1098,7 +1120,6 @@ var SelectInput = /*#__PURE__*/React.forwardRef(function (props, _) {
1098
1120
  value: value,
1099
1121
  isDisabled: props.disabled,
1100
1122
  placeholder: props.placeholder,
1101
- isClearable: true,
1102
1123
  onChange: onChange,
1103
1124
  options: values,
1104
1125
  onCreateOption: function onCreateOption(option) {
@@ -1113,7 +1134,6 @@ var SelectInput = /*#__PURE__*/React.forwardRef(function (props, _) {
1113
1134
  name: "".concat(props.label, "-search"),
1114
1135
  isLoading: loading,
1115
1136
  value: value,
1116
- isClearable: true,
1117
1137
  defaultValue: value,
1118
1138
  isDisabled: props.disabled,
1119
1139
  placeholder: props.placeholder,
@@ -28863,18 +28883,28 @@ var buildSubResolver = function buildSubResolver(props, key, dependencies, rawDa
28863
28883
  return s.condition === ref;
28864
28884
  }
28865
28885
  });
28866
- return option(filterSwitch).getOrElse(condiSchema["switch"].find(function (s) {
28886
+ return option(filterSwitch).orElse(condiSchema["switch"].find(function (s) {
28867
28887
  return s["default"];
28868
- }));
28869
- }).getOrElse({}),
28888
+ })).getOrElse({
28889
+ schema: {},
28890
+ flow: []
28891
+ });
28892
+ }).getOrElse({
28893
+ schema: {},
28894
+ flow: []
28895
+ }),
28870
28896
  schema = _option$map$getOrElse.schema,
28871
28897
  flow = _option$map$getOrElse.flow;
28872
28898
 
28873
- var _subResolver2 = getShapeAndDependencies(flow || Object.keys(schema), schema, dependencies, rawData);
28899
+ var realFlow = flow || Object.keys(schema);
28874
28900
 
28875
- return constraints.reduce(function (resolver, constraint) {
28876
- return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
28877
- }, yup.object().shape(_subResolver2.shape, dependencies));
28901
+ if (realFlow.length) {
28902
+ var _subResolver2 = getShapeAndDependencies(flow || Object.keys(schema), schema, dependencies, rawData);
28903
+
28904
+ return constraints.reduce(function (resolver, constraint) {
28905
+ return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
28906
+ }, yup.object().shape(_subResolver2.shape, dependencies));
28907
+ }
28878
28908
  } else {
28879
28909
  return constraints.reduce(function (resolver, constraint) {
28880
28910
  return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
@@ -28928,22 +28958,21 @@ var CustomizableInput$1 = /*#__PURE__*/React.memo(function (props) {
28928
28958
 
28929
28959
  return props.children;
28930
28960
  }, function (prev, next) {
28931
- if (next.render) {
28961
+ if (next.render || next.conditionalSchema) {
28932
28962
  return false;
28933
28963
  }
28934
28964
 
28935
28965
  return prev.field.value === next.field.value && next.errorDisplayed === prev.errorDisplayed;
28936
28966
  });
28937
28967
  var ControlledInput = function ControlledInput(_ref) {
28938
- var defaultValue = _ref.defaultValue,
28939
- step = _ref.step,
28968
+ var step = _ref.step,
28940
28969
  entry = _ref.entry,
28941
28970
  children = _ref.children,
28942
28971
  component = _ref.component,
28943
28972
  errorDisplayed = _ref.errorDisplayed;
28944
28973
 
28945
28974
  var _useController = useController({
28946
- defaultValue: defaultValue || null,
28975
+ defaultValue: isDefined(step.defaultValue) ? step.defaultValue : null,
28947
28976
  name: entry
28948
28977
  }),
28949
28978
  field = _useController.field;
@@ -29103,8 +29132,8 @@ var CustomizableInput = function CustomizableInput(props) {
29103
29132
  return props.children;
29104
29133
  };
29105
29134
 
29106
- var defaultVal = function defaultVal(value, array, defaultValue) {
29107
- if (!!defaultValue) return defaultValue;
29135
+ var defaultVal = function defaultVal(value, array, defaultValue, type) {
29136
+ if (isDefined(defaultValue)) return defaultValue;
29108
29137
  if (!!array) return [];
29109
29138
  return value;
29110
29139
  };
@@ -29141,9 +29170,17 @@ var cleanInputArray = function cleanInputArray(obj, defaultValues, flow, subSche
29141
29170
  key = _ref5[0],
29142
29171
  step = _ref5[1];
29143
29172
 
29144
- var v;
29145
- if (obj) v = obj[key];
29146
- if ((step.type === type.bool && v === null || step.type !== type.bool && !v) && defaultValues) v = defaultValues[key];
29173
+ var v = null;
29174
+
29175
+ if (obj) {
29176
+ v = obj[key];
29177
+ }
29178
+
29179
+ var maybeDefaultValue = defaultValues[key];
29180
+
29181
+ if (!v && isDefined(maybeDefaultValue)) {
29182
+ v = maybeDefaultValue;
29183
+ }
29147
29184
 
29148
29185
  if (step.array && !step.render) {
29149
29186
  return _objectSpread2$1(_objectSpread2$1({}, acc), {}, _defineProperty$1({}, key, (v || []).map(function (value) {
@@ -29305,21 +29342,20 @@ var Form = /*#__PURE__*/React.forwardRef(function (_ref7, ref) {
29305
29342
 
29306
29343
  var _useState = useState(false),
29307
29344
  _useState2 = _slicedToArray(_useState, 2),
29308
- initialReseted = _useState2[0],
29309
- setReset = _useState2[1];
29345
+ initialReseted = _useState2[0];
29346
+ _useState2[1]; // useEffect(() => {
29347
+ // reset(cleanInputArray(value, defaultValues, flow, schema))
29348
+ // setReset(true)
29349
+ // }, [reset])
29310
29350
 
29311
- useEffect(function () {
29312
- _reset(cleanInputArray(value, defaultValues, flow, schema));
29313
29351
 
29314
- setReset(true);
29315
- }, [_reset]);
29316
29352
  useEffect(function () {
29317
- initialReseted && trigger();
29353
+ trigger();
29318
29354
  }, [trigger, initialReseted]);
29319
29355
  var _handleSubmit = methods.handleSubmit,
29320
- _methods$formState = methods.formState,
29321
- errors = _methods$formState.errors,
29322
- dirtyFields = _methods$formState.dirtyFields,
29356
+ _methods$formState = methods.formState;
29357
+ _methods$formState.errors;
29358
+ var dirtyFields = _methods$formState.dirtyFields,
29323
29359
  _reset = methods.reset,
29324
29360
  trigger = methods.trigger,
29325
29361
  getValues = methods.getValues,
@@ -29327,7 +29363,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (_ref7, ref) {
29327
29363
  var prev = usePrevious(value);
29328
29364
  var prevSchema = usePrevious(schema);
29329
29365
  useEffect(function () {
29330
- if (!deepEqual(value, prev) || !deepEqual(schema, prevSchema)) {
29366
+ if (prev && prevSchema && !deepEqual(value, prev) || !deepEqual(schema, prevSchema)) {
29331
29367
  _reset(_objectSpread2$1({}, cleanInputArray(value, defaultValues, flow, schema)));
29332
29368
  }
29333
29369
  }, [value, schema]);
@@ -29380,9 +29416,6 @@ var Form = /*#__PURE__*/React.forwardRef(function (_ref7, ref) {
29380
29416
  return null;
29381
29417
  }
29382
29418
 
29383
- _typeof$1(entry) === 'object' ? undefined : entry.split('.').reduce(function (object, key) {
29384
- return object && object[key];
29385
- }, errors);
29386
29419
  var visibleStep = option(step).map(function (s) {
29387
29420
  return s.visible;
29388
29421
  }).map(function (visible) {
@@ -29670,9 +29703,10 @@ var Step = function Step(_ref8) {
29670
29703
  step: step,
29671
29704
  entry: entry,
29672
29705
  errorDisplayed: errorDisplayed
29673
- }, /*#__PURE__*/React.createElement(SelectInput, {
29706
+ }, /*#__PURE__*/React.createElement(SelectInput, _extends({
29674
29707
  className: classNames(classes.flex_grow_1, _defineProperty$1({}, classes.input__invalid, errorDisplayed)),
29675
- disabled: functionalProperty(entry, step.disabled),
29708
+ disabled: functionalProperty(entry, step.disabled)
29709
+ }, step.props, {
29676
29710
  possibleValues: step.options,
29677
29711
  httpClient: httpClient,
29678
29712
  isMulti: step.isMulti,
@@ -29680,7 +29714,7 @@ var Step = function Step(_ref8) {
29680
29714
  transformer: step.transformer,
29681
29715
  buttons: step.format === format.buttonsSelect,
29682
29716
  optionsFrom: step.optionsFrom
29683
- }));
29717
+ })));
29684
29718
  }
29685
29719
 
29686
29720
  default:
@@ -29704,8 +29738,9 @@ var Step = function Step(_ref8) {
29704
29738
  step: step,
29705
29739
  entry: entry,
29706
29740
  errorDisplayed: errorDisplayed
29707
- }, /*#__PURE__*/React.createElement(SelectInput, {
29708
- className: classNames(classes.content, _defineProperty$1({}, classes.input__invalid, errorDisplayed)),
29741
+ }, /*#__PURE__*/React.createElement(SelectInput, _extends({
29742
+ className: classNames(classes.content, _defineProperty$1({}, classes.input__invalid, errorDisplayed))
29743
+ }, step.props, {
29709
29744
  possibleValues: step.options,
29710
29745
  httpClient: httpClient,
29711
29746
  isMulti: step.isMulti,
@@ -29714,7 +29749,7 @@ var Step = function Step(_ref8) {
29714
29749
  transformer: step.transformer,
29715
29750
  buttons: step.format === format.buttonsSelect,
29716
29751
  optionsFrom: step.optionsFrom
29717
- }));
29752
+ })));
29718
29753
 
29719
29754
  default:
29720
29755
  return /*#__PURE__*/React.createElement(ControlledInput, {
@@ -29730,7 +29765,6 @@ var Step = function Step(_ref8) {
29730
29765
 
29731
29766
  case type.bool:
29732
29767
  return /*#__PURE__*/React.createElement(ControlledInput, {
29733
- defaultValue: defaultValue,
29734
29768
  step: step,
29735
29769
  entry: entry,
29736
29770
  errorDisplayed: errorDisplayed
@@ -29747,8 +29781,9 @@ var Step = function Step(_ref8) {
29747
29781
  step: step,
29748
29782
  entry: entry,
29749
29783
  errorDisplayed: errorDisplayed
29750
- }, /*#__PURE__*/React.createElement(SelectInput, {
29751
- className: classNames(classes.flex_grow_1, _defineProperty$1({}, classes.input__invalid, errorDisplayed)),
29784
+ }, /*#__PURE__*/React.createElement(SelectInput, _extends({
29785
+ className: classNames(classes.flex_grow_1, _defineProperty$1({}, classes.input__invalid, errorDisplayed))
29786
+ }, step.props, {
29752
29787
  possibleValues: step.options,
29753
29788
  httpClient: httpClient,
29754
29789
  isMulti: step.isMulti,
@@ -29757,7 +29792,7 @@ var Step = function Step(_ref8) {
29757
29792
  transformer: step.transformer,
29758
29793
  buttons: step.format === format.buttonsSelect,
29759
29794
  optionsFrom: step.optionsFrom
29760
- }));
29795
+ })));
29761
29796
 
29762
29797
  case format.form:
29763
29798
  //todo: disabled ?
@@ -30032,7 +30067,7 @@ var ArrayStep = function ArrayStep(_ref11) {
30032
30067
  };
30033
30068
 
30034
30069
  var NestedForm = function NestedForm(_ref12) {
30035
- var _classNames17;
30070
+ var _step$conditionalSche, _classNames17;
30036
30071
 
30037
30072
  var schema = _ref12.schema,
30038
30073
  flow = _ref12.flow,
@@ -30049,8 +30084,6 @@ var NestedForm = function NestedForm(_ref12) {
30049
30084
  getValues = _useFormContext4.getValues,
30050
30085
  setValue = _useFormContext4.setValue,
30051
30086
  watch = _useFormContext4.watch;
30052
- _useFormContext4.trigger;
30053
- _useFormContext4.formState;
30054
30087
 
30055
30088
  var _useState7 = useState(!!step.collapsed),
30056
30089
  _useState8 = _slicedToArray(_useState7, 2),
@@ -30058,6 +30091,7 @@ var NestedForm = function NestedForm(_ref12) {
30058
30091
  setCollapsed = _useState8[1];
30059
30092
 
30060
30093
  var classes = useCustomStyle();
30094
+ useWatch(step === null || step === void 0 ? void 0 : (_step$conditionalSche = step.conditionalSchema) === null || _step$conditionalSche === void 0 ? void 0 : _step$conditionalSche.ref);
30061
30095
  var schemaAndFlow = option(step.conditionalSchema).map(function (condiSchema) {
30062
30096
  var ref = option(condiSchema.ref).map(function (ref) {
30063
30097
  return getValues(ref);
@@ -30073,9 +30107,16 @@ var NestedForm = function NestedForm(_ref12) {
30073
30107
  return s.condition === ref;
30074
30108
  }
30075
30109
  });
30076
- return option(filterSwitch).getOrElse(condiSchema["switch"].find(function (s) {
30110
+ var schemaAndFlow = option(filterSwitch).orElse(condiSchema["switch"].find(function (s) {
30077
30111
  return s["default"];
30078
- }));
30112
+ })).getOrElse({
30113
+ schema: {},
30114
+ flow: []
30115
+ });
30116
+ return {
30117
+ schema: schemaAndFlow.schema,
30118
+ flow: schemaAndFlow.flow || Object.keys(schemaAndFlow.schema)
30119
+ };
30079
30120
  }).getOrElse({
30080
30121
  schema: schema,
30081
30122
  flow: flow
@@ -30117,7 +30158,7 @@ var NestedForm = function NestedForm(_ref12) {
30117
30158
  }, []);
30118
30159
  var bordered = computedSandF.filter(function (x) {
30119
30160
  return x.visibleStep;
30120
- }).length > 1 && step.label !== null;
30161
+ }).length >= 1 && step.label !== null;
30121
30162
  return /*#__PURE__*/React.createElement("div", {
30122
30163
  className: classNames((_classNames17 = {}, _defineProperty$1(_classNames17, classes.nestedform__border, bordered), _defineProperty$1(_classNames17, classes.border__error, !!errorDisplayed), _classNames17)),
30123
30164
  style: {
package/lib/index.js CHANGED
@@ -108,7 +108,8 @@ var _matches = function matches() {
108
108
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This field does not match the pattern";
109
109
  return function (r) {
110
110
  return r.matches(regexp, {
111
- message: message
111
+ message: message,
112
+ excludeEmptyString: false
112
113
  });
113
114
  };
114
115
  }; //string & number
@@ -249,7 +250,7 @@ var _oneOf = function oneOf(arrayOfValues) {
249
250
  return function (r) {
250
251
  return r.oneOf(arrayOfValues.map(maybeRef), message);
251
252
  };
252
- };
253
+ }; //todo: rename by notOneOf
253
254
 
254
255
  var _blacklist = function blacklist(arrayOfValues) {
255
256
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This value can't include the following values ".concat(arrayOfValues.join(', '));
@@ -970,6 +971,9 @@ var arrayFlatten = function arrayFlatten(array) {
970
971
 
971
972
  return array;
972
973
  };
974
+ var isDefined = function isDefined(value) {
975
+ return value !== null && value !== undefined;
976
+ };
973
977
 
974
978
  var valueToSelectOption = function valueToSelectOption(value) {
975
979
  var possibleValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
@@ -1004,6 +1008,9 @@ var valueToSelectOption = function valueToSelectOption(value) {
1004
1008
  };
1005
1009
 
1006
1010
  var SelectInput = /*#__PURE__*/React__default["default"].forwardRef(function (props, _) {
1011
+ var _useFormContext = reactHookForm.useFormContext(),
1012
+ getValues = _useFormContext.getValues;
1013
+
1007
1014
  var classes = useCustomStyle();
1008
1015
  var possibleValues = (props.possibleValues || []).map(function (v) {
1009
1016
  return props.transformer ? typeof props.transformer === 'function' ? props.transformer(v) : {
@@ -1043,9 +1050,24 @@ var SelectInput = /*#__PURE__*/React__default["default"].forwardRef(function (pr
1043
1050
 
1044
1051
  if (cond) {
1045
1052
  setLoading(true);
1046
- var promise = isPromise(props.optionsFrom) ? props.optionsFrom : props.httpClient(props.optionsFrom, 'GET').then(function (r) {
1047
- return r.json();
1048
- });
1053
+ var promise;
1054
+
1055
+ if (isPromise(props.optionsFrom)) {
1056
+ promise = props.optionsFrom;
1057
+ } else if (typeof props.optionsFrom === 'function') {
1058
+ var result = props.optionsFrom({
1059
+ rawValues: getValues(),
1060
+ value: getValues(props.id)
1061
+ });
1062
+ promise = isPromise(result) ? result : props.httpClient(result, 'GET').then(function (r) {
1063
+ return r.json();
1064
+ });
1065
+ } else {
1066
+ promise = props.httpClient(props.optionsFrom, 'GET').then(function (r) {
1067
+ return r.json();
1068
+ });
1069
+ }
1070
+
1049
1071
  promise.then(function (values) {
1050
1072
  return values.map(function (x) {
1051
1073
  return props.transformer ? props.transformer(x) : valueToSelectOption(x, values, props.isMulti, props.transformer);
@@ -1132,7 +1154,6 @@ var SelectInput = /*#__PURE__*/React__default["default"].forwardRef(function (pr
1132
1154
  value: value,
1133
1155
  isDisabled: props.disabled,
1134
1156
  placeholder: props.placeholder,
1135
- isClearable: true,
1136
1157
  onChange: onChange,
1137
1158
  options: values,
1138
1159
  onCreateOption: function onCreateOption(option) {
@@ -1147,7 +1168,6 @@ var SelectInput = /*#__PURE__*/React__default["default"].forwardRef(function (pr
1147
1168
  name: "".concat(props.label, "-search"),
1148
1169
  isLoading: loading,
1149
1170
  value: value,
1150
- isClearable: true,
1151
1171
  defaultValue: value,
1152
1172
  isDisabled: props.disabled,
1153
1173
  placeholder: props.placeholder,
@@ -28897,18 +28917,28 @@ var buildSubResolver = function buildSubResolver(props, key, dependencies, rawDa
28897
28917
  return s.condition === ref;
28898
28918
  }
28899
28919
  });
28900
- return option(filterSwitch).getOrElse(condiSchema["switch"].find(function (s) {
28920
+ return option(filterSwitch).orElse(condiSchema["switch"].find(function (s) {
28901
28921
  return s["default"];
28902
- }));
28903
- }).getOrElse({}),
28922
+ })).getOrElse({
28923
+ schema: {},
28924
+ flow: []
28925
+ });
28926
+ }).getOrElse({
28927
+ schema: {},
28928
+ flow: []
28929
+ }),
28904
28930
  schema = _option$map$getOrElse.schema,
28905
28931
  flow = _option$map$getOrElse.flow;
28906
28932
 
28907
- var _subResolver2 = getShapeAndDependencies(flow || Object.keys(schema), schema, dependencies, rawData);
28933
+ var realFlow = flow || Object.keys(schema);
28908
28934
 
28909
- return constraints.reduce(function (resolver, constraint) {
28910
- return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
28911
- }, yup__namespace.object().shape(_subResolver2.shape, dependencies));
28935
+ if (realFlow.length) {
28936
+ var _subResolver2 = getShapeAndDependencies(flow || Object.keys(schema), schema, dependencies, rawData);
28937
+
28938
+ return constraints.reduce(function (resolver, constraint) {
28939
+ return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
28940
+ }, yup__namespace.object().shape(_subResolver2.shape, dependencies));
28941
+ }
28912
28942
  } else {
28913
28943
  return constraints.reduce(function (resolver, constraint) {
28914
28944
  return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
@@ -28962,22 +28992,21 @@ var CustomizableInput$1 = /*#__PURE__*/React__default["default"].memo(function (
28962
28992
 
28963
28993
  return props.children;
28964
28994
  }, function (prev, next) {
28965
- if (next.render) {
28995
+ if (next.render || next.conditionalSchema) {
28966
28996
  return false;
28967
28997
  }
28968
28998
 
28969
28999
  return prev.field.value === next.field.value && next.errorDisplayed === prev.errorDisplayed;
28970
29000
  });
28971
29001
  var ControlledInput = function ControlledInput(_ref) {
28972
- var defaultValue = _ref.defaultValue,
28973
- step = _ref.step,
29002
+ var step = _ref.step,
28974
29003
  entry = _ref.entry,
28975
29004
  children = _ref.children,
28976
29005
  component = _ref.component,
28977
29006
  errorDisplayed = _ref.errorDisplayed;
28978
29007
 
28979
29008
  var _useController = reactHookForm.useController({
28980
- defaultValue: defaultValue || null,
29009
+ defaultValue: isDefined(step.defaultValue) ? step.defaultValue : null,
28981
29010
  name: entry
28982
29011
  }),
28983
29012
  field = _useController.field;
@@ -29137,8 +29166,8 @@ var CustomizableInput = function CustomizableInput(props) {
29137
29166
  return props.children;
29138
29167
  };
29139
29168
 
29140
- var defaultVal = function defaultVal(value, array, defaultValue) {
29141
- if (!!defaultValue) return defaultValue;
29169
+ var defaultVal = function defaultVal(value, array, defaultValue, type) {
29170
+ if (isDefined(defaultValue)) return defaultValue;
29142
29171
  if (!!array) return [];
29143
29172
  return value;
29144
29173
  };
@@ -29175,9 +29204,17 @@ var cleanInputArray = function cleanInputArray(obj, defaultValues, flow, subSche
29175
29204
  key = _ref5[0],
29176
29205
  step = _ref5[1];
29177
29206
 
29178
- var v;
29179
- if (obj) v = obj[key];
29180
- if ((step.type === type.bool && v === null || step.type !== type.bool && !v) && defaultValues) v = defaultValues[key];
29207
+ var v = null;
29208
+
29209
+ if (obj) {
29210
+ v = obj[key];
29211
+ }
29212
+
29213
+ var maybeDefaultValue = defaultValues[key];
29214
+
29215
+ if (!v && isDefined(maybeDefaultValue)) {
29216
+ v = maybeDefaultValue;
29217
+ }
29181
29218
 
29182
29219
  if (step.array && !step.render) {
29183
29220
  return _objectSpread2$1(_objectSpread2$1({}, acc), {}, _defineProperty$1({}, key, (v || []).map(function (value) {
@@ -29339,21 +29376,20 @@ var Form = /*#__PURE__*/React__default["default"].forwardRef(function (_ref7, re
29339
29376
 
29340
29377
  var _useState = React.useState(false),
29341
29378
  _useState2 = _slicedToArray(_useState, 2),
29342
- initialReseted = _useState2[0],
29343
- setReset = _useState2[1];
29379
+ initialReseted = _useState2[0];
29380
+ _useState2[1]; // useEffect(() => {
29381
+ // reset(cleanInputArray(value, defaultValues, flow, schema))
29382
+ // setReset(true)
29383
+ // }, [reset])
29344
29384
 
29345
- React.useEffect(function () {
29346
- _reset(cleanInputArray(value, defaultValues, flow, schema));
29347
29385
 
29348
- setReset(true);
29349
- }, [_reset]);
29350
29386
  React.useEffect(function () {
29351
- initialReseted && trigger();
29387
+ trigger();
29352
29388
  }, [trigger, initialReseted]);
29353
29389
  var _handleSubmit = methods.handleSubmit,
29354
- _methods$formState = methods.formState,
29355
- errors = _methods$formState.errors,
29356
- dirtyFields = _methods$formState.dirtyFields,
29390
+ _methods$formState = methods.formState;
29391
+ _methods$formState.errors;
29392
+ var dirtyFields = _methods$formState.dirtyFields,
29357
29393
  _reset = methods.reset,
29358
29394
  trigger = methods.trigger,
29359
29395
  getValues = methods.getValues,
@@ -29361,7 +29397,7 @@ var Form = /*#__PURE__*/React__default["default"].forwardRef(function (_ref7, re
29361
29397
  var prev = usePrevious(value);
29362
29398
  var prevSchema = usePrevious(schema);
29363
29399
  React.useEffect(function () {
29364
- if (!deepEqual__default["default"](value, prev) || !deepEqual__default["default"](schema, prevSchema)) {
29400
+ if (prev && prevSchema && !deepEqual__default["default"](value, prev) || !deepEqual__default["default"](schema, prevSchema)) {
29365
29401
  _reset(_objectSpread2$1({}, cleanInputArray(value, defaultValues, flow, schema)));
29366
29402
  }
29367
29403
  }, [value, schema]);
@@ -29414,9 +29450,6 @@ var Form = /*#__PURE__*/React__default["default"].forwardRef(function (_ref7, re
29414
29450
  return null;
29415
29451
  }
29416
29452
 
29417
- _typeof$1(entry) === 'object' ? undefined : entry.split('.').reduce(function (object, key) {
29418
- return object && object[key];
29419
- }, errors);
29420
29453
  var visibleStep = option(step).map(function (s) {
29421
29454
  return s.visible;
29422
29455
  }).map(function (visible) {
@@ -29704,9 +29737,10 @@ var Step = function Step(_ref8) {
29704
29737
  step: step,
29705
29738
  entry: entry,
29706
29739
  errorDisplayed: errorDisplayed
29707
- }, /*#__PURE__*/React__default["default"].createElement(SelectInput, {
29740
+ }, /*#__PURE__*/React__default["default"].createElement(SelectInput, _extends({
29708
29741
  className: classNames__default["default"](classes.flex_grow_1, _defineProperty$1({}, classes.input__invalid, errorDisplayed)),
29709
- disabled: functionalProperty(entry, step.disabled),
29742
+ disabled: functionalProperty(entry, step.disabled)
29743
+ }, step.props, {
29710
29744
  possibleValues: step.options,
29711
29745
  httpClient: httpClient,
29712
29746
  isMulti: step.isMulti,
@@ -29714,7 +29748,7 @@ var Step = function Step(_ref8) {
29714
29748
  transformer: step.transformer,
29715
29749
  buttons: step.format === format.buttonsSelect,
29716
29750
  optionsFrom: step.optionsFrom
29717
- }));
29751
+ })));
29718
29752
  }
29719
29753
 
29720
29754
  default:
@@ -29738,8 +29772,9 @@ var Step = function Step(_ref8) {
29738
29772
  step: step,
29739
29773
  entry: entry,
29740
29774
  errorDisplayed: errorDisplayed
29741
- }, /*#__PURE__*/React__default["default"].createElement(SelectInput, {
29742
- className: classNames__default["default"](classes.content, _defineProperty$1({}, classes.input__invalid, errorDisplayed)),
29775
+ }, /*#__PURE__*/React__default["default"].createElement(SelectInput, _extends({
29776
+ className: classNames__default["default"](classes.content, _defineProperty$1({}, classes.input__invalid, errorDisplayed))
29777
+ }, step.props, {
29743
29778
  possibleValues: step.options,
29744
29779
  httpClient: httpClient,
29745
29780
  isMulti: step.isMulti,
@@ -29748,7 +29783,7 @@ var Step = function Step(_ref8) {
29748
29783
  transformer: step.transformer,
29749
29784
  buttons: step.format === format.buttonsSelect,
29750
29785
  optionsFrom: step.optionsFrom
29751
- }));
29786
+ })));
29752
29787
 
29753
29788
  default:
29754
29789
  return /*#__PURE__*/React__default["default"].createElement(ControlledInput, {
@@ -29764,7 +29799,6 @@ var Step = function Step(_ref8) {
29764
29799
 
29765
29800
  case type.bool:
29766
29801
  return /*#__PURE__*/React__default["default"].createElement(ControlledInput, {
29767
- defaultValue: defaultValue,
29768
29802
  step: step,
29769
29803
  entry: entry,
29770
29804
  errorDisplayed: errorDisplayed
@@ -29781,8 +29815,9 @@ var Step = function Step(_ref8) {
29781
29815
  step: step,
29782
29816
  entry: entry,
29783
29817
  errorDisplayed: errorDisplayed
29784
- }, /*#__PURE__*/React__default["default"].createElement(SelectInput, {
29785
- className: classNames__default["default"](classes.flex_grow_1, _defineProperty$1({}, classes.input__invalid, errorDisplayed)),
29818
+ }, /*#__PURE__*/React__default["default"].createElement(SelectInput, _extends({
29819
+ className: classNames__default["default"](classes.flex_grow_1, _defineProperty$1({}, classes.input__invalid, errorDisplayed))
29820
+ }, step.props, {
29786
29821
  possibleValues: step.options,
29787
29822
  httpClient: httpClient,
29788
29823
  isMulti: step.isMulti,
@@ -29791,7 +29826,7 @@ var Step = function Step(_ref8) {
29791
29826
  transformer: step.transformer,
29792
29827
  buttons: step.format === format.buttonsSelect,
29793
29828
  optionsFrom: step.optionsFrom
29794
- }));
29829
+ })));
29795
29830
 
29796
29831
  case format.form:
29797
29832
  //todo: disabled ?
@@ -30066,7 +30101,7 @@ var ArrayStep = function ArrayStep(_ref11) {
30066
30101
  };
30067
30102
 
30068
30103
  var NestedForm = function NestedForm(_ref12) {
30069
- var _classNames17;
30104
+ var _step$conditionalSche, _classNames17;
30070
30105
 
30071
30106
  var schema = _ref12.schema,
30072
30107
  flow = _ref12.flow,
@@ -30083,8 +30118,6 @@ var NestedForm = function NestedForm(_ref12) {
30083
30118
  getValues = _useFormContext4.getValues,
30084
30119
  setValue = _useFormContext4.setValue,
30085
30120
  watch = _useFormContext4.watch;
30086
- _useFormContext4.trigger;
30087
- _useFormContext4.formState;
30088
30121
 
30089
30122
  var _useState7 = React.useState(!!step.collapsed),
30090
30123
  _useState8 = _slicedToArray(_useState7, 2),
@@ -30092,6 +30125,7 @@ var NestedForm = function NestedForm(_ref12) {
30092
30125
  setCollapsed = _useState8[1];
30093
30126
 
30094
30127
  var classes = useCustomStyle();
30128
+ reactHookForm.useWatch(step === null || step === void 0 ? void 0 : (_step$conditionalSche = step.conditionalSchema) === null || _step$conditionalSche === void 0 ? void 0 : _step$conditionalSche.ref);
30095
30129
  var schemaAndFlow = option(step.conditionalSchema).map(function (condiSchema) {
30096
30130
  var ref = option(condiSchema.ref).map(function (ref) {
30097
30131
  return getValues(ref);
@@ -30107,9 +30141,16 @@ var NestedForm = function NestedForm(_ref12) {
30107
30141
  return s.condition === ref;
30108
30142
  }
30109
30143
  });
30110
- return option(filterSwitch).getOrElse(condiSchema["switch"].find(function (s) {
30144
+ var schemaAndFlow = option(filterSwitch).orElse(condiSchema["switch"].find(function (s) {
30111
30145
  return s["default"];
30112
- }));
30146
+ })).getOrElse({
30147
+ schema: {},
30148
+ flow: []
30149
+ });
30150
+ return {
30151
+ schema: schemaAndFlow.schema,
30152
+ flow: schemaAndFlow.flow || Object.keys(schemaAndFlow.schema)
30153
+ };
30113
30154
  }).getOrElse({
30114
30155
  schema: schema,
30115
30156
  flow: flow
@@ -30151,7 +30192,7 @@ var NestedForm = function NestedForm(_ref12) {
30151
30192
  }, []);
30152
30193
  var bordered = computedSandF.filter(function (x) {
30153
30194
  return x.visibleStep;
30154
- }).length > 1 && step.label !== null;
30195
+ }).length >= 1 && step.label !== null;
30155
30196
  return /*#__PURE__*/React__default["default"].createElement("div", {
30156
30197
  className: classNames__default["default"]((_classNames17 = {}, _defineProperty$1(_classNames17, classes.nestedform__border, bordered), _defineProperty$1(_classNames17, classes.border__error, !!errorDisplayed), _classNames17)),
30157
30198
  style: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@maif/react-forms",
3
3
  "description": "Build react safe forms as fast as possible",
4
- "version": "1.0.53",
4
+ "version": "1.0.56",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/esm/index.js",
7
7
  "types": "lib/index.js",
@@ -56,7 +56,7 @@
56
56
  ]
57
57
  },
58
58
  "peerDependencies": {
59
- "react": "^17.0.2"
59
+ "react": "^17.0.2 || 18"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@babel/cli": "^7.17.6",
@@ -98,7 +98,6 @@
98
98
  "@codemirror/theme-one-dark": "^0.19.1",
99
99
  "@fortawesome/fontawesome-free": "^5.15.3",
100
100
  "@hookform/resolvers": "2.4.0",
101
- "@maif/react-forms": "^1.0.37-rc.1",
102
101
  "@popperjs/core": "^2.11.2",
103
102
  "@testing-library/jest-dom": "^5.11.4",
104
103
  "@testing-library/react": "^11.1.0",