Skip to content

Commit

Permalink
fix: handle cellvalue & select field choices & text field trim (#1038)
Browse files Browse the repository at this point in the history
* fix: handle cellvalue & select field choices & text field trim

* fix: e2e expect

* fix: convert select field, choice trim
  • Loading branch information
boris-w authored Oct 29, 2024
1 parent b4edf84 commit bb7abb6
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class TypeCastAndValidate {
throw new BadRequestException(fromZodError(validate.error).message);
}
}
if (this.field.type === FieldType.SingleLineText) {
if (this.field.type === FieldType.SingleLineText || this.field.type === FieldType.LongText) {
return this.field.convertStringToCellValue(validate.data as string);
}
return validate.data == null ? null : validate.data;
Expand Down
6 changes: 3 additions & 3 deletions apps/nestjs-backend/test/field-converting.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ describe('OpenAPI Freely perform column transformations (e2e)', () => {
{ name: 'x', color: Colors.Blue },
{ name: 'y', color: Colors.Red },
{ name: "','" },
{ name: ', ' },
{ name: ',' },
{ name: 'z' },
],
},
Expand All @@ -972,8 +972,8 @@ describe('OpenAPI Freely perform column transformations (e2e)', () => {
expect(values[1]).toEqual(['x', 'y']);
expect(values[2]).toEqual(['x', 'z']);
expect(values[3]).toEqual(['x', "','"]);
expect(values[4]).toEqual(['x', 'y', ', ']);
expect(values[5]).toEqual(["','", ', ']);
expect(values[4]).toEqual(['x', 'y', ',']);
expect(values[5]).toEqual(["','", ',']);
});

it('should convert long text to attachment', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { FieldCore } from '../../field';

export const selectFieldChoiceSchema = z.object({
id: z.string(),
name: z.string().min(1),
name: z
.string()
.transform((s) => s.trim())
.pipe(z.string().min(1)),
color: z.nativeEnum(Colors),
});

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/models/field/derivate/long-text.field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { CellValueType, FieldType } from '../constant';
import { FieldCore } from '../field';

export const longTextFieldOptionsSchema = z
.object({ defaultValue: z.string().optional() })
.object({
defaultValue: z
.string()
.optional()
.transform((value) => (typeof value === 'string' ? value.trim() : value)),
})
.strict();

export type ILongTextFieldOptions = z.infer<typeof longTextFieldOptionsSchema>;
Expand Down Expand Up @@ -43,7 +48,7 @@ export class LongTextFieldCore extends FieldCore {
return null;
}

return value;
return value.trim();
}

repair(value: unknown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class MultipleSelectFieldCore extends SelectFieldCore {
}

let cellValue = value.split(/[\n\r,]\s?(?=(?:[^"]*"[^"]*")*[^"]*$)/).map((item) => {
return item.includes(',') ? item.slice(1, -1) : item;
return item.includes(',') ? item.slice(1, -1).trim() : item.trim();
});

cellValue = shouldExtend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { singleLineTextShowAsSchema } from '../show-as';

export const singlelineTextFieldOptionsSchema = z.object({
showAs: singleLineTextShowAsSchema.optional(),
defaultValue: z.string().optional(),
defaultValue: z
.string()
.optional()
.transform((value) => (typeof value === 'string' ? value.trim() : value)),
});

export type ISingleLineTextFieldOptions = z.infer<typeof singlelineTextFieldOptionsSchema>;
Expand Down Expand Up @@ -46,7 +49,7 @@ export class SingleLineTextFieldCore extends FieldCore {
}

// eslint-disable-next-line regexp/prefer-character-class
return value.replace(/\n|\r|\t/g, ' ');
return value.replace(/\n|\r|\t/g, ' ').trim();
}

repair(value: unknown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SingleSelectFieldCore extends SelectFieldCore {
return null;
}

const cellValue = String(value).replace(/\n|\r/g, ' ');
const cellValue = String(value).replace(/\n|\r/g, ' ').trim();
if (shouldExtend) {
return cellValue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/components/editor/text/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const TextEditorBase: ForwardRefRenderFunction<IEditorRef<string>, ITextEditor>
};

const saveValue = () => {
onChange?.(text || null);
onChange?.(text ? text.trim() : null);
};

const onJump = (type: SingleLineTextDisplayType) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const TextEditorBase: ForwardRefRenderFunction<

const saveValue = () => {
if (value === displayData || !isEditing) return;
onChange?.(type === CellType.Number ? Number(value) : value);
if (type === CellType.Number) {
onChange?.(Number(value));
} else {
onChange?.(typeof value === 'string' ? value.trim() : value);
}
};

const onChangeInner = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
Expand Down

0 comments on commit bb7abb6

Please sign in to comment.