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

View File

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