feat(database): add trim option for text field (#6603)

This commit is contained in:
Junyi 2025-04-01 17:47:12 +08:00 committed by GitHub
parent e6a3cbf613
commit 54817e1453
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View File

@ -31,6 +31,12 @@ export class TextareaFieldInterface extends CollectionFieldInterface {
titleUsable = true;
properties = {
...defaultProps,
trim: {
type: 'boolean',
'x-content': '{{t("Automatically remove heading and tailing spaces")}}',
'x-decorator': 'FormItem',
'x-component': 'Checkbox',
},
};
schemaInitialize(schema: ISchema, { block }) {
if (['Table', 'Kanban'].includes(block)) {

View File

@ -52,4 +52,18 @@ describe('text field', () => {
});
await Test.sync();
});
it('trim', async () => {
const collection = db.collection({
name: 'tests',
fields: [{ type: 'text', name: 'name', trim: true }],
});
await db.sync();
const model = await collection.model.create({
name: ' n1\n ',
});
expect(model.toJSON()).toMatchObject({
name: 'n1',
});
});
});

View File

@ -23,9 +23,20 @@ export class TextField extends Field {
this.options.defaultValue = null;
}
}
additionalSequelizeOptions() {
const { name, trim } = this.options;
return {
set(value) {
this.setDataValue(name, trim ? value?.trim() : value);
},
};
}
}
export interface TextFieldOptions extends BaseColumnFieldOptions {
type: 'text';
length?: 'tiny' | 'medium' | 'long';
trim?: boolean;
}