Adding empty value on foreign key

This commit is contained in:
2025-02-02 22:08:38 +01:00
parent 1e8731d78b
commit 0b150abae4

View File

@@ -13,6 +13,11 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
id: props.value != null ? props.value : undefined id: props.value != null ? props.value : undefined
}); });
const empty_option: BaseRecord = {
id: null
}
empty_option[labelField] = "(None)"
const [inputValue, setInputValue] = useState<string>(""); const [inputValue, setInputValue] = useState<string>("");
const [selectedValue, setSelectedValue] = useState(valueResult.data?.data || null); const [selectedValue, setSelectedValue] = useState(valueResult.data?.data || null);
const [debouncedInputValue, setDebouncedInputValue] = useState<string>(inputValue); const [debouncedInputValue, setDebouncedInputValue] = useState<string>(inputValue);
@@ -31,10 +36,6 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
const options = listResult.data?.data || []; const options = listResult.data?.data || [];
if (! props.required) { if (! props.required) {
const empty_option: BaseRecord = {
id: null
}
empty_option[labelField] = "(None)"
options.unshift(empty_option); options.unshift(empty_option);
} }
const isLoading = listResult.isLoading || valueResult.isLoading; const isLoading = listResult.isLoading || valueResult.isLoading;
@@ -47,7 +48,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
<Autocomplete <Autocomplete
value={selectedValue} value={selectedValue}
onChange={(event, newValue) => { onChange={(event, newValue) => {
setSelectedValue(newValue); setSelectedValue(newValue ? newValue : empty_option);
props.onChange(newValue ? newValue.id : null); props.onChange(newValue ? newValue.id : null);
return true; return true;
}} }}