Skip to content

Commit

Permalink
Merge pull request #143 from irontec/CDL-109-autoselect-single-option…
Browse files Browse the repository at this point in the history
…-on-autocomplete

Fixed error when id equals zero
  • Loading branch information
danigargar authored Oct 21, 2024
2 parents f79f994 + 2c99e6a commit 477438f
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 477438f

Please sign in to comment.