Skip to content

Commit 54baff7

Browse files
committed
feat: enhance enum suggestion control with array normalization and model value binding
- Added a new function to normalize array control data, ensuring consistent handling of various data types. - Updated the model value binding in the enum-and-suggestion component to use the computed model value from the control. - Improved the onChange logic to handle both array and non-array inputs effectively, enhancing the overall functionality of the enum suggestion control.
1 parent de54da4 commit 54baff7

3 files changed

Lines changed: 99 additions & 7 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Logger } from '@nestjs/common';
2+
import { InjectConnection } from '@nestjs/mongoose';
3+
import { Connection } from 'mongoose';
4+
5+
export default class DepartmentNumber1781081453 {
6+
private readonly logger = new Logger(DepartmentNumber1781081453.name);
7+
8+
public constructor(@InjectConnection() private mongo: Connection) {}
9+
10+
public async up(): Promise<void> {
11+
this.logger.log('DepartmentNumber1781081453 up started');
12+
13+
await this._migrateDepartmentNumberToArray();
14+
}
15+
16+
private async _migrateDepartmentNumberToArray(): Promise<void> {
17+
const identities = await this.mongo.collection('identities').find();
18+
19+
for await (const identity of identities) {
20+
const departmentNumber = identity.inetOrgPerson?.departmentNumber;
21+
22+
if (typeof departmentNumber === 'string') {
23+
this.logger.log(`Migrating departmentNumber for identity ${identity._id}`);
24+
await this.mongo
25+
.collection('identities')
26+
.updateOne({ _id: identity._id }, { $set: { 'inetOrgPerson.departmentNumber': [departmentNumber] } });
27+
} else if (Array.isArray(departmentNumber)) {
28+
if (typeof departmentNumber[0] === 'number') {
29+
this.logger.log(`Migrating departmentNumber for identity ${identity._id}`);
30+
await this.mongo
31+
.collection('identities')
32+
.updateOne(
33+
{ _id: identity._id },
34+
{ $set: { 'inetOrgPerson.departmentNumber': departmentNumber.map(String) } },
35+
);
36+
}
37+
}
38+
}
39+
40+
this.logger.log('Migration terminée avec succès');
41+
}
42+
}

apps/web/src/jsonforms/composables/controls/useEnumSuggestionControl.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, ref } from 'vue'
1+
import { computed, ref, watch } from 'vue'
22
import { first, get, isArray, isEmpty, isNumber, isObject, isString } from 'radash'
33
import { useNuxtApp } from '#imports'
44
import { determineClearValue, useQuasarControl } from '../../utils'
@@ -30,6 +30,22 @@ export const createEnumAdaptTarget = (clearValue: unknown) => {
3030
return (value: unknown) => (isBlankValue(value) ? clearValue : value)
3131
}
3232

33+
export const normalizeArrayControlData = (data: unknown): unknown[] | null => {
34+
if (data === null || data === undefined) {
35+
return null
36+
}
37+
38+
if (Array.isArray(data)) {
39+
return data
40+
}
41+
42+
if (typeof data === 'string' && data.trim().length === 0) {
43+
return []
44+
}
45+
46+
return [data]
47+
}
48+
3349
export const isArraySchemaControl = (control: JsonFormsEnumControl['control']['value']) => {
3450
const schemaType = control.schema?.type
3551

@@ -93,6 +109,40 @@ export const useEnumSuggestionControl = ({
93109

94110
const isArrayControl = computed(() => isArraySchemaControl(control.control.value))
95111

112+
const modelValue = computed(() => {
113+
const data = control.control.value.data
114+
115+
if (!isArrayControl.value) {
116+
return data ?? null
117+
}
118+
119+
return normalizeArrayControlData(data) ?? null
120+
})
121+
122+
watch(
123+
() => control.control.value.data,
124+
(data) => {
125+
if (!isArrayControl.value) {
126+
return
127+
}
128+
129+
if (data !== null && data !== undefined && !Array.isArray(data)) {
130+
control.onChange(adaptTarget(normalizeArrayControlData(data) ?? []))
131+
}
132+
},
133+
{ immediate: true },
134+
)
135+
136+
const onChange = (value: unknown) => {
137+
if (isArrayControl.value) {
138+
const normalized = Array.isArray(value) ? value : normalizeArrayControlData(value) ?? []
139+
control.onChange(adaptTarget(normalized))
140+
return
141+
}
142+
143+
control.onChange(adaptTarget(value))
144+
}
145+
96146
const optionValueKey = computed(() =>
97147
resolveOptionKey(control.appliedOptions.value?.optionValue, 'value'),
98148
)
@@ -209,24 +259,24 @@ export const useEnumSuggestionControl = ({
209259

210260
const createValue = (newValue: unknown, done: () => void) => {
211261
if (isArrayControl.value) {
212-
const currentValue = Array.isArray(control.control.value.data)
213-
? control.control.value.data
214-
: []
262+
const currentValue = normalizeArrayControlData(control.control.value.data) ?? []
215263

216264
if (newValue != null && !currentValue.includes(newValue)) {
217265
const updatedValue = [...currentValue, newValue]
218-
control.onChange(adaptTarget(updatedValue))
266+
onChange(updatedValue)
219267
}
220268
} else {
221-
control.onChange(adaptTarget(newValue))
269+
onChange(newValue)
222270
}
223271
done()
224272
}
225273

226274
return {
227275
...control,
228276
adaptTarget,
277+
onChange,
229278
createValue,
279+
modelValue,
230280
isArrayControl,
231281
optionValueKey,
232282
optionLabelKey,

apps/web/src/jsonforms/controls/enum-and-suggestion.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@blur="isFocused = false"
1313
@new-value="createValue"
1414
:id="control.id"
15-
:model-value="control.data || null"
15+
:model-value="modelValue"
1616
:label="computedLabel"
1717
:class="styles.control.input"
1818
clear-icon="mdi-close"

0 commit comments

Comments
 (0)