@pdg/react-form 1.0.18 → 1.0.20

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.
@@ -27,6 +27,7 @@ export interface FormAutocompleteProps extends CommonSxProps, Omit<FormValueItem
27
27
  disableClearable?: boolean;
28
28
  async?: boolean;
29
29
  onLoadItems?: (inputValue?: string) => Promise<FormAutocompleteItem[]>;
30
+ onAsyncLoadValueItem?: (value: FormAutocompleteValue) => Promise<FormAutocompleteComponentValue>;
30
31
  onRenderItem?: (item: FormAutocompleteItem) => ReactNode;
31
32
  onRenderTag?: (item: FormAutocompleteItem) => ReactNode;
32
33
  onValue?: (value: FormAutocompleteValue) => FormAutocompleteValue;
package/dist/index.esm.js CHANGED
@@ -7404,7 +7404,7 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7404
7404
  // ID --------------------------------------------------------------------------------------------------------------
7405
7405
  var initVariant = _a.variant, initSize = _a.size, initColor = _a.color, initFocused = _a.focused, initLabelShrink = _a.labelShrink, initFullWidth = _a.fullWidth,
7406
7406
  //----------------------------------------------------------------------------------------------------------------
7407
- name = _a.name, labelIcon = _a.labelIcon, label = _a.label, initLoading = _a.loading, initItems = _a.items, initValue = _a.value, initError = _a.error, initHelperText = _a.helperText, initDisabled = _a.disabled, readOnly = _a.readOnly, required = _a.required, exceptValue = _a.exceptValue, width = _a.width, placeholder = _a.placeholder, multiple = _a.multiple, formValueSeparator = _a.formValueSeparator, formValueSort = _a.formValueSort, disablePortal = _a.disablePortal, noOptionsText = _a.noOptionsText, loadingText = _a.loadingText, limitTags = _a.limitTags, openOnFocus = _a.openOnFocus, disableClearable = _a.disableClearable, async = _a.async, onLoadItems = _a.onLoadItems, onRenderItem = _a.onRenderItem, onRenderTag = _a.onRenderTag, onAddItem = _a.onAddItem,
7407
+ name = _a.name, labelIcon = _a.labelIcon, label = _a.label, initLoading = _a.loading, initItems = _a.items, initValue = _a.value, initError = _a.error, initHelperText = _a.helperText, initDisabled = _a.disabled, readOnly = _a.readOnly, required = _a.required, exceptValue = _a.exceptValue, width = _a.width, placeholder = _a.placeholder, multiple = _a.multiple, formValueSeparator = _a.formValueSeparator, formValueSort = _a.formValueSort, disablePortal = _a.disablePortal, noOptionsText = _a.noOptionsText, loadingText = _a.loadingText, limitTags = _a.limitTags, openOnFocus = _a.openOnFocus, disableClearable = _a.disableClearable, async = _a.async, onLoadItems = _a.onLoadItems, onAsyncLoadValueItem = _a.onAsyncLoadValueItem, onRenderItem = _a.onRenderItem, onRenderTag = _a.onRenderTag, onAddItem = _a.onAddItem,
7408
7408
  //----------------------------------------------------------------------------------------------------------------
7409
7409
  onChange = _a.onChange, onValue = _a.onValue, onValidate = _a.onValidate,
7410
7410
  //----------------------------------------------------------------------------------------------------------------
@@ -7413,6 +7413,7 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7413
7413
  // Ref -------------------------------------------------------------------------------------------------------------
7414
7414
  var textFieldRef = useRef(null);
7415
7415
  var asyncTimerRef = useRef(null);
7416
+ var oldComponentValueRef = useRef(null);
7416
7417
  // FormState -------------------------------------------------------------------------------------------------------
7417
7418
  var _b = useFormState(), formVariant = _b.variant, formSize = _b.size, formColor = _b.color, formFocused = _b.focused, formLabelShrink = _b.labelShrink, formFullWidth = _b.fullWidth, onAddValueItem = _b.onAddValueItem, onRemoveValueItem = _b.onRemoveValueItem, onValueChange = _b.onValueChange, onValueChangeByUser = _b.onValueChangeByUser, onRequestSearchSubmit = _b.onRequestSearchSubmit;
7418
7419
  // Memo - FormState ------------------------------------------------------------------------------------------------
@@ -7500,6 +7501,7 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7500
7501
  }, [multiple, formValueSeparator, itemsValues, onValue]);
