fix: change the interface options override of the field

This commit is contained in:
chenos 2020-12-09 12:23:57 +08:00
parent 32a8483336
commit f24d948f83
6 changed files with 52 additions and 5 deletions

View File

@ -13,7 +13,7 @@ describe('models.collection', () => {
afterEach(() => app.database.close()); afterEach(() => app.database.close());
it.only('import', async () => { it('import', async () => {
await app.database.getModel('collections').import({ await app.database.getModel('collections').import({
title: '示例', title: '示例',
name: 'examples', name: 'examples',

View File

@ -0,0 +1,23 @@
import { Agent, getAgent, getApp } from '../';
import { Application } from '@nocobase/server';
import * as types from '../../interfaces/types';
jest.setTimeout(30000);
import { FieldModel } from '../../models';
describe('models.field', () => {
let app: Application;
let agent: Agent;
beforeEach(async () => {
app = await getApp();
agent = getAgent(app);
});
afterEach(() => app.database.close());
it('updatedAt', async () => {
const Field = app.database.getModel('fields');
const field = new Field();
field.setInterface('updatedAt');
expect(field.get()).toMatchObject(types.updatedAt.options)
});
});

View File

@ -209,6 +209,17 @@ export default {
type: 'drawerSelect', type: 'drawerSelect',
}, },
}, },
{
interface: 'boolean',
type: 'virtual',
name: 'showTime',
title: '显示时间',
defaultValue: false,
component: {
type: 'boolean',
showInForm: true,
},
},
{ {
interface: 'boolean', interface: 'boolean',
type: 'boolean', type: 'boolean',

View File

@ -3,8 +3,9 @@ import * as types from '../interfaces/types';
import _ from 'lodash'; import _ from 'lodash';
export default async function (model: FieldModel) { export default async function (model: FieldModel) {
model.generateNameIfNull();
if (model.get('interface')) { if (model.get('interface')) {
model.setInterface(model.get('interface')); model.setInterface(model.get('interface'));
} }
// 生成随机 name 要放最后
model.generateNameIfNull();
} }

View File

@ -85,7 +85,7 @@ export const percent = {
sortable: true, sortable: true,
precision: 0, precision: 0,
component: { component: {
type: 'number', type: 'percent',
suffix: '%', suffix: '%',
}, },
}, },
@ -205,7 +205,7 @@ export const datetime = {
options: { options: {
interface: 'datetime', interface: 'datetime',
type: 'date', type: 'date',
dateonly: false, // dateonly showTime: false,
filterable: true, filterable: true,
sortable: true, sortable: true,
format: 'YYYY-MM-DD HH:mm:ss', format: 'YYYY-MM-DD HH:mm:ss',
@ -349,6 +349,8 @@ export const createdAt = {
options: { options: {
interface: 'createdAt', interface: 'createdAt',
type: 'date', type: 'date',
name: 'created_at',
showTime: true,
required: true, required: true,
filterable: true, filterable: true,
sortable: true, sortable: true,
@ -375,6 +377,8 @@ export const updatedAt = {
options: { options: {
interface: 'updatedAt', interface: 'updatedAt',
type: 'date', type: 'date',
name: 'updated_at',
showTime: true,
required: true, required: true,
filterable: true, filterable: true,
sortable: true, sortable: true,

View File

@ -22,8 +22,16 @@ export class FieldModel extends BaseModel {
setInterface(value) { setInterface(value) {
const { options } = types[value]; const { options } = types[value];
let args = [];
// 如果是新数据或 interface 不相等interface options 放后
if (this.isNewRecord || this.get('interface') !== value) {
args = [this.get(), options];
} else {
// 已存在的数据更新不相等interface options 放前面
args = [options, this.get()];
}
// @ts-ignore // @ts-ignore
const values = Utils.merge(options, this.get()); const values = Utils.merge(...args);
this.set(values); this.set(values);
} }