diff --git a/library/package.json b/library/package.json index 6552ae7..6e27a8e 100644 --- a/library/package.json +++ b/library/package.json @@ -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", diff --git a/library/src/services/form/Field/Autocomplete/Autocomplete.tsx b/library/src/services/form/Field/Autocomplete/Autocomplete.tsx index 93bf7a2..dfb52ef 100644 --- a/library/src/services/form/Field/Autocomplete/Autocomplete.tsx +++ b/library/src/services/form/Field/Autocomplete/Autocomplete.tsx @@ -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; @@ -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 (