fix: collection support title field setting (#4322)

This commit is contained in:
katherinehhh 2024-05-12 20:44:32 +08:00 committed by GitHub
parent 31b410dd69
commit 086984cb90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,8 +10,8 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { createForm, Field } from '@formily/core'; import { createForm, Field } from '@formily/core';
import { FieldContext, FormContext, useField } from '@formily/react'; import { FieldContext, FormContext, useField } from '@formily/react';
import { Space, Switch, Table, TableColumnProps, Tag, Tooltip } from 'antd'; import { Space, Switch, Table, TableColumnProps, Tag, Tooltip, message } from 'antd';
import React, { useContext, useMemo, createContext, useState } from 'react'; import React, { useContext, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { import {
Action, Action,
@ -36,6 +36,7 @@ import {
useResourceActionContext, useResourceActionContext,
useResourceContext, useResourceContext,
ViewCollectionField, ViewCollectionField,
useAPIClient,
} from '@nocobase/client'; } from '@nocobase/client';
import { collection } from './schemas/collectionFields'; import { collection } from './schemas/collectionFields';
@ -96,12 +97,16 @@ const CurrentFields = (props) => {
const { getInterface } = useCollectionManager_deprecated(); const { getInterface } = useCollectionManager_deprecated();
const { t } = useTranslation(); const { t } = useTranslation();
const { setState } = useResourceActionContext(); const { setState } = useResourceActionContext();
const { resource, targetKey } = props.collectionResource || {}; const { targetKey } = props.collectionResource || {};
const parentRecordData = useRecord(); const parentRecordData = useRecord();
const [loadingRecord, setLoadingRecord] = React.useState<any>(null); const [loadingRecord, setLoadingRecord] = React.useState<any>(null);
const { refreshCM, isTitleField, getTemplate } = useCollectionManager_deprecated(); const { refreshCM, isTitleField, getTemplate } = useCollectionManager_deprecated();
const { [targetKey]: filterByTk, titleField, template } = parentRecordData; const { [targetKey]: filterByTk, titleField: targetTitleField, template } = parentRecordData;
const [titleField, setTitleField] = useState(targetTitleField);
const targetTemplate = getTemplate(template); const targetTemplate = getTemplate(template);
const api = useAPIClient();
const columns: TableColumnProps<any>[] = [ const columns: TableColumnProps<any>[] = [
{ {
dataIndex: ['uiSchema', 'title'], dataIndex: ['uiSchema', 'title'],
@ -121,19 +126,18 @@ const CurrentFields = (props) => {
dataIndex: 'titleField', dataIndex: 'titleField',
title: t('Title field'), title: t('Title field'),
render: function Render(_, record) { render: function Render(_, record) {
const handleChange = (checked) => { const handleChange = async (checked) => {
setLoadingRecord(record); setLoadingRecord(record);
resource await api.request({
.update({ filterByTk, values: { titleField: checked ? record.name : 'id' } }) url: `collections:update?filterByTk=${filterByTk}`,
.then(async () => { method: 'post',
data: { titleField: checked ? record.name : 'id' },
});
message.success(t('Saved successfully'));
setTitleField(checked ? record.name : 'id');
await props.refreshAsync(); await props.refreshAsync();
setLoadingRecord(null); setLoadingRecord(null);
refreshCM(); refreshCM();
})
.catch((err) => {
setLoadingRecord(null);
console.error(err);
});
}; };
return isTitleField(record) ? ( return isTitleField(record) ? (