7501
7502
  // State - value ---------------------------------------------------------------------------------------------------
7502
7503
  var _k = useAutoUpdateLayoutState(initValue, getFinalValue), value = _k[0], setValue = _k[1];
7504
+ var _l = useState(null), valueItem = _l[0], setValueItem = _l[1];
7503
7505
  var componentValue = useMemo(function () {
7504
7506
  var finalValue = value;
7505
7507
  if (finalValue != null) {
@@ -7517,18 +7519,67 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7517
7519
  else {
7518
7520
  finalValue = multiple ? [] : undefined;
7519
7521
  }
7520
- if (notEmpty(finalValue) && items) {
7521
- if (Array.isArray(finalValue)) {
7522
- return items.filter(function (info) { return Array.isArray(finalValue) && finalValue.includes(info.value); });
7522
+ var newComponentValue = multiple ? [] : null;
7523
+ if (notEmpty(finalValue)) {
7524
+ if (items) {
7525
+ if (Array.isArray(finalValue)) {
7526
+ newComponentValue = items.filter(function (info) { return Array.isArray(finalValue) && finalValue.includes(info.value); });
7527
+ }
7528
+ else {
7529
+ newComponentValue = items.find(function (info) { return info.value === value; }) || (multiple ? [] : null);
7530
+ }
7523
7531
  }
7524
- else {
7525
- return items.find(function (info) { return info.value === value; }) || (multiple ? [] : null);
7532
+ if (empty$1(newComponentValue) && valueItem) {
7533
+ if (Array.isArray(finalValue)) {
7534
+ if (Array.isArray(valueItem)) {
7535
+ newComponentValue = valueItem.filter(function (info) { return Array.isArray(finalValue) && finalValue.includes(info.value); });
7536
+ }
7537
+ }
7538
+ else {
7539
+ if (!Array.isArray(valueItem) && finalValue === valueItem.value) {
7540
+ newComponentValue = valueItem;
7541
+ }
7542
+ }
7526
7543
  }
7527
7544
  }
7545
+ if ((oldComponentValueRef.current, isSame$2(oldComponentValueRef.current, newComponentValue))) {
7546
+ return oldComponentValueRef.current;
7547
+ }
7528
7548
  else {
7529
- return multiple ? [] : null;
7549
+ oldComponentValueRef.current = newComponentValue;
7550
+ return newComponentValue;
7530
7551
  }
7531
- }, [value, multiple, items]);
7552
+ }, [value, multiple, items, valueItem]);
7553
+ useEffect(function () {
7554
+ if (async && onAsyncLoadValueItem) {
7555
+ if (value != null) {
7556
+ if (!valueItem) {
7557
+ onAsyncLoadValueItem(value).then(function (valueItem) {
7558
+ setValueItem(valueItem);
7559
+ if (valueItem) {
7560
+ if (Array.isArray(valueItem)) {
7561
+ setItems(valueItem);
7562
+ }
7563
+ else {
7564
+ setItems([valueItem]);
7565
+ }
7566
+ }
7567
+ });
7568
+ }
7569
+ }
7570
+ else {
7571
+ setValueItem(null);
7572
+ }
7573
+ }
7574
+ // eslint-disable-next-line react-hooks/exhaustive-deps
7575
+ }, [async, value, valueItem]);
7576
+ // Function ----------------------------------------------------------------------------------------------------------
7577
+ var showOnGetItemLoading = useCallback(function () {
7578
+ setIsOnGetItemLoading(true);
7579
+ }, []);
7580
+ var hideOnGetItemLoading = useCallback(function () {
7581
+ setIsOnGetItemLoading(false);
7582
+ }, []);
7532
7583
  // Effect ----------------------------------------------------------------------------------------------------------
7533
7584
  useEffect(function () {
7534
7585
  if (value !== initValue) {
@@ -7537,12 +7588,18 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7537
7588
  onValueChange(name, value);
7538
7589
  }
7539
7590
  if (!async && onLoadItems) {
7540
- setIsOnGetItemLoading(true);
7591
+ showOnGetItemLoading();
7541
7592
  onLoadItems().then(function (items) {
7542
7593
  setItems(items);
7543
- setIsOnGetItemLoading(false);
7594
+ hideOnGetItemLoading();
7544
7595
  });
7545
7596
  }
7597
+ return function () {
7598
+ if (asyncTimerRef.current) {
7599
+ clearTimeout(asyncTimerRef.current);
7600
+ asyncTimerRef.current = null;
7601
+ }
7602
+ };
7546
7603
  // eslint-disable-next-line react-hooks/exhaustive-deps
7547
7604
  }, []);
7548
7605
  useFirstSkipEffect$1(function () {
@@ -7553,19 +7610,50 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7553
7610
  onValueChange(name, value);
7554
7611
  }, [value]);
7555
7612
  useEffect(function () {
7556
- if (asyncTimerRef.current) {
7557
- clearTimeout(asyncTimerRef.current);
7558
- asyncTimerRef.current = null;
7559
- }
7560
- if (async && onLoadItems && inputValue != null) {
7561
- asyncTimerRef.current = setTimeout(function () {
7613
+ if (async && onLoadItems) {
7614
+ if (asyncTimerRef.current) {
7615
+ clearTimeout(asyncTimerRef.current);
7562
7616
  asyncTimerRef.current = null;
7563
- setIsOnGetItemLoading(true);
7564
- onLoadItems(inputValue).then(function (items) {
7565
- setItems(items);
7566
- setIsOnGetItemLoading(false);
7567
- });
7568
- }, 300);
7617
+ }
7618
+ if (inputValue != null) {
7619
+ showOnGetItemLoading();
7620
+ asyncTimerRef.current = setTimeout(function () {
7621
+ asyncTimerRef.current = null;
7622
+ onLoadItems(inputValue)
7623
+ .then(function (items) {
7624
+ if (componentValue) {
7625
+ if (Array.isArray(componentValue)) {
7626
+ var exceptValues_1 = componentValue.map(function (info) { return info.value; });
7627
+ setItems(__spreadArray(__spreadArray([], componentValue, true), items.filter(function (info) { return !exceptValues_1.includes(info.value); }), true));
7628
+ }
7629
+ else {
7630
+ var exceptValue_1 = componentValue.value;
7631
+ setItems(__spreadArray([componentValue], items.filter(function (info) { return info.value !== exceptValue_1; }), true));
7632
+ }
7633
+ }
7634
+ else {
7635
+ setItems(items);
7636
+ }
7637
+ })
7638
+ .finally(function () {
7639
+ hideOnGetItemLoading();
7640
+ });
7641
+ }, 300);
7642
+ }
7643
+ else {
7644
+ if (Array.isArray(componentValue)) {
7645
+ setItems(componentValue);
7646
+ }
7647
+ else {
7648
+ if (componentValue) {
7649
+ setItems([componentValue]);
7650
+ }
7651
+ else {
7652
+ setItems([]);
7653
+ }
7654
+ }
7655
+ hideOnGetItemLoading();
7656
+ }
7569
7657
  }
7570
7658
  // eslint-disable-next-line react-hooks/exhaustive-deps
7571
7659
  }, [async, inputValue]);
@@ -7706,6 +7794,7 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7706
7794
  var finalValue = getFinalValue(newValue);
7707
7795
  if (!isSame$2(value, finalValue)) {
7708
7796
  setValue(finalValue);
7797
+ setValueItem(componentValue);
7709
7798
  nextTick(function () {
7710
7799
  onValueChangeByUser(name, finalValue);
7711
7800
  onRequestSearchSubmit(name, finalValue);
@@ -7738,6 +7827,9 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
7738
7827
  if (reason === 'input') {
7739
7828
  setInputValue(newInputValue);
7740
7829
  }
7830
+ else if (reason === 'reset') {
7831
+ setInputValue(undefined);
7832
+ }
7741
7833
  }, renderTags: function (value, getTagProps) {
7742
7834
  return value.map(function (option, index) { return (React__default.createElement(Chip, __assign$4({ size: 'small', label: onRenderTag ? onRenderTag(option) : option.label }, getTagProps({ index: index })))); });
7743
7835
  }, renderInput: function (params) { return (React__default.createElement(FormTextField, __assign$4({}, params, { ref: textFieldRef, name: name, variant: variant, size: size, color: color, labelIcon: labelIcon, label: label, labelShrink: labelShrink, required: required, focused: focused, error: error, helperText: helperText, placeholder: placeholder, noFormValueItem: true, InputProps: __assign$4(__assign$4({}, params.InputProps), { endAdornment: (React__default.createElement(React__default.Fragment, null,