Skip to content

Commit

Permalink
Merge pull request #141 from irontec/CDL-109-combo-box-auto-default-v…
Browse files Browse the repository at this point in the history
…alue

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

Please sign in to comment.