fix: update useCreateActionProps & useUpdateActionProps

This commit is contained in:
chenos 2022-04-19 09:27:52 +08:00
parent 10dcc7fea0
commit fca0943e8d
2 changed files with 76 additions and 51 deletions

View File

@ -44,25 +44,29 @@ export const useCreateActionProps = () => {
if (!skipValidator) {
await form.submit();
}
const values = {};
for (const key in form.values) {
if (fieldNames.includes(key)) {
const items = form.values[key];
const collectionField = getField(key);
if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id';
if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') {
values[key] = items[targetKey];
let values = {};
if (resource instanceof TableFieldResource) {
values = form.values;
} else {
for (const key in form.values) {
if (fieldNames.includes(key)) {
const items = form.values[key];
const collectionField = getField(key);
if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id';
if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') {
values[key] = items[targetKey];
} else {
values[key] = items;
}
} else {
values[key] = items[targetKey];
values[key] = form.values[key];
}
} else {
values[key] = form.values[key];
}
} else {
values[key] = form.values[key];
}
}
await resource.create({
@ -110,28 +114,36 @@ export const useUpdateActionProps = () => {
await form.submit();
}
const fieldNames = fields.map((field) => field.name);
const values = {};
for (const key in form.values) {
if (fieldNames.includes(key)) {
if (!field.added.has(key)) {
continue;
}
const items = form.values[key];
const collectionField = getField(key);
if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id';
if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') {
values[key] = items[targetKey];
let values = {};
if (resource instanceof TableFieldResource) {
values = form.values;
} else {
for (const key in form.values) {
if (fieldNames.includes(key)) {
const collectionField = getField(key);
if (collectionField.interface === 'subTable') {
values[key] = form.values[key];
continue;
}
if (!field.added.has(key)) {
continue;
}
const items = form.values[key];
if (collectionField.interface === 'linkTo') {
const targetKey = collectionField.targetKey || 'id';
if (Array.isArray(items)) {
values[key] = items.map((item) => item[targetKey]);
} else if (items && typeof items === 'object') {
values[key] = items[targetKey];
} else {
values[key] = items;
}
} else {
values[key] = items[targetKey];
values[key] = form.values[key];
}
} else {
values[key] = form.values[key];
}
} else {
values[key] = form.values[key];
}
}
await resource.update({

View File

@ -97,19 +97,25 @@ export class CollectionManagerPlugin extends Plugin {
foreignKey: model.get('foreignKey'),
interface: 'linkTo',
reverseField: {
interface: 'linkTo',
interface: 'subTable',
uiSchema: {
type: 'void',
title: through.get('title'),
'x-component': 'RecordPicker',
'x-component-props': {
// mode: 'tags',
multiple: true,
fieldNames: {
label: 'id',
value: 'id',
},
},
'x-component': 'TableField',
'x-component-props': {},
},
// uiSchema: {
// title: through.get('title'),
// 'x-component': 'RecordPicker',
// 'x-component-props': {
// // mode: 'tags',
// multiple: true,
// fieldNames: {
// label: 'id',
// value: 'id',
// },
// },
// },
},
uiSchema: {
title: db.getCollection(sourceName)?.options?.title || sourceName,
@ -135,19 +141,26 @@ export class CollectionManagerPlugin extends Plugin {
foreignKey: model.get('otherKey'),
interface: 'linkTo',
reverseField: {
interface: 'linkTo',
interface: 'subTable',
uiSchema: {
type: 'void',
title: through.get('title'),
'x-component': 'RecordPicker',
'x-component-props': {
// mode: 'tags',
multiple: true,
fieldNames: {
label: 'id',
value: 'id',
},
},
'x-component': 'TableField',
'x-component-props': {},
},
// interface: 'linkTo',
// uiSchema: {
// title: through.get('title'),
// 'x-component': 'RecordPicker',
// 'x-component-props': {
// // mode: 'tags',
// multiple: true,
// fieldNames: {
// label: 'id',
// value: 'id',
// },
// },
// },
},
uiSchema: {
title: db.getCollection(targetName)?.options?.title || targetName,