From 0b150abae48c372f83a6abc35a141a16f5c9b87e Mon Sep 17 00:00:00 2001 From: ewandor Date: Sun, 2 Feb 2025 22:08:38 +0100 Subject: [PATCH] Adding empty value on foreign key --- gui/app/src/common/crud/widgets/foreign-key.tsx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gui/app/src/common/crud/widgets/foreign-key.tsx b/gui/app/src/common/crud/widgets/foreign-key.tsx index 1a6666f..d14b39d 100644 --- a/gui/app/src/common/crud/widgets/foreign-key.tsx +++ b/gui/app/src/common/crud/widgets/foreign-key.tsx @@ -13,6 +13,11 @@ export const ForeignKeyWidget = (props: WidgetProps) => { id: props.value != null ? props.value : undefined }); + const empty_option: BaseRecord = { + id: null + } + empty_option[labelField] = "(None)" + const [inputValue, setInputValue] = useState(""); const [selectedValue, setSelectedValue] = useState(valueResult.data?.data || null); const [debouncedInputValue, setDebouncedInputValue] = useState(inputValue); @@ -31,10 +36,6 @@ export const ForeignKeyWidget = (props: WidgetProps) => { const options = listResult.data?.data || []; if (! props.required) { - const empty_option: BaseRecord = { - id: null - } - empty_option[labelField] = "(None)" options.unshift(empty_option); } const isLoading = listResult.isLoading || valueResult.isLoading; @@ -47,7 +48,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => { { - setSelectedValue(newValue); + setSelectedValue(newValue ? newValue : empty_option); props.onChange(newValue ? newValue.id : null); return true; }}