@dartech/arsenal-ui 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -1507,7 +1507,7 @@ const isPropertyValueEmpty = value => {
1507
1507
  return true;
1508
1508
  };
1509
1509
  const formatPropertyValue = (propertyType, value, params) => {
1510
- if ((value === null || value === undefined) && propertyType !== PropertyType.BOOLEAN) {
1510
+ if (value === null || value === undefined) {
1511
1511
  return null;
1512
1512
  }
1513
1513
  let parsedValue = value;
@@ -1516,8 +1516,10 @@ const formatPropertyValue = (propertyType, value, params) => {
1516
1516
  case PropertyType.BOOLEAN:
1517
1517
  if (value) {
1518
1518
  parsedValue = true;
1519
- } else {
1519
+ } else if (!value && value !== null) {
1520
1520
  parsedValue = false;
1521
+ } else {
1522
+ parsedValue = null;
1521
1523
  }
1522
1524
  break;
1523
1525
  case PropertyType.INTEGER:
@@ -1632,7 +1634,7 @@ const propertiesArrayToObject = (properties, isGlobalParameter) => {
1632
1634
  uiSettings,
1633
1635
  validationNode
1634
1636
  } = property;
1635
- const propertyType = typeof property.propertyType === 'string' ? property.propertyType : property.propertyType.value;
1637
+ const propertyType = property.propertyType ? typeof property.propertyType === 'string' ? property.propertyType : property.propertyType.value : null;
1636
1638
  const resultProperty = Object.assign({
1637
1639
  name,
1638
1640
  propertyType,
@@ -2277,10 +2279,12 @@ const ControlArrayInput = ({
2277
2279
  name,
2278
2280
  control,
2279
2281
  label,
2280
- required
2282
+ required,
2283
+ defaultValue: _defaultValue = null
2281
2284
  }) => {
2282
2285
  var _a, _b, _c;
2283
2286
  const [localValue, setLoacalValue] = useState('');
2287
+ const [error, setError] = useState(null);
2284
2288
  const {
2285
2289
  field: {
2286
2290
  onChange,
@@ -2292,22 +2296,25 @@ const ControlArrayInput = ({
2292
2296
  }
2293
2297
  } = useController({
2294
2298
  control,
2295
- name
2299
+ name,
2300
+ defaultValue: _defaultValue
2296
2301
  });
2297
2302
  const handleInputChange = useCallback(e => {
2298
2303
  const {
2299
2304
  value
2300
2305
  } = e.target;
2301
2306
  setLoacalValue(value);
2302
- if (Array.isArray(values) && values.includes(value)) ;
2303
- }, [values]);
2307
+ }, []);
2304
2308
  const handleAdd = useCallback(() => {
2305
2309
  var _a;
2306
- if (localValue && !((_a = errors[name]) === null || _a === void 0 ? void 0 : _a.message)) {
2310
+ setError(null);
2311
+ if (localValue && !((_a = errors[name]) === null || _a === void 0 ? void 0 : _a.message) && !(Array.isArray(values) && values.includes(localValue))) {
2307
2312
  onChange([...(values !== null && values !== void 0 ? values : []), localValue]);
2308
2313
  setLoacalValue('');
2314
+ } else if (Array.isArray(values) && values.includes(localValue)) {
2315
+ setError('This value already exists');
2309
2316
  }
2310
- }, [localValue, onChange, values, name, errors]);
2317
+ }, [localValue, errors, name, onChange, values]);
2311
2318
  const handleDelete = useCallback(value => {
2312
2319
  onChange(values.filter(code => code !== value));
2313
2320
  }, [values, onChange]);
@@ -2323,7 +2330,7 @@ const ControlArrayInput = ({
2323
2330
  variant: "subtitle1"
2324
2331
  }, {
2325
2332
  children: required ? jsxs(Fragment, {
2326
- children: [label, " ", jsx(Box, Object.assign({
2333
+ children: [label, ' ', jsx(Box, Object.assign({
2327
2334
  component: "span",
2328
2335
  color: "#D6331F"
2329
2336
  }, {
@@ -2371,7 +2378,11 @@ const ControlArrayInput = ({
2371
2378
  })
2372
2379
  }))
2373
2380
  }
2374
- })]
2381
+ }), error && jsx(FormHelperText, Object.assign({
2382
+ error: true
2383
+ }, {
2384
+ children: error
2385
+ }))]
2375
2386
  });
2376
2387
  };
2377
2388
 
@@ -4002,7 +4013,7 @@ const usePropertyFiller = ({
4002
4013
  case PropertyType.JSON:
4003
4014
  return {};
4004
4015
  case PropertyType.BOOLEAN:
4005
- return true;
4016
+ return false;
4006
4017
  case PropertyType.INTEGER:
4007
4018
  case PropertyType.BIG_DECIMAL:
4008
4019
  case PropertyType.BIG_INTEGER:
@@ -4024,8 +4035,8 @@ const usePropertyFiller = ({
4024
4035
  }, [value, fillOption, needRecursionCheck]);
4025
4036
  useEffect(() => {
4026
4037
  if (!fillOption) {
4027
- if (isPropertyValueEmpty(value) && (fillOptions === null || fillOptions === void 0 ? void 0 : fillOptions.length)) {
4028
- setFillOption(fillOptions[0].value);
4038
+ if (isPropertyValueEmpty(value) && !property.isRequired && (property.defaultValue === null || property.value === null)) {
4039
+ setFillOption('null');
4029
4040
  } else if (_useExpression && isExpression(value, needRecursionCheck)) {
4030
4041
  setFillOption('expression');
4031
4042
  } else {
@@ -4034,7 +4045,7 @@ const usePropertyFiller = ({
4034
4045
  } else if (!isDirty) {
4035
4046
  checkFillOption();
4036
4047
  }
4037
- }, [value, fillOption, fillOptions, _useExpression, isDirty, checkFillOption, needRecursionCheck]);
4048
+ }, [value, fillOption, fillOptions, _useExpression, isDirty, checkFillOption, needRecursionCheck, _required, property]);
4038
4049
  return {
4039
4050
  propertyType,
4040
4051
  valueLabel,
@@ -4242,6 +4253,7 @@ const ControlPropertyFiller = ({
4242
4253
  entityReferenceValueComponent: entityReferenceValueComponent,
4243
4254
  fileReferenceValueComponent: fileReferenceValueComponent
4244
4255
  }), fillOption === 'dem_builder' && jsx(CreateDefinition, {
4256
+ control: control,
4245
4257
  title: "JSON",
4246
4258
  definitionFieldName: name,
4247
4259
  entityReferenceDefinitionSources: entityReferenceDefinitionSources,
@@ -5323,6 +5335,7 @@ const defaultPropertyValues = {
5323
5335
  uiSettings: ''
5324
5336
  };
5325
5337
  const CreatePropertiesListContext = ({
5338
+ control,
5326
5339
  parentFieldName,
5327
5340
  hideAddButton,
5328
5341
  focusOnIndex,
@@ -5337,9 +5350,9 @@ const CreatePropertiesListContext = ({
5337
5350
  }) => {
5338
5351
  var _a;
5339
5352
  const fieldName = useMemo(() => parentFieldName ? `${parentFieldName}.properties` : `properties`, [parentFieldName]);
5353
+ // const { } = useControler({ name: fieldName })
5340
5354
  const {
5341
5355
  clearErrors,
5342
- control,
5343
5356
  setValue
5344
5357
  } = useFormContext();
5345
5358
  const {
@@ -5453,6 +5466,7 @@ const CreatePropertiesListContext = ({
5453
5466
  };
5454
5467
 
5455
5468
  const CreateDefinition = /*#__PURE__*/forwardRef(({
5469
+ control: extControl,
5456
5470
  definitionFieldName,
5457
5471
  title,
5458
5472
  hideCodeField,
@@ -5490,7 +5504,7 @@ const CreateDefinition = /*#__PURE__*/forwardRef(({
5490
5504
  }, {
5491
5505
  children: jsx(ControlInput, {
5492
5506
  required: true,
5493
- control: control,
5507
+ control: extControl !== null && extControl !== void 0 ? extControl : control,
5494
5508
  name: definitionFieldName ? `${definitionFieldName}.name` : `name`,
5495
5509
  label: "Name"
5496
5510
  })
@@ -5499,7 +5513,7 @@ const CreateDefinition = /*#__PURE__*/forwardRef(({
5499
5513
  }, {
5500
5514
  children: jsx(ControlInput, {
5501
5515
  required: true,
5502
- control: control,
5516
+ control: extControl !== null && extControl !== void 0 ? extControl : control,
5503
5517
  name: definitionFieldName ? `${definitionFieldName}.code` : `code`,
5504
5518
  label: "Code"
5505
5519
  })
@@ -5508,7 +5522,7 @@ const CreateDefinition = /*#__PURE__*/forwardRef(({
5508
5522
  }, {
5509
5523
  children: jsx(ControlNumberInput, {
5510
5524
  required: true,
5511
- control: control,
5525
+ control: extControl !== null && extControl !== void 0 ? extControl : control,
5512
5526
  name: definitionFieldName ? `${definitionFieldName}.version` : `version`,
5513
5527
  label: "Version"
5514
5528
  })
@@ -5521,6 +5535,7 @@ const CreateDefinition = /*#__PURE__*/forwardRef(({
5521
5535
  entityReferenceValueComponent: entityReferenceValueComponent,
5522
5536
  fileReferenceValueComponent: fileReferenceValueComponent
5523
5537
  }) : jsx(CreatePropertiesListContext, {
5538
+ control: extControl,
5524
5539
  required: _required,
5525
5540
  parentFieldName: definitionFieldName,
5526
5541
  entityReferenceDefinitionSources: entityReferenceDefinitionSources,
@@ -5823,7 +5838,6 @@ const PropertyFiller = ({
5823
5838
  } = useController({
5824
5839
  name,
5825
5840
  control,
5826
- defaultValue: property.defaultValue,
5827
5841
  rules: {
5828
5842
  validate: val => {
5829
5843
  if (required && val === null) return 'Please, fill this field';
@@ -5831,7 +5845,11 @@ const PropertyFiller = ({
5831
5845
  }
5832
5846
  }
5833
5847
  });
5834
- const value = watch(name);
5848
+ // const value = watch(name);
5849
+ const value = useWatch({
5850
+ control,
5851
+ name
5852
+ });
5835
5853
  const prevValueRef = useRef({});
5836
5854
  const {
5837
5855
  propertyType,
@@ -5905,14 +5923,24 @@ const PropertyFiller = ({
5905
5923
  clearErrors();
5906
5924
  }, [name, defaultProperyValue, clearErrors, value, setFillOption, fillOption, propertyType, setValue, property]);
5907
5925
  useEffect(() => {
5908
- if (!isDirty && (property.defaultValue || property.value) && !value) {
5909
- setValue(name, property.defaultValue || property.value);
5926
+ if (!isDirty && (property.defaultValue || property.value) && isPropertyValueEmpty(value)) {
5927
+ setValue(name, property.defaultValue || property.value, {
5928
+ shouldDirty: true
5929
+ });
5910
5930
  }
5911
- }, [isDirty, property, name, setValue, value]);
5931
+ }, [isDirty, property, name, setValue, value, required]);
5912
5932
  useEffect(() => {
5913
5933
  var _a;
5914
- if (value === undefined || property.isRequired && value === null && !(property.defaultValue || property.value)) {
5915
- setValue(name, (_a = prevValueRef.current[fillOption]) !== null && _a !== void 0 ? _a : defaultProperyValue);
5934
+ if (
5935
+ // value === undefined ||
5936
+ property.isRequired && isPropertyValueEmpty(value) && (property.defaultValue === null || property.value === null)) {
5937
+ setValue(name, (_a = prevValueRef.current[fillOption]) !== null && _a !== void 0 ? _a : defaultProperyValue, {
5938
+ shouldDirty: true
5939
+ });
5940
+ } else if ((value === undefined || value === null) && !property.isRequired && (property.defaultValue === null || property.value === null)) {
5941
+ setValue(name, null, {
5942
+ shouldDirty: true
5943
+ });
5916
5944
  }
5917
5945
  }, [value, setValue, defaultProperyValue, property, name, fillOption]);
5918
5946
  return jsxs(Grid, Object.assign({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dartech/arsenal-ui",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "author": "DAR",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -1,7 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { type RefMethods } from '../../Property/UpsertProperty';
3
+ import { Control } from 'react-hook-form';
3
4
  import { EntityReferenceProperty } from '../../../interfaces';
4
5
  type Props = {
6
+ control?: Control<any>;
5
7
  definitionFieldName?: string;
6
8
  title?: string;
7
9
  hideCodeField?: boolean;
@@ -5,6 +5,7 @@ type Props = {
5
5
  control: Control<any>;
6
6
  label: ReactNode;
7
7
  required?: boolean;
8
+ defaultValue?: [] | null;
8
9
  };
9
- export declare const ControlArrayInput: ({ name, control, label, required }: Props) => JSX.Element;
10
+ export declare const ControlArrayInput: ({ name, control, label, required, defaultValue, }: Props) => JSX.Element;
10
11
  export {};
@@ -1,6 +1,8 @@
1
1
  /// <reference types="react" />
2
+ import { Control } from 'react-hook-form';
2
3
  import { CustomPropertyFieldProps, EntityReferenceProperty } from '../../../interfaces';
3
4
  type Props = {
5
+ control: Control<any>;
4
6
  parentFieldName?: string;
5
7
  hideAddButton?: boolean;
6
8
  focusOnIndex?: number;
@@ -24,5 +26,5 @@ type Props = {
24
26
  name: string;
25
27
  }>;
26
28
  };
27
- export declare const CreatePropertiesListContext: ({ parentFieldName, hideAddButton, focusOnIndex, required, entityDefinition, globalParameter, customPropertyFields, parentRemove, entityReferenceDefinitionSources, entityReferenceValueComponent, fileReferenceValueComponent, }: Props) => JSX.Element;
29
+ export declare const CreatePropertiesListContext: ({ control, parentFieldName, hideAddButton, focusOnIndex, required, entityDefinition, globalParameter, customPropertyFields, parentRemove, entityReferenceDefinitionSources, entityReferenceValueComponent, fileReferenceValueComponent, }: Props) => JSX.Element;
28
30
  export default CreatePropertiesListContext;