Skip to content

Commit

Permalink
fix: fix import date field type
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 18, 2024
1 parent e922e27 commit 724c8b5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import * as Dialog from "$lib/components/ui/dialog"
import FieldOptions from "../field-options/field-options.svelte"
import FieldIcon from "../field-icon/field-icon.svelte"
import { ScrollArea } from "$lib/components/ui/scroll-area/index.js"
export let tableNames: string[]
Expand Down Expand Up @@ -83,6 +82,7 @@
if (!selectedFields.includes(field.id)) {
continue
}
const value = castFieldValue(field, r[j])
record.values[field.id!] = value
}
Expand All @@ -107,7 +107,12 @@
})
async function handleFile() {
if (!file) return
if (!file) {
data = undefined
schema = undefined
selectedFields = []
return
}
const parsed = await parse(file)
if (firstRowAsHeader) {
Expand Down Expand Up @@ -162,6 +167,7 @@
function removeFile() {
file = undefined
handleFile()
}
$: filteredSchema = (schema?.filter((field) => !!field.id && selectedFields.includes(field.id)) ??
Expand Down Expand Up @@ -261,7 +267,7 @@
</Table.Header>
<Table.Body class="w-full flex-1 overflow-y-auto">
{#each schema as field, idx}
<Table.Row class="group flex w-full">
<Table.Row data-field-id={field.id} class="group flex w-full">
<Table.Cell class="flex w-[40px] items-center justify-center font-medium">
{#if !!field.id}
<Checkbox
Expand Down Expand Up @@ -337,7 +343,7 @@
{/if}
<Button
disabled={(step === 0 && !file) ||
(step === 1 && schema.length < 1) ||
(step === 1 && (!schema || schema.length < 1)) ||
$createTable.isPending ||
$createRecords.isPending ||
selectedFields.length < 1}
Expand Down
3 changes: 2 additions & 1 deletion packages/table/src/modules/schema/fields/dto/field.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { autoIncrementFieldDTO } from "../variants/autoincrement-field"
import { checkboxFieldDTO, createCheckboxFieldDTO } from "../variants/checkbox-field"
import { createdAtFieldDTO } from "../variants/created-at-field"
import { createdByFieldDTO } from "../variants/created-by-field"
import { currencyFieldDTO } from "../variants/currency-field"
import { createCurrencyFieldDTO, currencyFieldDTO } from "../variants/currency-field"
import { createEmailFieldDTO, emailFieldDTO } from "../variants/email-field"
import { idFieldDTO } from "../variants/id-field/id-field.vo"
import { createLongTextFieldDTO, longTextFieldDTO } from "../variants/long-text-field"
Expand Down Expand Up @@ -63,6 +63,7 @@ export const inferCreateFieldDTO = z.discriminatedUnion("type", [
createStringFieldDTO.omit({ id: true, name: true }),
createNumberFieldDTO.omit({ id: true, name: true }),
createEmailFieldDTO.omit({ id: true, name: true }),
createCurrencyFieldDTO.omit({ id: true, name: true }),
createDateFieldDTO.omit({ id: true, name: true }),
createJsonFieldDTO.omit({ id: true, name: true }),
createCheckboxFieldDTO.omit({ id: true, name: true }),
Expand Down
11 changes: 11 additions & 0 deletions packages/table/src/modules/schema/fields/field.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getIsSystemFieldType,
getRollupFnByType,
inferCreateFieldType,
isCurrencyValue,
isDateValue,
isFieldSortable,
isJsonValue,
Expand Down Expand Up @@ -41,6 +42,16 @@ describe("field.util", () => {
})
})

describe("isCurrencyValue", () => {
it("should check if is currency value", () => {
expect(isCurrencyValue(1000)).toBe(true)
expect(isCurrencyValue("1,000.00")).toBe(true)
expect(isCurrencyValue("1,000")).toBe(true)
expect(isCurrencyValue("1000.50")).toBe(true)
expect(isCurrencyValue("not a currency")).toBe(false)
})
})

describe("isNumberValue", () => {
it("should return true for numbers", () => {
expect(isNumberValue(123)).toBe(true)
Expand Down
21 changes: 21 additions & 0 deletions packages/table/src/modules/schema/fields/field.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ export function isJsonValue(value: unknown): boolean {
return isObject(value)
}

export function isCurrencyValue(value: unknown): boolean {
if (typeof value !== "number" && typeof value !== "string") return false

const stringValue = value.toString()

// 检查是否包含逗号
if (stringValue.includes(",")) {
return /^-?\d{1,3}(,\d{3})*(\.\d{2})?$/.test(stringValue)
}

// 检查是否有一个小数点和两位小数
if (/^-?\d+\.\d{2}$/.test(stringValue)) {
return true
}

// 检查是否为整数
return /^-?\d+$/.test(stringValue)
}

export function isNumberValue(value: unknown): boolean {
return match(value)
.returnType<boolean>()
Expand All @@ -43,6 +62,7 @@ export const inferCreateFieldType = (values: (string | number | null | object |
.with(P.array(P.string.regex(EMAIL_REGEXP)), () => ({ type: "email" }))
.with(P.array(P.string.regex(URL_REGEXP)), () => ({ type: "url" }))
.with(P.array(P.boolean), () => ({ type: "checkbox" }))
.with(P.array(P.when(isCurrencyValue)), () => ({ type: "currency", option: { symbol: "$" } }))
.with(P.array(P.when(isNumberValue)), () => ({ type: "number" }))
.with(P.array(P.when(isDateValue)), () => ({ type: "date" }))
.with(P.array(P.when(isJsonValue)), () => ({ type: "json" }))
Expand Down Expand Up @@ -236,6 +256,7 @@ export const castFieldValue = (dto: ICreateSchemaDTO[0], value: string | number
.split(",")
.map((s) => s.trim())
})
.with({ type: "date" }, () => (isString(value) ? new Date(value).toISOString() : null))
.otherwise(() => value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CheckboxField extends AbstractField<CheckboxFieldValue> {
}

static create(dto: ICreateCheckboxFieldDTO) {
return new CheckboxField({ ...dto, id: FieldIdVo.create().value })
return new CheckboxField({ ...dto, id: FieldIdVo.fromStringOrCreate(dto.id).value })
}

override type = CHECKBOX_TYPE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class DateField extends AbstractField<DateFieldValue> {
}

static create(dto: ICreateDateFieldDTO) {
return new DateField({ ...dto, id: FieldIdVo.create().value })
return new DateField({ ...dto, id: FieldIdVo.fromStringOrCreate(dto.id).value })
}

get formatter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class UserField extends AbstractField<UserFieldValue> {
}

static create(dto: ICreateUserFieldDTO) {
return new UserField({ ...dto, id: FieldIdVo.create().value })
return new UserField({ ...dto, id: FieldIdVo.fromStringOrCreate(dto.id).value })
}

override type = USER_TYPE
Expand Down

0 comments on commit 724c8b5

Please sign in to comment.