Skip to content

Commit 46a9626

Browse files
committed
refactor: simplify value handling for integer, float, and decimal types across data connectors and components
1 parent ef7d9ce commit 46a9626

File tree

5 files changed

+30
-43
lines changed

5 files changed

+30
-43
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -224,62 +224,36 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
224224
// Int
225225
if (field.type === AdminForthDataTypes.INTEGER) {
226226
if (value === "" || value === null) return this.setFieldValue(field, null);
227-
if (!Number.isFinite(value) || Math.trunc(value) !== value) {
227+
if (!Number.isFinite(value)) {
228228
throw new Error(`Value is not an integer. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`);
229229
}
230-
return this.setFieldValue(field, Math.trunc(value));
230+
return this.setFieldValue(field, value);
231231
}
232232

233233
// Float
234234
if (field.type === AdminForthDataTypes.FLOAT) {
235235
if (value === "" || value === null) return this.setFieldValue(field, null);
236-
let number: any;
237-
if (typeof value === "number") {
238-
number = value;
239-
} else if (typeof value === "object") {
240-
number = (value as any).valueOf();
241-
} else {
242-
number = NaN;
243-
}
244236

245-
if (typeof number !== "number" || !Number.isFinite(number)) {
237+
if (typeof value !== "number" || !Number.isFinite(value)) {
246238
throw new Error(
247239
`Value is not a float. Field ${field.name} with type is ${field.type}, but got value: ${String(value)} with type ${typeof value}`
248240
);
249241
}
250242

251-
return this.setFieldValue(field, number);
243+
return this.setFieldValue(field, value);
252244
}
253245

254246
// Decimal
255247
if (field.type === AdminForthDataTypes.DECIMAL) {
256248
if (value === "" || value === null) return this.setFieldValue(field, null);
257-
258-
if (typeof value === "number") {
259-
if (!Number.isFinite(value)) {
260-
throw new Error(`Value is not a decimal. Field ${field.name} got: ${value} (number)`);
261-
}
262-
return this.setFieldValue(field, value);
263-
}
264-
265249
if (typeof value === "string") {
266250
const string = value.trim();
267251
if (!string) return this.setFieldValue(field, null);
268252
if (Number.isFinite(Number(string))) return this.setFieldValue(field, string);
269-
throw new Error(`Value is not a decimal. Field ${field.name} got: ${value} (string)`);
270-
}
271-
272-
if (typeof value === "object") {
273-
if (typeof value.toString !== "function") {
274-
throw new Error(`Decimal object has no toString(). Field ${field.name} got: ${String(value)}`);
275-
}
276-
const string = value.toString().trim();
277-
if (!string) return this.setFieldValue(field, null);
278-
if (Number.isFinite(Number(string))) return this.setFieldValue(field, string);
279-
throw new Error(`Value is not a decimal. Field ${field.name} got: ${string} (object->string)`);
253+
throw new Error(`Value is not a decimal. Field ${field.name} with type is ${field.type}, but got value: ${value} with type ${typeof value}`);
280254
}
281255

282-
throw new Error(`Value is not a decimal. Field ${field.name} got: ${String(value)} (${typeof value})`);
256+
throw new Error(`Value is not a decimal. Field ${field.name} with type is ${field.type}, but got value: ${String(value)} with type ${typeof value}`);
283257
}
284258

285259
// Date

adminforth/dataConnectors/mongo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,12 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
223223

224224
if (field.type === AdminForthDataTypes.FLOAT) {
225225
if (value === "" || value === null) return null;
226-
return Number.isFinite(value) ? new Double(value) : null;
226+
return Number.isFinite(value) ? value : null;
227227
}
228228

229229
if (field.type === AdminForthDataTypes.DECIMAL) {
230230
if (value === "" || value === null) return null;
231-
return Decimal128.fromString(value.toString());
231+
return value.toString();
232232
}
233233

234234
return value;

adminforth/spa/src/components/ColumnValueInput.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@
8686
:readonly="(column.editReadonly && source === 'edit') || readonly"
8787
/>
8888
<Input
89-
v-else-if="['decimal', 'float'].includes(type || column.type)"
89+
v-else-if="(type || column.type) === 'decimal'"
90+
ref="input"
91+
type="number"
92+
inputmode="decimal"
93+
class="w-40"
94+
placeholder="0.0"
95+
:fullWidth="true"
96+
:prefix="column.inputPrefix"
97+
:suffix="column.inputSuffix"
98+
:modelValue="String(value)"
99+
@update:modelValue="$emit('update:modelValue', String($event))"
100+
/>
101+
<Input
102+
v-else-if="(type || column.type) === 'float'"
90103
ref="input"
91104
type="number"
92105
step="0.1"

adminforth/spa/src/components/Filters.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,24 @@
123123
:min="getFilterMinValue(c.name)"
124124
:max="getFilterMaxValue(c.name)"
125125
:valueStart="getFilterItem({ column: c, operator: 'gte' })"
126-
@update:valueStart="onFilterInput[c.name]({ column: c, operator: 'gte', value: ($event !== '' && $event !== null) ? $event : undefined })"
126+
@update:valueStart="onFilterInput[c.name]({ column: c, operator: 'gte', value: ($event !== '' && $event !== null) ? (c.type === 'decimal' ? String($event) : $event) : undefined })"
127127
:valueEnd="getFilterItem({ column: c, operator: 'lte' })"
128-
@update:valueEnd="onFilterInput[c.name]({ column: c, operator: 'lte', value: ($event !== '' && $event !== null) ? $event : undefined })"
128+
@update:valueEnd="onFilterInput[c.name]({ column: c, operator: 'lte', value: ($event !== '' && $event !== null) ? (c.type === 'decimal' ? String($event) : $event) : undefined })"
129129
/>
130130

131131
<div v-else-if="['integer', 'decimal', 'float'].includes(c.type)" class="flex gap-2">
132132
<Input
133133
type="number"
134134
aria-describedby="helper-text-explanation"
135135
:placeholder="$t('From')"
136-
@update:modelValue="onFilterInput[c.name]({ column: c, operator: 'gte', value: ($event !== '' && $event !== null) ? $event : undefined })"
136+
@update:modelValue="onFilterInput[c.name]({ column: c, operator: 'gte', value: ($event !== '' && $event !== null) ? (c.type === 'decimal' ? String($event) : $event) : undefined })"
137137
:modelValue="getFilterItem({ column: c, operator: 'gte' })"
138138
/>
139139
<Input
140140
type="number"
141141
aria-describedby="helper-text-explanation"
142142
:placeholder="$t('To')"
143-
@update:modelValue="onFilterInput[c.name]({ column: c, operator: 'lte', value: ($event !== '' && $event !== null) ? $event : undefined })"
143+
@update:modelValue="onFilterInput[c.name]({ column: c, operator: 'lte', value: ($event !== '' && $event !== null) ? (c.type === 'decimal' ? String($event) : $event) : undefined })"
144144
:modelValue="getFilterItem({ column: c, operator: 'lte' })"
145145
/>
146146
</div>

adminforth/spa/src/components/ResourceForm.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ const setCurrentValue = (key: any, value: any, index = null) => {
206206
} else if (index === currentValues.value[key].length) {
207207
currentValues.value[key].push(null);
208208
} else {
209-
if (['integer', 'float', 'decimal'].includes(col.isArray.itemType)) {
209+
if (['integer', 'float'].includes(col.isArray.itemType)) {
210210
if (value || value === 0) {
211211
currentValues.value[key][index] = +value;
212212
} else {
@@ -215,12 +215,12 @@ const setCurrentValue = (key: any, value: any, index = null) => {
215215
} else {
216216
currentValues.value[key][index] = value;
217217
}
218-
if (col?.isArray && ['text', 'richtext', 'string'].includes(col.isArray.itemType) && col.enforceLowerCase) {
218+
if (col?.isArray && ['text', 'richtext', 'string', 'decimal'].includes(col.isArray.itemType) && col.enforceLowerCase) {
219219
currentValues.value[key][index] = currentValues.value[key][index].toLowerCase();
220220
}
221221
}
222222
} else {
223-
if (col?.type && ['integer', 'float', 'decimal'].includes(col.type)) {
223+
if (col?.type && ['integer', 'float'].includes(col.type)) {
224224
if (value || value === 0) {
225225
currentValues.value[key] = +value;
226226
} else {
@@ -229,7 +229,7 @@ const setCurrentValue = (key: any, value: any, index = null) => {
229229
} else {
230230
currentValues.value[key] = value;
231231
}
232-
if (col?.type && ['text', 'richtext', 'string'].includes(col?.type) && col.enforceLowerCase) {
232+
if (col?.type && ['text', 'richtext', 'string', 'decimal'].includes(col?.type) && col.enforceLowerCase) {
233233
currentValues.value[key] = currentValues.value[key].toLowerCase();
234234
}
235235
}

0 commit comments

Comments
 (0)