Skip to content

Commit

Permalink
Fixed error when id equals zero
Browse files Browse the repository at this point in the history
Added support for __auto__ default value
  • Loading branch information
danigargar committed Oct 21, 2024
1 parent f79f994 commit 2c99e6a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@irontec/ivoz-ui",
"version": "1.6.1",
"version": "1.6.2",
"description": "UI library used in ivozprovider",
"license": "GPL-3.0",
"main": "index.js",
Expand Down
33 changes: 31 additions & 2 deletions library/src/services/form/Field/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const Autocomplete = (props: AutocompleteProps): JSX.Element | null => {
hasChanged,
} = props;

const value = props.value || null;
const value = props.value ?? null;

const i18n = getI18n();

let className = props.className;
Expand Down Expand Up @@ -183,12 +184,40 @@ const Autocomplete = (props: AutocompleteProps): JSX.Element | null => {
: true;
}

const autoDefaultValue = (value: unknown): unknown => {
if (value !== '__auto__') {
return value;
}

if (arrayChoices.length === 0) {
return value;
}

const realChoices = arrayChoices.filter((dac) => dac.id != '__null__');

if (realChoices.length != 1) {
return '__null__';
}

return realChoices[0].id;
};

let autocompleteValue;
if (multiple) {
autocompleteValue = arrayChoices.length ? value : [];
} else {
const autoValue = autoDefaultValue(value);
autocompleteValue =
arrayChoices?.find((item) => `${item.id}` === `${value}`) ?? null;
arrayChoices?.find((item) => `${item.id}` === `${autoValue}`) ?? null;

if (autoValue != value) {
onChange({
target: {
name: name,
value: autoValue,
},
});
}
}

return (
Expand Down

0 comments on commit 2c99e6a

Please sign in to comment.