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 17, 2024
1 parent f79f994 commit 907a9b2
Showing 1 changed file with 31 additions and 2 deletions.
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 907a9b2

Please sign in to comment.