@pdg/react-form 1.0.19 → 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,60 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
|
|
|
7517
7519
|
else {
|
|
7518
7520
|
finalValue = multiple ? [] : undefined;
|
|
7519
7521
|
}
|
|
7520
|
-
|
|
7521
|
-
|
|
7522
|
-
|
|
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
|
-
|
|
7525
|
-
|
|
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
|
-
|
|
7549
|
+
oldComponentValueRef.current = newComponentValue;
|
|
7550
|
+
return newComponentValue;
|
|
7551
|
+
}
|
|
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
|
+
}
|
|
7530
7573
|
}
|
|
7531
|
-
|
|
7574
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7575
|
+
}, [async, value, valueItem]);
|
|
7532
7576
|
// Function ----------------------------------------------------------------------------------------------------------
|
|
7533
7577
|
var showOnGetItemLoading = useCallback(function () {
|
|
7534
7578
|
setIsOnGetItemLoading(true);
|
|
@@ -7566,22 +7610,50 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
|
|
|
7566
7610
|
onValueChange(name, value);
|
|
7567
7611
|
}, [value]);
|
|
7568
7612
|
useEffect(function () {
|
|
7569
|
-
if (async && onLoadItems
|
|
7613
|
+
if (async && onLoadItems) {
|
|
7570
7614
|
if (asyncTimerRef.current) {
|
|
7571
7615
|
clearTimeout(asyncTimerRef.current);
|
|
7572
7616
|
asyncTimerRef.current = null;
|
|
7573
7617
|
}
|
|
7574
|
-
|
|
7575
|
-
|
|
7576
|
-
asyncTimerRef.current =
|
|
7577
|
-
|
|
7578
|
-
|
|
7579
|
-
|
|
7580
|
-
|
|
7581
|
-
|
|
7582
|
-
|
|
7583
|
-
|
|
7584
|
-
|
|
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
|
+
}
|
|
7585
7657
|
}
|
|
7586
7658
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7587
7659
|
}, [async, inputValue]);
|
|
@@ -7722,6 +7794,7 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
|
|
|
7722
7794
|
var finalValue = getFinalValue(newValue);
|
|
7723
7795
|
if (!isSame$2(value, finalValue)) {
|
|
7724
7796
|
setValue(finalValue);
|
|
7797
|
+
setValueItem(componentValue);
|
|
7725
7798
|
nextTick(function () {
|
|
7726
7799
|
onValueChangeByUser(name, finalValue);
|
|
7727
7800
|
onRequestSearchSubmit(name, finalValue);
|
|
@@ -7754,6 +7827,9 @@ FormTextEditor.defaultProps = FormTextEditorDefaultProps;var FormAutocompleteDef
|
|
|
7754
7827
|
if (reason === 'input') {
|
|
7755
7828
|
setInputValue(newInputValue);
|
|
7756
7829
|
}
|
|
7830
|
+
else if (reason === 'reset') {
|
|
7831
|
+
setInputValue(undefined);
|
|
7832
|
+
}
|
|
7757
7833
|
}, renderTags: function (value, getTagProps) {
|
|
7758
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 })))); });
|
|
7759
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,
|