Skip to content

Commit 724c8b5

Browse files
committed
fix: fix import date field type
1 parent e922e27 commit 724c8b5

File tree

7 files changed

+47
-8
lines changed

7 files changed

+47
-8
lines changed

apps/frontend/src/lib/components/blocks/import-table/import-table.svelte

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import * as Dialog from "$lib/components/ui/dialog"
3636
import FieldOptions from "../field-options/field-options.svelte"
3737
import FieldIcon from "../field-icon/field-icon.svelte"
38-
import { ScrollArea } from "$lib/components/ui/scroll-area/index.js"
3938
4039
export let tableNames: string[]
4140
@@ -83,6 +82,7 @@
8382
if (!selectedFields.includes(field.id)) {
8483
continue
8584
}
85+
8686
const value = castFieldValue(field, r[j])
8787
record.values[field.id!] = value
8888
}
@@ -107,7 +107,12 @@
107107
})
108108
109109
async function handleFile() {
110-
if (!file) return
110+
if (!file) {
111+
data = undefined
112+
schema = undefined
113+
selectedFields = []
114+
return
115+
}
111116
112117
const parsed = await parse(file)
113118
if (firstRowAsHeader) {
@@ -162,6 +167,7 @@
162167
163168
function removeFile() {
164169
file = undefined
170+
handleFile()
165171
}
166172
167173
$: filteredSchema = (schema?.filter((field) => !!field.id && selectedFields.includes(field.id)) ??
@@ -261,7 +267,7 @@
261267
</Table.Header>
262268
<Table.Body class="w-full flex-1 overflow-y-auto">
263269
{#each schema as field, idx}
264-
<Table.Row class="group flex w-full">
270+
<Table.Row data-field-id={field.id} class="group flex w-full">
265271
<Table.Cell class="flex w-[40px] items-center justify-center font-medium">
266272
{#if !!field.id}
267273
<Checkbox
@@ -337,7 +343,7 @@
337343
{/if}
338344
<Button
339345
disabled={(step === 0 && !file) ||
340-
(step === 1 && schema.length < 1) ||
346+
(step === 1 && (!schema || schema.length < 1)) ||
341347
$createTable.isPending ||
342348
$createRecords.isPending ||
343349
selectedFields.length < 1}

packages/table/src/modules/schema/fields/dto/field.dto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { autoIncrementFieldDTO } from "../variants/autoincrement-field"
2020
import { checkboxFieldDTO, createCheckboxFieldDTO } from "../variants/checkbox-field"
2121
import { createdAtFieldDTO } from "../variants/created-at-field"
2222
import { createdByFieldDTO } from "../variants/created-by-field"
23-
import { currencyFieldDTO } from "../variants/currency-field"
23+
import { createCurrencyFieldDTO, currencyFieldDTO } from "../variants/currency-field"
2424
import { createEmailFieldDTO, emailFieldDTO } from "../variants/email-field"
2525
import { idFieldDTO } from "../variants/id-field/id-field.vo"
2626
import { createLongTextFieldDTO, longTextFieldDTO } from "../variants/long-text-field"
@@ -63,6 +63,7 @@ export const inferCreateFieldDTO = z.discriminatedUnion("type", [
6363
createStringFieldDTO.omit({ id: true, name: true }),
6464
createNumberFieldDTO.omit({ id: true, name: true }),
6565
createEmailFieldDTO.omit({ id: true, name: true }),
66+
createCurrencyFieldDTO.omit({ id: true, name: true }),
6667
createDateFieldDTO.omit({ id: true, name: true }),
6768
createJsonFieldDTO.omit({ id: true, name: true }),
6869
createCheckboxFieldDTO.omit({ id: true, name: true }),

packages/table/src/modules/schema/fields/field.util.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getIsSystemFieldType,
1010
getRollupFnByType,
1111
inferCreateFieldType,
12+
isCurrencyValue,
1213
isDateValue,
1314
isFieldSortable,
1415
isJsonValue,
@@ -41,6 +42,16 @@ describe("field.util", () => {
4142
})
4243
})
4344

45+
describe("isCurrencyValue", () => {
46+
it("should check if is currency value", () => {
47+
expect(isCurrencyValue(1000)).toBe(true)
48+
expect(isCurrencyValue("1,000.00")).toBe(true)
49+
expect(isCurrencyValue("1,000")).toBe(true)
50+
expect(isCurrencyValue("1000.50")).toBe(true)
51+
expect(isCurrencyValue("not a currency")).toBe(false)
52+
})
53+
})
54+
4455
describe("isNumberValue", () => {
4556
it("should return true for numbers", () => {
4657
expect(isNumberValue(123)).toBe(true)

packages/table/src/modules/schema/fields/field.util.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ export function isJsonValue(value: unknown): boolean {
2121
return isObject(value)
2222
}
2323

24+
export function isCurrencyValue(value: unknown): boolean {
25+
if (typeof value !== "number" && typeof value !== "string") return false
26+
27+
const stringValue = value.toString()
28+
29+
// 检查是否包含逗号
30+
if (stringValue.includes(",")) {
31+
return /^-?\d{1,3}(,\d{3})*(\.\d{2})?$/.test(stringValue)
32+
}
33+
34+
// 检查是否有一个小数点和两位小数
35+
if (/^-?\d+\.\d{2}$/.test(stringValue)) {
36+
return true
37+
}
38+
39+
// 检查是否为整数
40+
return /^-?\d+$/.test(stringValue)
41+
}
42+
2443
export function isNumberValue(value: unknown): boolean {
2544
return match(value)
2645
.returnType<boolean>()
@@ -43,6 +62,7 @@ export const inferCreateFieldType = (values: (string | number | null | object |
4362
.with(P.array(P.string.regex(EMAIL_REGEXP)), () => ({ type: "email" }))
4463
.with(P.array(P.string.regex(URL_REGEXP)), () => ({ type: "url" }))
4564
.with(P.array(P.boolean), () => ({ type: "checkbox" }))
65+
.with(P.array(P.when(isCurrencyValue)), () => ({ type: "currency", option: { symbol: "$" } }))
4666
.with(P.array(P.when(isNumberValue)), () => ({ type: "number" }))
4767
.with(P.array(P.when(isDateValue)), () => ({ type: "date" }))
4868
.with(P.array(P.when(isJsonValue)), () => ({ type: "json" }))
@@ -236,6 +256,7 @@ export const castFieldValue = (dto: ICreateSchemaDTO[0], value: string | number
236256
.split(",")
237257
.map((s) => s.trim())
238258
})
259+
.with({ type: "date" }, () => (isString(value) ? new Date(value).toISOString() : null))
239260
.otherwise(() => value)
240261
}
241262

packages/table/src/modules/schema/fields/variants/checkbox-field/checkbox-field.vo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class CheckboxField extends AbstractField<CheckboxFieldValue> {
4343
}
4444

4545
static create(dto: ICreateCheckboxFieldDTO) {
46-
return new CheckboxField({ ...dto, id: FieldIdVo.create().value })
46+
return new CheckboxField({ ...dto, id: FieldIdVo.fromStringOrCreate(dto.id).value })
4747
}
4848

4949
override type = CHECKBOX_TYPE

packages/table/src/modules/schema/fields/variants/date-field/date-field.vo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class DateField extends AbstractField<DateFieldValue> {
4545
}
4646

4747
static create(dto: ICreateDateFieldDTO) {
48-
return new DateField({ ...dto, id: FieldIdVo.create().value })
48+
return new DateField({ ...dto, id: FieldIdVo.fromStringOrCreate(dto.id).value })
4949
}
5050

5151
get formatter() {

packages/table/src/modules/schema/fields/variants/user-field/user-field.vo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class UserField extends AbstractField<UserFieldValue> {
4848
}
4949

5050
static create(dto: ICreateUserFieldDTO) {
51-
return new UserField({ ...dto, id: FieldIdVo.create().value })
51+
return new UserField({ ...dto, id: FieldIdVo.fromStringOrCreate(dto.id).value })
5252
}
5353

5454
override type = USER_TYPE

0 commit comments

Comments
 (0)