drf-react-by-schema 0.14.5 → 0.14.6

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.
@@ -51,17 +51,22 @@ const filter = (0, Autocomplete_1.createFilterOptions)();
51
51
  const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) => {
52
52
  var { control, schema, errors, setValue, getValues, fieldKey, labelKey = 'nome', index, name = 'name', optionsAC, optionsModel, getOptionLabel, renderOption, onEditModel, sx = { mr: 2 }, multiple = false, disabled = false } = _a, other = __rest(_a, ["control", "schema", "errors", "setValue", "getValues", "fieldKey", "labelKey", "index", "name", "optionsAC", "optionsModel", "getOptionLabel", "renderOption", "onEditModel", "sx", "multiple", "disabled"]);
53
53
  const { setDialog } = (0, APIWrapperContext_1.useAPIWrapper)();
54
+ const [options, setOptions] = (0, react_1.useState)(null);
54
55
  const model = name;
55
56
  const label = schema[model].label;
56
57
  if (!optionsModel) {
57
58
  optionsModel = model;
58
59
  }
59
- const options = optionsAC && optionsAC[optionsModel] ? optionsAC[optionsModel] : [];
60
60
  if (fieldKey && index && index >= 0) {
61
61
  name = `${fieldKey}.${index}.${name}`;
62
62
  }
63
63
  (0, react_1.useEffect)(() => {
64
- if (!getValues || !setValue || !options || options.length === 0) {
64
+ if (optionsAC && optionsModel && optionsAC[optionsModel]) {
65
+ setOptions(optionsAC[optionsModel]);
66
+ }
67
+ }, [optionsAC, optionsModel]);
68
+ (0, react_1.useEffect)(() => {
69
+ if (!getValues || !setValue || !options) {
65
70
  return;
66
71
  }
67
72
  const valuesInitial = getValues(model);
@@ -81,7 +86,7 @@ const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) =
81
86
  }
82
87
  const harmonizedValues = options.find((option) => option.id === valuesInitial.id);
83
88
  setValue(model, harmonizedValues);
84
- }, [options]);
89
+ }, [options, setValue, getValues, model, multiple]);
85
90
  const { error, helperText } = fieldKey && index && index >= 0
86
91
  ? (0, utils_1.errorProps)({
87
92
  fieldKey,
@@ -95,6 +100,9 @@ const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) =
95
100
  ? errors[name].message
96
101
  : schema[name].help_text || '',
97
102
  };
103
+ if (options === null) {
104
+ return react_1.default.createElement(react_1.default.Fragment, null);
105
+ }
98
106
  if (!disabled && !schema[model].disabled && onEditModel && optionsModel) {
99
107
  return (react_1.default.createElement(react_hook_form_1.Controller, { control: control, name: name, render: ({ field }) => (react_1.default.createElement(Autocomplete_1.default, Object.assign({ key: name }, field, { id: name, options: options, disabled: schema[model].disabled === true, autoHighlight: true, isOptionEqualToValue: (option, value) => option.id === value.id, fullWidth: true, multiple: multiple, sx: sx, onChange: (e, value) => {
100
108
  let valueAr = value;
@@ -149,12 +157,15 @@ const EditableAutocompleteFieldBySchema = react_1.default.forwardRef((_a, ref) =
149
157
  }
150
158
  } },
151
159
  react_1.default.createElement(Edit_1.default, null))),
152
- params.InputProps.endAdornment)) }) }, other))), freeSolo: true, filterOptions: (options, params) => {
153
- const filtered = filter(options, params);
160
+ params.InputProps.endAdornment)) }) }, other))), freeSolo: true, filterOptions: (filteredOptions, params) => {
161
+ if (filteredOptions.length === 0) {
162
+ return [];
163
+ }
164
+ const filtered = filter(filteredOptions, params);
154
165
  const { inputValue } = params;
155
166
  const inputValueSlug = (0, utils_1.slugify)(inputValue);
156
167
  // Suggest the creation of a new value
157
- const isExisting = options.some((option) => inputValueSlug === (0, utils_1.slugify)(option.label));
168
+ const isExisting = filteredOptions.some((option) => inputValueSlug === (0, utils_1.slugify)(option.label));
158
169
  if (inputValue !== '' && !isExisting) {
159
170
  filtered.push({
160
171
  inputValue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.14.5",
3
+ "version": "0.14.6",
4
4
  "description": "Components and Tools for building a React App having Django Rest Framework (DRF) as server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",