Skip to content

Commit 1ed1962

Browse files
committed
fix: don't allow to change state to empty in the afcl select in create/edit view.
If field is not requred - use option unset to clear the field, instead of just clicking on selected item twice https://web.tracklify.com/project/2b7ZVgE5/AdminForth/1118/lKCY0uOR/when-we-are-trying-to-clear-se
1 parent 237a19b commit 1ed1962

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

adminforth/spa/src/afcl/Select.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ const props = defineProps({
159159
type: String,
160160
default: '',
161161
},
162+
disableTogleOfSelectedItem: {
163+
type: Boolean,
164+
default: false,
165+
}
162166
});
163167
164168
const emit = defineEmits(['update:modelValue', 'scroll-near-end', 'search']);
@@ -314,7 +318,7 @@ const removeClickListener = () => {
314318
};
315319
316320
const toogleItem = (item: any) => {
317-
if (selectedItems.value.includes(item)) {
321+
if (selectedItems.value.includes(item) && !props.disableTogleOfSelectedItem) {
318322
selectedItems.value = selectedItems.value.filter(i => i.value !== item.value);
319323
} else {
320324
if (!props.multiple) {

adminforth/spa/src/components/ColumnValueInput.vue

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ref="input"
2121
:key="`select-${column.name}-${source}-${column.foreignResource?.name || column.foreignResource?.table || ''}`"
2222
class="w-full min-w-24"
23-
:options="columnOptions[column.name] || []"
23+
:options="formatSelectOptions(columnOptions[column.name] || [], column)"
2424
:searchDisabled="!column.foreignResource.searchableFields"
2525
@scroll-near-end="loadMoreOptions && loadMoreOptions(column.name)"
2626
@search="(searchTerm) => {
@@ -33,6 +33,7 @@
3333
:modelValue="value"
3434
:readonly="(column.editReadonly && source === 'edit') || readonly"
3535
@update:modelValue="$emit('update:modelValue', $event)"
36+
disableTogleOfSelectedItem
3637
>
3738
<template #extra-item v-if="columnLoadingState && columnLoadingState[column.name]?.loading">
3839
<div class="text-center text-gray-400 dark:text-gray-300 py-2 flex items-center justify-center gap-2">
@@ -45,11 +46,12 @@
4546
v-else-if="column.enum"
4647
ref="input"
4748
class="w-full min-w-24"
48-
:options="column.enum"
49+
:options="formatSelectOptions(column.enum, column)"
4950
teleportToBody
5051
:modelValue="value"
5152
:readonly="(column.editReadonly && source === 'edit') || readonly"
5253
@update:modelValue="$emit('update:modelValue', $event)"
54+
disableTogleOfSelectedItem
5355
/>
5456
<Select
5557
v-else-if="(type || column.type) === 'boolean'"
@@ -60,6 +62,7 @@
6062
:modelValue="value"
6163
:readonly="(column.editReadonly && source === 'edit') || readonly"
6264
@update:modelValue="$emit('update:modelValue', $event)"
65+
disableTogleOfSelectedItem
6366
/>
6467
<Input
6568
v-else-if="['integer'].includes(type || column.type)"
@@ -188,7 +191,7 @@
188191
type?: string,
189192
value: any,
190193
currentValues: any,
191-
mode: string,
194+
mode: 'create' | 'edit',
192195
columnOptions: any,
193196
unmasked: any,
194197
deletable?: boolean,
@@ -218,6 +221,14 @@ const input = ref<HTMLInputElement | null>(null);
218221
return options;
219222
};
220223
224+
const formatSelectOptions = (options: any, column: any) => {
225+
const optionsToReturn = options;
226+
if (!column.required[props.mode] && !column.isArray?.enabled) {
227+
optionsToReturn.push({ label: t('Unset'), value: null });
228+
}
229+
return optionsToReturn;
230+
};
231+
221232
function onFocusHandler(event:FocusEvent, column:any, source:string, ) {
222233
const focusedInput = event.target as HTMLInputElement;
223234
if(!focusedInput) return;

adminforth/spa/src/components/ColumnValueInputWrapper.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
source: 'create' | 'edit',
6969
column: any,
7070
currentValues: any,
71-
mode: string,
71+
mode: 'create' | 'edit',
7272
columnOptions: any,
7373
unmasked: any,
7474
setCurrentValue: Function,

adminforth/spa/src/components/GroupsTable.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
const props = defineProps<{
8181
source: 'create' | 'edit',
8282
group: any,
83-
mode: string,
83+
mode: 'create' | 'edit',
8484
validatingMode: boolean,
8585
currentValues: any,
8686
unmasked: any,

0 commit comments

Comments
 (0)